[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/phpbb/template/twig/ -> twig.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\exception\user_object_not_available;
  17  
  18  /**
  19  * Twig Template class.
  20  */
  21  class twig extends \phpbb\template\base
  22  {
  23      /**
  24      * Path of the cache directory for the template
  25      *
  26      * Cannot be changed during runtime.
  27      *
  28      * @var string
  29      */
  30      private $cachepath = '';
  31  
  32      /**
  33      * phpBB path helper
  34      * @var \phpbb\path_helper
  35      */
  36      protected $path_helper;
  37  
  38      /**
  39      * phpBB root path
  40      * @var string
  41      */
  42      protected $phpbb_root_path;
  43  
  44      /**
  45      * PHP file extension
  46      * @var string
  47      */
  48      protected $php_ext;
  49  
  50      /**
  51      * phpBB config instance
  52      * @var \phpbb\config\config
  53      */
  54      protected $config;
  55  
  56      /**
  57      * Current user
  58      * @var \phpbb\user
  59      */
  60      protected $user;
  61  
  62      /**
  63      * Extension manager.
  64      *
  65      * @var \phpbb\extension\manager
  66      */
  67      protected $extension_manager;
  68  
  69      /**
  70      * Twig Environment
  71      *
  72      * @var \Twig\Environment
  73      */
  74      protected $twig;
  75  
  76      /**
  77      * Constructor.
  78      *
  79      * @param \phpbb\path_helper $path_helper
  80      * @param \phpbb\config\config $config
  81      * @param \phpbb\template\context $context template context
  82      * @param \phpbb\template\twig\environment $twig_environment
  83      * @param string $cache_path
  84      * @param \phpbb\user|null $user
  85      * @param array|\ArrayAccess $extensions
  86      * @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
  87      */
  88  	public function __construct(\phpbb\path_helper $path_helper, $config, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, \phpbb\user $user = null, $extensions = array(), \phpbb\extension\manager $extension_manager = null)
  89      {
  90          $this->path_helper = $path_helper;
  91          $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
  92          $this->php_ext = $path_helper->get_php_ext();
  93          $this->config = $config;
  94          $this->user = $user;
  95          $this->context = $context;
  96          $this->extension_manager = $extension_manager;
  97          $this->cachepath = $cache_path;
  98          $this->twig = $twig_environment;
  99  
 100          foreach ($extensions as $extension)
 101          {
 102              $this->twig->addExtension($extension);
 103          }
 104  
 105          // Add admin namespace
 106          if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))
 107          {
 108              $this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
 109          }
 110      }
 111  
 112      /**
 113      * Clear the cache
 114      *
 115      * @return \phpbb\template\template
 116      */
 117  	public function clear_cache()
 118      {
 119          if (is_dir($this->cachepath))
 120          {
 121              $this->twig->clearCacheFiles();
 122          }
 123  
 124          return $this;
 125      }
 126  
 127      /**
 128      * Get the style tree of the style preferred by the current user
 129      *
 130      * @return array Style tree, most specific first
 131      *
 132      * @throws \phpbb\template\exception\user_object_not_available    When user service was not set
 133      */
 134  	public function get_user_style()
 135      {
 136          if ($this->user === null)
 137          {
 138              throw new user_object_not_available();
 139          }
 140  
 141          $style_list = array(
 142              $this->user->style['style_path'],
 143          );
 144  
 145          if ($this->user->style['style_parent_id'])
 146          {
 147              $style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
 148          }
 149  
 150          return $style_list;
 151      }
 152  
 153      /**
 154      * Set style location based on (current) user's chosen style.
 155      *
 156      * @param array $style_directories The directories to add style paths for
 157      *     E.g. array('ext/foo/bar/styles', 'styles')
 158      *     Default: array('styles') (phpBB's style directory)
 159      * @return \phpbb\template\template $this
 160      */
 161  	public function set_style($style_directories = array('styles'))
 162      {
 163          if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array())
 164          {
 165              // We should set up the core styles path since not already setup
 166              $this->set_style();
 167          }
 168  
 169          $names = $this->get_user_style();
 170          // Add 'all' folder to $names array
 171          //    It allows extensions to load a template file from 'all' folder,
 172          //    if a style doesn't include it.
 173          $names[] = 'all';
 174  
 175          $paths = array();
 176          foreach ($style_directories as $directory)
 177          {
 178              foreach ($names as $name)
 179              {
 180                  $path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
 181                  $template_path = $path . 'template/';
 182                  $theme_path = $path . 'theme/';
 183  
 184                  $is_valid_dir = false;
 185                  if (is_dir($template_path))
 186                  {
 187                      $is_valid_dir = true;
 188                      $paths[] = $template_path;
 189                  }
 190                  if (is_dir($theme_path))
 191                  {
 192                      $is_valid_dir = true;
 193                      $paths[] = $theme_path;
 194                  }
 195  
 196                  if ($is_valid_dir)
 197                  {
 198                      // Add the base style directory as a safe directory
 199                      $this->twig->getLoader()->addSafeDirectory($path);
 200                  }
 201              }
 202          }
 203  
 204          // If we're setting up the main phpBB styles directory and the core
 205          // namespace isn't setup yet, we will set it up now
 206          if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array())
 207          {
 208              // Set up the core style paths namespace
 209              $this->twig->getLoader()->setPaths($paths, 'core');
 210          }
 211  
 212          $this->set_custom_style($names, $paths);
 213  
 214          return $this;
 215      }
 216  
 217      /**
 218      * Set custom style location (able to use directory outside of phpBB).
 219      *
 220      * Note: Templates are still compiled to phpBB's cache directory.
 221      *
 222      * @param string|array $names Array of names (or detailed names) or string of name of template(s) in inheritance tree order, used by extensions.
 223      *    E.g. array(
 224      *            'name'         => 'adm',
 225      *            'ext_path'     => 'adm/style/',
 226      *        )
 227      * @param string|array of string $paths Array of style paths, relative to current root directory
 228      * @return \phpbb\template\template $this
 229      */
 230  	public function set_custom_style($names, $paths)
 231      {
 232          $paths = (is_string($paths)) ? array($paths) : $paths;
 233          $names = (is_string($names)) ? array($names) : $names;
 234  
 235          // Set as __main__ namespace
 236          $this->twig->getLoader()->setPaths($paths);
 237  
 238          // Add all namespaces for all extensions
 239          if ($this->extension_manager instanceof \phpbb\extension\manager)
 240          {
 241              $names[] = 'all';
 242  
 243              foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
 244              {
 245                  // namespaces cannot contain /
 246                  $namespace = str_replace('/', '_', $ext_namespace);
 247                  $paths = array();
 248  
 249                  foreach ($names as $template_dir)
 250                  {
 251                      if (is_array($template_dir))
 252                      {
 253                          if (isset($template_dir['ext_path']))
 254                          {
 255                              $ext_style_template_path = $ext_path . $template_dir['ext_path'];
 256                              $ext_style_path = dirname($ext_style_template_path);
 257                              $ext_style_theme_path = $ext_style_path . 'theme/';
 258                          }
 259                          else
 260                          {
 261                              $ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
 262                              $ext_style_template_path = $ext_style_path . 'template/';
 263                              $ext_style_theme_path = $ext_style_path . 'theme/';
 264                          }
 265                      }
 266                      else
 267                      {
 268                          $ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
 269                          $ext_style_template_path = $ext_style_path . 'template/';
 270                          $ext_style_theme_path = $ext_style_path . 'theme/';
 271                      }
 272  
 273                      $is_valid_dir = false;
 274                      if (is_dir($ext_style_template_path))
 275                      {
 276                          $is_valid_dir = true;
 277                          $paths[] = $ext_style_template_path;
 278                      }
 279                      if (is_dir($ext_style_theme_path))
 280                      {
 281                          $is_valid_dir = true;
 282                          $paths[] = $ext_style_theme_path;
 283                      }
 284  
 285                      if ($is_valid_dir)
 286                      {
 287                          // Add the base style directory as a safe directory
 288                          $this->twig->getLoader()->addSafeDirectory($ext_style_path);
 289                      }
 290                  }
 291  
 292                  $this->twig->getLoader()->setPaths($paths, $namespace);
 293              }
 294          }
 295  
 296          return $this;
 297      }
 298  
 299      /**
 300      * Display a template for provided handle.
 301      *
 302      * The template will be loaded and compiled, if necessary, first.
 303      *
 304      * This function calls hooks.
 305      *
 306      * @param string $handle Handle to display
 307      * @return \phpbb\template\template $this
 308      */
 309  	public function display($handle)
 310      {
 311          $result = $this->call_hook($handle, __FUNCTION__);
 312          if ($result !== false)
 313          {
 314              return $result[0];
 315          }
 316  
 317          $this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
 318  
 319          return $this;
 320      }
 321  
 322      /**
 323      * Display the handle and assign the output to a template variable
 324      * or return the compiled result.
 325      *
 326      * @param string $handle Handle to operate on
 327      * @param string $template_var Template variable to assign compiled handle to
 328      * @param bool $return_content If true return compiled handle, otherwise assign to $template_var
 329      * @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
 330      */
 331  	public function assign_display($handle, $template_var = '', $return_content = true)
 332      {
 333          if ($return_content)
 334          {
 335              return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
 336          }
 337  
 338          $this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars()));
 339  
 340          return $this;
 341      }
 342  
 343      /**
 344      * Get template vars in a format Twig will use (from the context)
 345      *
 346      * @return array
 347      */
 348  	protected function get_template_vars()
 349      {
 350          $context_vars = $this->context->get_data_ref();
 351  
 352          $vars = array_merge(
 353              $context_vars['.'][0], // To get normal vars
 354              array(
 355                  'definition'    => new \phpbb\template\twig\definition(),
 356                  'loops'            => $context_vars, // To get loops
 357              )
 358          );
 359  
 360          if ($this->user instanceof \phpbb\user)
 361          {
 362              $vars['user'] = $this->user;
 363          }
 364  
 365          // cleanup
 366          unset($vars['loops']['.']);
 367  
 368          // Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops.
 369          foreach ($vars['loops'] as $key => &$value)
 370          {
 371              $vars[$key] = $value;
 372          }
 373  
 374          return $vars;
 375      }
 376  
 377      /**
 378      * {@inheritdoc}
 379      */
 380  	public function get_source_file_for_handle($handle)
 381      {
 382          return $this->twig->getLoader()->getCacheKey($this->get_filename_from_handle($handle));
 383      }
 384  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1