[ Index ]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1