[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/phpbb/template/twig/ -> environment.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\template\twig;
  15  
  16  use phpbb\template\assets_bag;
  17  
  18  class environment extends \Twig\Environment
  19  {
  20      /** @var \phpbb\config\config */
  21      protected $phpbb_config;
  22  
  23      /** @var \phpbb\filesystem\filesystem */
  24      protected $filesystem;
  25  
  26      /** @var \phpbb\path_helper */
  27      protected $phpbb_path_helper;
  28  
  29      /** @var \Symfony\Component\DependencyInjection\ContainerInterface */
  30      protected $container;
  31  
  32      /** @var \phpbb\extension\manager */
  33      protected $extension_manager;
  34  
  35      /** @var \phpbb\event\dispatcher_interface */
  36      protected $phpbb_dispatcher;
  37  
  38      /** @var string */
  39      protected $phpbb_root_path;
  40  
  41      /** @var string */
  42      protected $web_root_path;
  43  
  44      /** @var array **/
  45      protected $namespace_look_up_order = array('__main__');
  46  
  47      /** @var assets_bag */
  48      protected $assets_bag;
  49  
  50      /**
  51      * Constructor
  52      *
  53      * @param \phpbb\config\config $phpbb_config The phpBB configuration
  54      * @param \phpbb\filesystem\filesystem $filesystem
  55      * @param \phpbb\path_helper $path_helper phpBB path helper
  56      * @param string $cache_path The path to the cache directory
  57      * @param \phpbb\extension\manager $extension_manager phpBB extension manager
  58      * @param \Twig\Loader\LoaderInterface $loader Twig loader interface
  59      * @param \phpbb\event\dispatcher_interface    $phpbb_dispatcher    Event dispatcher object
  60      * @param array $options Array of options to pass to Twig
  61      */
  62  	public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig\Loader\LoaderInterface $loader = null, \phpbb\event\dispatcher_interface $phpbb_dispatcher = null, $options = array())
  63      {
  64          $this->phpbb_config = $phpbb_config;
  65  
  66          $this->filesystem = $filesystem;
  67          $this->phpbb_path_helper = $path_helper;
  68          $this->extension_manager = $extension_manager;
  69          $this->phpbb_dispatcher = $phpbb_dispatcher;
  70  
  71          $this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path();
  72          $this->web_root_path = $this->phpbb_path_helper->get_web_root_path();
  73  
  74          $this->assets_bag = new assets_bag();
  75  
  76          $options = array_merge(array(
  77              'cache'            => (defined('IN_INSTALL')) ? false : $cache_path,
  78              'debug'            => false,
  79              'auto_reload'    => (bool) $this->phpbb_config['load_tplcompile'],
  80              'autoescape'    => false,
  81          ), $options);
  82  
  83          parent::__construct($loader, $options);
  84      }
  85  
  86      /**
  87      * Get the list of enabled phpBB extensions
  88      *
  89      * Used in EVENT node
  90      *
  91      * @return array
  92      */
  93  	public function get_phpbb_extensions()
  94      {
  95          return ($this->extension_manager) ? $this->extension_manager->all_enabled() : array();
  96      }
  97  
  98      /**
  99      * Get phpBB config
 100      *
 101      * @return \phpbb\config\config
 102      */
 103  	public function get_phpbb_config()
 104      {
 105          return $this->phpbb_config;
 106      }
 107  
 108      /**
 109       * Get the phpBB root path
 110       *
 111       * @return string
 112       */
 113  	public function get_phpbb_root_path()
 114      {
 115          return $this->phpbb_root_path;
 116      }
 117  
 118      /**
 119      * Get the filesystem object
 120      *
 121      * @return \phpbb\filesystem\filesystem
 122      */
 123  	public function get_filesystem()
 124      {
 125          return $this->filesystem;
 126      }
 127  
 128      /**
 129      * Get the web root path
 130      *
 131      * @return string
 132      */
 133  	public function get_web_root_path()
 134      {
 135          return $this->web_root_path;
 136      }
 137  
 138      /**
 139      * Get the phpbb path helper object
 140      *
 141      * @return \phpbb\path_helper
 142      */
 143  	public function get_path_helper()
 144      {
 145          return $this->phpbb_path_helper;
 146      }
 147  
 148      /**
 149       * Gets the assets bag
 150       *
 151       * @return assets_bag
 152       */
 153  	public function get_assets_bag()
 154      {
 155          return $this->assets_bag;
 156      }
 157  
 158      /**
 159      * Get the namespace look up order
 160      *
 161      * @return array
 162      */
 163  	public function getNamespaceLookUpOrder()
 164      {
 165          return $this->namespace_look_up_order;
 166      }
 167  
 168      /**
 169      * Set the namespace look up order to load templates from
 170      *
 171      * @param array $namespace
 172      * @return \Twig\Environment
 173      */
 174  	public function setNamespaceLookUpOrder($namespace)
 175      {
 176          $this->namespace_look_up_order = $namespace;
 177  
 178          return $this;
 179      }
 180  
 181      /**
 182       * {@inheritdoc}
 183       */
 184  	public function render($name, array $context = [])
 185      {
 186          return $this->display_with_assets($name, $context);
 187      }
 188  
 189      /**
 190       * {@inheritdoc}
 191       */
 192  	public function display($name, array $context = [])
 193      {
 194          echo $this->display_with_assets($name, $context);
 195      }
 196  
 197      /**
 198       * {@inheritdoc}
 199       */
 200  	private function display_with_assets($name, array $context = [])
 201      {
 202          $placeholder_salt = unique_id();
 203  
 204          if (array_key_exists('definition', $context))
 205          {
 206              $context['definition']->set('SCRIPTS', '__SCRIPTS_' . $placeholder_salt . '__');
 207              $context['definition']->set('STYLESHEETS', '__STYLESHEETS_' . $placeholder_salt . '__');
 208          }
 209  
 210          /**
 211          * Allow changing the template output stream before rendering
 212          *
 213          * @event core.twig_environment_render_template_before
 214          * @var    array    context        Array with template variables
 215          * @var    string  name        The template name
 216          * @since 3.2.1-RC1
 217          */
 218          if ($this->phpbb_dispatcher)
 219          {
 220              $vars = array('context', 'name');
 221              extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_before', compact($vars)));
 222          }
 223  
 224          $output = parent::render($name, $context);
 225  
 226          /**
 227          * Allow changing the template output stream after rendering
 228          *
 229          * @event core.twig_environment_render_template_after
 230          * @var    array    context        Array with template variables
 231          * @var    string  name        The template name
 232          * @var    string    output        Rendered template output stream
 233          * @since 3.2.1-RC1
 234          */
 235          if ($this->phpbb_dispatcher)
 236          {
 237              $vars = array('context', 'name', 'output');
 238              extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_after', compact($vars)));
 239          }
 240  
 241          return $this->inject_assets($output, $placeholder_salt);
 242      }
 243  
 244      /**
 245       * Injects the assets (from INCLUDECSS/JS) in the output.
 246       *
 247       * @param string $output
 248       *
 249       * @return string
 250       */
 251  	private function inject_assets($output, $placeholder_salt)
 252      {
 253          $output = str_replace('__STYLESHEETS_' . $placeholder_salt . '__', $this->assets_bag->get_stylesheets_content(), $output);
 254          $output = str_replace('__SCRIPTS_' . $placeholder_salt . '__', $this->assets_bag->get_scripts_content(), $output);
 255  
 256          return $output;
 257      }
 258  
 259      /**
 260      * Loads a template by name.
 261      *
 262      * @param string  $name  The template name
 263      * @param integer $index The index if it is an embedded template
 264      * @return \Twig\Template A template instance representing the given template name
 265      * @throws \Twig\Error\LoaderError
 266      */
 267  	public function loadTemplate($name, $index = null)
 268      {
 269          if (strpos($name, '@') === false)
 270          {
 271              foreach ($this->getNamespaceLookUpOrder() as $namespace)
 272              {
 273                  try
 274                  {
 275                      if ($namespace === '__main__')
 276                      {
 277                          return parent::loadTemplate($name, $index);
 278                      }
 279  
 280                      return parent::loadTemplate('@' . $namespace . '/' . $name, $index);
 281                  }
 282                  catch (\Twig\Error\LoaderError $e)
 283                  {
 284                  }
 285              }
 286  
 287              // We were unable to load any templates
 288              throw $e;
 289          }
 290          else
 291          {
 292              return parent::loadTemplate($name, $index);
 293          }
 294      }
 295  
 296      /**
 297      * Finds a template by name.
 298      *
 299      * @param string  $name  The template name
 300      * @return string
 301      * @throws \Twig\Error\LoaderError
 302      */
 303  	public function findTemplate($name)
 304      {
 305          if (strpos($name, '@') === false)
 306          {
 307              foreach ($this->getNamespaceLookUpOrder() as $namespace)
 308              {
 309                  try
 310                  {
 311                      if ($namespace === '__main__')
 312                      {
 313                          return parent::getLoader()->getCacheKey($name);
 314                      }
 315  
 316                      return parent::getLoader()->getCacheKey('@' . $namespace . '/' . $name);
 317                  }
 318                  catch (\Twig\Error\LoaderError $e)
 319                  {
 320                  }
 321              }
 322  
 323              // We were unable to load any templates
 324              throw $e;
 325          }
 326          else
 327          {
 328              return parent::getLoader()->getCacheKey($name);
 329          }
 330      }
 331  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1