Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

twig.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 8.93 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\template\twig;
 015   
 016  /**
 017  * Twig Template class.
 018  */
 019  class twig extends \phpbb\template\base
 020  {
 021      /**
 022      * Path of the cache directory for the template
 023      *
 024      * Cannot be changed during runtime.
 025      *
 026      * @var string
 027      */
 028      private $cachepath = '';
 029   
 030      /**
 031      * phpBB path helper
 032      * @var \phpbb\path_helper
 033      */
 034      protected $path_helper;
 035   
 036      /**
 037      * phpBB root path
 038      * @var string
 039      */
 040      protected $phpbb_root_path;
 041   
 042      /**
 043      * PHP file extension
 044      * @var string
 045      */
 046      protected $php_ext;
 047   
 048      /**
 049      * phpBB config instance
 050      * @var \phpbb\config\config
 051      */
 052      protected $config;
 053   
 054      /**
 055      * Current user
 056      * @var \phpbb\user
 057      */
 058      protected $user;
 059   
 060      /**
 061      * Extension manager.
 062      *
 063      * @var \phpbb\extension\manager
 064      */
 065      protected $extension_manager;
 066   
 067      /**
 068      * Twig Environment
 069      *
 070      * @var \Twig_Environment
 071      */
 072      protected $twig;
 073   
 074      /**
 075      * Constructor.
 076      *
 077      * @param \phpbb\path_helper $path_helper
 078      * @param \phpbb\config\config $config
 079      * @param \phpbb\user $user
 080      * @param \phpbb\template\context $context template context
 081      * @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
 082      */
 083      public function __construct(\phpbb\path_helper $path_helper, $config, $user, \phpbb\template\context $context, \phpbb\extension\manager $extension_manager = null)
 084      {
 085          $this->path_helper = $path_helper;
 086          $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
 087          $this->php_ext = $path_helper->get_php_ext();
 088          $this->config = $config;
 089          $this->user = $user;
 090          $this->context = $context;
 091          $this->extension_manager = $extension_manager;
 092   
 093          $this->cachepath = $this->phpbb_root_path . 'cache/twig/';
 094   
 095          // Initiate the loader, __main__ namespace paths will be setup later in set_style_names()
 096          $loader = new \phpbb\template\twig\loader('');
 097   
 098          $this->twig = new \phpbb\template\twig\environment(
 099              $this->config,
 100              $this->path_helper,
 101              $this->extension_manager,
 102              $loader,
 103              array(
 104                  'cache'            => (defined('IN_INSTALL')) ? false : $this->cachepath,
 105                  'debug'            => defined('DEBUG'),
 106                  'auto_reload'    => (bool) $this->config['load_tplcompile'],
 107                  'autoescape'    => false,
 108              )
 109          );
 110   
 111          $this->twig->addExtension(
 112              new \phpbb\template\twig\extension(
 113                  $this->context,
 114                  $this->user
 115              )
 116          );
 117   
 118          $lexer = new \phpbb\template\twig\lexer($this->twig);
 119   
 120          $this->twig->setLexer($lexer);
 121   
 122          // Add admin namespace
 123          if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))
 124          {
 125              $this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
 126          }
 127      }
 128   
 129      /**
 130      * Clear the cache
 131      *
 132      * @return \phpbb\template\template
 133      */
 134      public function clear_cache()
 135      {
 136          if (is_dir($this->cachepath))
 137          {
 138              $this->twig->clearCacheFiles();
 139          }
 140   
 141          return $this;
 142      }
 143   
 144      /**
 145      * Get the style tree of the style preferred by the current user
 146      *
 147      * @return array Style tree, most specific first
 148      */
 149      public function get_user_style()
 150      {
 151          $style_list = array(
 152              $this->user->style['style_path'],
 153          );
 154   
 155          if ($this->user->style['style_parent_id'])
 156          {
 157              $style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
 158          }
 159   
 160          return $style_list;
 161      }
 162   
 163      /**
 164      * Set style location based on (current) user's chosen style.
 165      *
 166      * @param array $style_directories The directories to add style paths for
 167      *     E.g. array('ext/foo/bar/styles', 'styles')
 168      *     Default: array('styles') (phpBB's style directory)
 169      * @return \phpbb\template\template $this
 170      */
 171      public function set_style($style_directories = array('styles'))
 172      {
 173          if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array())
 174          {
 175              // We should set up the core styles path since not already setup
 176              $this->set_style();
 177          }
 178   
 179          $names = $this->get_user_style();
 180          // Add 'all' folder to $names array
 181          //    It allows extensions to load a template file from 'all' folder,
 182          //    if a style doesn't include it.
 183          $names[] = 'all';
 184   
 185          $paths = array();
 186          foreach ($style_directories as $directory)
 187          {
 188              foreach ($names as $name)
 189              {
 190                  $path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
 191                  $template_path = $path . 'template/';
 192   
 193                  if (is_dir($template_path))
 194                  {
 195                      // Add the base style directory as a safe directory
 196                      $this->twig->getLoader()->addSafeDirectory($path);
 197   
 198                      $paths[] = $template_path;
 199                  }
 200              }
 201          }
 202   
 203          // If we're setting up the main phpBB styles directory and the core
 204          // namespace isn't setup yet, we will set it up now
 205          if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array())
 206          {
 207              // Set up the core style paths namespace
 208              $this->twig->getLoader()->setPaths($paths, 'core');
 209          }
 210   
 211          $this->set_custom_style($names, $paths);
 212   
 213          return $this;
 214      }
 215   
 216      /**
 217      * Set custom style location (able to use directory outside of phpBB).
 218      *
 219      * Note: Templates are still compiled to phpBB's cache directory.
 220      *
 221      * @param string|array $names Array of names (or detailed names) or string of name of template(s) in inheritance tree order, used by extensions.
 222      *    E.g. array(
 223      *            'name'         => 'adm',
 224      *            'ext_path'     => 'adm/style/',
 225      *        )
 226      * @param string|array of string $paths Array of style paths, relative to current root directory
 227      * @return \phpbb\template\template $this
 228      */
 229      public function set_custom_style($names, $paths)
 230      {
 231          $paths = (is_string($paths)) ? array($paths) : $paths;
 232          $names = (is_string($names)) ? array($names) : $names;
 233   
 234          // Set as __main__ namespace
 235          $this->twig->getLoader()->setPaths($paths);
 236   
 237          // Add all namespaces for all extensions
 238          if ($this->extension_manager instanceof \phpbb\extension\manager)
 239          {
 240              $names[] = 'all';
 241   
 242              foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
 243              {
 244                  // namespaces cannot contain /
 245                  $namespace = str_replace('/', '_', $ext_namespace);
 246                  $paths = array();
 247   
 248                  foreach ($names as $template_dir)
 249                  {
 250                      if (is_array($template_dir))
 251                      {
 252                          if (isset($template_dir['ext_path']))
 253                          {
 254                              $ext_style_template_path = $ext_path . $template_dir['ext_path'];
 255                              $ext_style_path = dirname($ext_style_template_path);
 256                          }
 257                          else
 258                          {
 259                              $ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
 260                              $ext_style_template_path = $ext_style_path . 'template/';
 261                          }
 262                      }
 263                      else
 264                      {
 265                          $ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
 266                          $ext_style_template_path = $ext_style_path . 'template/';
 267                      }
 268   
 269                      if (is_dir($ext_style_template_path))
 270                      {
 271                          // Add the base style directory as a safe directory
 272                          $this->twig->getLoader()->addSafeDirectory($ext_style_path);
 273   
 274                          $paths[] = $ext_style_template_path;
 275                      }
 276                  }
 277   
 278                  $this->twig->getLoader()->setPaths($paths, $namespace);
 279              }
 280          }
 281   
 282          return $this;
 283      }
 284   
 285      /**
 286      * Display a template for provided handle.
 287      *
 288      * The template will be loaded and compiled, if necessary, first.
 289      *
 290      * This function calls hooks.
 291      *
 292      * @param string $handle Handle to display
 293      * @return \phpbb\template\template $this
 294      */
 295      public function display($handle)
 296      {
 297          $result = $this->call_hook($handle, __FUNCTION__);
 298          if ($result !== false)
 299          {
 300              return $result[0];
 301          }
 302   
 303          $this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
 304   
 305          return $this;
 306      }
 307   
 308      /**
 309      * Display the handle and assign the output to a template variable
 310      * or return the compiled result.
 311      *
 312      * @param string $handle Handle to operate on
 313      * @param string $template_var Template variable to assign compiled handle to
 314      * @param bool $return_content If true return compiled handle, otherwise assign to $template_var
 315      * @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
 316      */
 317      public function assign_display($handle, $template_var = '', $return_content = true)
 318      {
 319          if ($return_content)
 320          {
 321              return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
 322          }
 323   
 324          $this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle, $this->get_template_vars())));
 325   
 326          return $this;
 327      }
 328   
 329      /**
 330      * Get template vars in a format Twig will use (from the context)
 331      *
 332      * @return array
 333      */
 334      protected function get_template_vars()
 335      {
 336          $context_vars = $this->context->get_data_ref();
 337   
 338          $vars = array_merge(
 339              $context_vars['.'][0], // To get normal vars
 340              array(
 341                  'definition'    => new \phpbb\template\twig\definition(),
 342                  'user'            => $this->user,
 343                  'loops'            => $context_vars, // To get loops
 344              )
 345          );
 346   
 347          // cleanup
 348          unset($vars['loops']['.']);
 349   
 350          return $vars;
 351      }
 352   
 353      /**
 354      * {@inheritdoc}
 355      */
 356      public function get_source_file_for_handle($handle)
 357      {
 358          return $this->twig->getLoader()->getCacheKey($this->get_filename_from_handle($handle));
 359      }
 360  }
 361