[ Index ]

PHP Cross Reference of phpBB-3.3.14-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  
  73          $this->assets_bag = new assets_bag();
  74  
  75          $options = array_merge(array(
  76              'cache'            => (defined('IN_INSTALL')) ? false : $cache_path,
  77              'debug'            => false,
  78              'auto_reload'    => (bool) $this->phpbb_config['load_tplcompile'],
  79              'autoescape'    => false,
  80          ), $options);
  81  
  82          parent::__construct($loader, $options);
  83      }
  84  
  85      /**
  86      * Get the list of enabled phpBB extensions
  87      *
  88      * Used in EVENT node
  89      *
  90      * @return array
  91      */
  92  	public function get_phpbb_extensions()
  93      {
  94          return ($this->extension_manager) ? $this->extension_manager->all_enabled() : array();
  95      }
  96  
  97      /**
  98      * Get phpBB config
  99      *
 100      * @return \phpbb\config\config
 101      */
 102  	public function get_phpbb_config()
 103      {
 104          return $this->phpbb_config;
 105      }
 106  
 107      /**
 108       * Get the phpBB root path
 109       *
 110       * @return string
 111       */
 112  	public function get_phpbb_root_path()
 113      {
 114          return $this->phpbb_root_path;
 115      }
 116  
 117      /**
 118      * Get the filesystem object
 119      *
 120      * @return \phpbb\filesystem\filesystem
 121      */
 122  	public function get_filesystem()
 123      {
 124          return $this->filesystem;
 125      }
 126  
 127      /**
 128      * Get the web root path
 129      *
 130      * @return string
 131      */
 132  	public function get_web_root_path()
 133      {
 134          return $this->web_root_path ?? $this->web_root_path = $this->phpbb_path_helper->get_web_root_path();
 135      }
 136  
 137      /**
 138      * Get the phpbb path helper object
 139      *
 140      * @return \phpbb\path_helper
 141      */
 142  	public function get_path_helper()
 143      {
 144          return $this->phpbb_path_helper;
 145      }
 146  
 147      /**
 148       * Gets the assets bag
 149       *
 150       * @return assets_bag
 151       */
 152  	public function get_assets_bag()
 153      {
 154          return $this->assets_bag;
 155      }
 156  
 157      /**
 158      * Get the namespace look up order
 159      *
 160      * @return array
 161      */
 162  	public function getNamespaceLookUpOrder()
 163      {
 164          return $this->namespace_look_up_order;
 165      }
 166  
 167      /**
 168      * Set the namespace look up order to load templates from
 169      *
 170      * @param array $namespace
 171      * @return \Twig\Environment
 172      */
 173  	public function setNamespaceLookUpOrder($namespace)
 174      {
 175          $this->namespace_look_up_order = $namespace;
 176  
 177          return $this;
 178      }
 179  
 180      /**
 181       * {@inheritdoc}
 182       */
 183  	public function render($name, array $context = [])
 184      {
 185          return $this->display_with_assets($name, $context);
 186      }
 187  
 188      /**
 189       * {@inheritdoc}
 190       */
 191  	public function display($name, array $context = [])
 192      {
 193          echo $this->display_with_assets($name, $context);
 194      }
 195  
 196      /**
 197       * {@inheritdoc}
 198       */
 199  	private function display_with_assets($name, array $context = [])
 200      {
 201          $placeholder_salt = unique_id();
 202  
 203          if (array_key_exists('definition', $context))
 204          {
 205              $context['definition']->set('SCRIPTS', '__SCRIPTS_' . $placeholder_salt . '__');
 206              $context['definition']->set('STYLESHEETS', '__STYLESHEETS_' . $placeholder_salt . '__');
 207          }
 208  
 209          /**
 210          * Allow changing the template output stream before rendering
 211          *
 212          * @event core.twig_environment_render_template_before
 213          * @var    array    context        Array with template variables
 214          * @var    string  name        The template name
 215          * @since 3.2.1-RC1
 216          */
 217          if ($this->phpbb_dispatcher)
 218          {
 219              $vars = array('context', 'name');
 220              extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_before', compact($vars)));
 221          }
 222  
 223          $output = parent::render($name, $context);
 224  
 225          /**
 226          * Allow changing the template output stream after rendering
 227          *
 228          * @event core.twig_environment_render_template_after
 229          * @var    array    context        Array with template variables
 230          * @var    string  name        The template name
 231          * @var    string    output        Rendered template output stream
 232          * @since 3.2.1-RC1
 233          */
 234          if ($this->phpbb_dispatcher)
 235          {
 236              $vars = array('context', 'name', 'output');
 237              extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_after', compact($vars)));
 238          }
 239  
 240          return $this->inject_assets($output, $placeholder_salt);
 241      }
 242  
 243      /**
 244       * Injects the assets (from INCLUDECSS/JS) in the output.
 245       *
 246       * @param string $output
 247       *
 248       * @return string
 249       */
 250  	private function inject_assets($output, $placeholder_salt)
 251      {
 252          $output = str_replace('__STYLESHEETS_' . $placeholder_salt . '__', $this->assets_bag->get_stylesheets_content(), $output);
 253          $output = str_replace('__SCRIPTS_' . $placeholder_salt . '__', $this->assets_bag->get_scripts_content(), $output);
 254  
 255          return $output;
 256      }
 257  
 258      /**
 259      * Loads a template by name.
 260      *
 261      * @param string  $name  The template name
 262      * @param integer $index The index if it is an embedded template
 263      * @return \Twig\Template A template instance representing the given template name
 264      * @throws \Twig\Error\LoaderError
 265      */
 266  	public function loadTemplate($name, $index = null)
 267      {
 268          if (strpos($name, '@') === false)
 269          {
 270              foreach ($this->getNamespaceLookUpOrder() as $namespace)
 271              {
 272                  try
 273                  {
 274                      if ($namespace === '__main__')
 275                      {
 276                          return parent::loadTemplate($name, $index);
 277                      }
 278  
 279                      return parent::loadTemplate('@' . $namespace . '/' . $name, $index);
 280                  }
 281                  catch (\Twig\Error\LoaderError $e)
 282                  {
 283                  }
 284              }
 285  
 286              // We were unable to load any templates
 287              throw $e;
 288          }
 289          else
 290          {
 291              return parent::loadTemplate($name, $index);
 292          }
 293      }
 294  
 295      /**
 296      * Finds a template by name.
 297      *
 298      * @param string  $name  The template name
 299      * @return string
 300      * @throws \Twig\Error\LoaderError
 301      */
 302  	public function findTemplate($name)
 303      {
 304          if (strpos($name, '@') === false)
 305          {
 306              foreach ($this->getNamespaceLookUpOrder() as $namespace)
 307              {
 308                  try
 309                  {
 310                      if ($namespace === '__main__')
 311                      {
 312                          return parent::getLoader()->getCacheKey($name);
 313                      }
 314  
 315                      return parent::getLoader()->getCacheKey('@' . $namespace . '/' . $name);
 316                  }
 317                  catch (\Twig\Error\LoaderError $e)
 318                  {
 319                  }
 320              }
 321  
 322              // We were unable to load any templates
 323              throw $e;
 324          }
 325          else
 326          {
 327              return parent::getLoader()->getCacheKey($name);
 328          }
 329      }
 330  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1