[ticket/16243] Update template paths

PHPBB3-16243
This commit is contained in:
mrgoldy 2019-12-03 14:28:37 +01:00 committed by Marc Alexander
parent c1408ecf63
commit 44805c976e
No known key found for this signature in database
GPG key ID: 50E0D2423696F995

View file

@ -21,71 +21,59 @@ use phpbb\template\exception\user_object_not_available;
class twig extends \phpbb\template\base class twig extends \phpbb\template\base
{ {
/** /**
* Path of the cache directory for the template * Path of the cache directory for the template
* * Cannot be changed during runtime.
* Cannot be changed during runtime. *
* * @var string
* @var string */
*/
private $cachepath = ''; private $cachepath = '';
/** /** @var \phpbb\path_helper */
* phpBB path helper
* @var \phpbb\path_helper
*/
protected $path_helper; protected $path_helper;
/** /** @var string phpBB root path */
* phpBB root path
* @var string
*/
protected $phpbb_root_path; protected $phpbb_root_path;
/** /** @var string php File extension */
* PHP file extension
* @var string
*/
protected $php_ext; protected $php_ext;
/** /** @var \phpbb\config\config */
* phpBB config instance
* @var \phpbb\config\config
*/
protected $config; protected $config;
/** /** @var \phpbb\user */
* Current user
* @var \phpbb\user
*/
protected $user; protected $user;
/** /** @var \phpbb\extension\manager */
* Extension manager.
*
* @var \phpbb\extension\manager
*/
protected $extension_manager; protected $extension_manager;
/** /** @var environment */
* Twig Environment
*
* @var \Twig\Environment
*/
protected $twig; protected $twig;
/** @var loader */
protected $loader;
/** /**
* Constructor. * Constructor.
* *
* @param \phpbb\path_helper $path_helper * @param \phpbb\path_helper $path_helper Path helper object
* @param \phpbb\config\config $config * @param \phpbb\config\config $config Config object
* @param \phpbb\template\context $context template context * @param \phpbb\template\context $context Template context
* @param \phpbb\template\twig\environment $twig_environment * @param environment $twig_environment Twig environment
* @param string $cache_path * @param string $cache_path Template's cache directory path
* @param \phpbb\user|null $user * @param null|\phpbb\user $user User object
* @param array|\ArrayAccess $extensions * @param array|\ArrayAccess $extensions Template extensions
* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked * @param null|\phpbb\extension\manager $extension_manager If null then template events will not be invoked
*/ */
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) public function __construct(
\phpbb\path_helper $path_helper,
\phpbb\config\config $config,
\phpbb\template\context $context,
environment $twig_environment,
$cache_path,
\phpbb\user $user = null,
$extensions = [],
\phpbb\extension\manager $extension_manager = null
)
{ {
$this->path_helper = $path_helper; $this->path_helper = $path_helper;
$this->phpbb_root_path = $path_helper->get_phpbb_root_path(); $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
@ -96,6 +84,7 @@ class twig extends \phpbb\template\base
$this->extension_manager = $extension_manager; $this->extension_manager = $extension_manager;
$this->cachepath = $cache_path; $this->cachepath = $cache_path;
$this->twig = $twig_environment; $this->twig = $twig_environment;
$this->loader = $twig_environment->getLoader();
foreach ($extensions as $extension) foreach ($extensions as $extension)
{ {
@ -105,7 +94,7 @@ class twig extends \phpbb\template\base
// Add admin namespace // Add admin namespace
if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/')) if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))
{ {
$this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin'); $this->loader->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
} }
} }
@ -138,9 +127,9 @@ class twig extends \phpbb\template\base
throw new user_object_not_available(); throw new user_object_not_available();
} }
$style_list = array( $style_list = [
$this->user->style['style_path'], $this->user->style['style_path'],
); ];
if ($this->user->style['style_parent_id']) if ($this->user->style['style_parent_id'])
{ {
@ -158,55 +147,61 @@ class twig extends \phpbb\template\base
* Default: array('styles') (phpBB's style directory) * Default: array('styles') (phpBB's style directory)
* @return \phpbb\template\template $this * @return \phpbb\template\template $this
*/ */
public function set_style($style_directories = array('styles')) public function set_style($style_directories = ['styles'])
{ {
if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array()) if ($style_directories !== ['styles'] && $this->loader->getPaths('core') === [])
{ {
// We should set up the core styles path since not already setup // We should set up the core styles path since not already setup
$this->set_style(); $this->set_style();
} }
$names = $this->get_user_style(); $paths = [];
// Add 'all' folder to $names array // Add 'all' folder to $names array
// It allows extensions to load a template file from 'all' folder, // It allows extensions to load a template file from 'all' folder,
// if a style doesn't include it. // if a style doesn't include it.
$names = $this->get_user_style();
$names[] = 'all'; $names[] = 'all';
$paths = array();
foreach ($style_directories as $directory) foreach ($style_directories as $directory)
{ {
foreach ($names as $name) foreach ($names as $name)
{ {
$path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/"; $path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
$template_path = $path . 'template/'; $handle = @opendir($path);
$theme_path = $path . 'theme/'; $valid = false;
$is_valid_dir = false; if ($handle)
if (is_dir($template_path))
{ {
$is_valid_dir = true; while (($file = readdir($handle)) !== false)
$paths[] = $template_path; {
} $dir = $path . $file;
if (is_dir($theme_path))
{ if ($file[0] !== '.' && is_dir($dir))
$is_valid_dir = true; {
$paths[] = $theme_path; $paths[] = $dir;
$valid = true;
}
}
closedir($handle);
} }
if ($is_valid_dir) if ($valid)
{ {
// Add the base style directory as a safe directory // Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($path); $this->loader->addSafeDirectory($path);
} }
} }
} }
// If we're setting up the main phpBB styles directory and the core // If we're setting up the main phpBB styles directory and the core
// namespace isn't setup yet, we will set it up now // namespace isn't setup yet, we will set it up now
if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array()) if ($style_directories === ['styles'] && $this->loader->getPaths('core') === [])
{ {
// Set up the core style paths namespace // Set up the core style paths namespace
$this->twig->getLoader()->setPaths($paths, 'core'); $this->loader->setPaths($paths, 'core');
} }
$this->set_custom_style($names, $paths); $this->set_custom_style($names, $paths);
@ -229,11 +224,11 @@ class twig extends \phpbb\template\base
*/ */
public function set_custom_style($names, $paths) public function set_custom_style($names, $paths)
{ {
$paths = (is_string($paths)) ? array($paths) : $paths; $paths = (is_string($paths)) ? [$paths] : $paths;
$names = (is_string($names)) ? array($names) : $names; $names = (is_string($names)) ? [$names] : $names;
// Set as __main__ namespace // Set as __main__ namespace
$this->twig->getLoader()->setPaths($paths); $this->loader->setPaths($paths);
// Add all namespaces for all extensions // Add all namespaces for all extensions
if ($this->extension_manager instanceof \phpbb\extension\manager) if ($this->extension_manager instanceof \phpbb\extension\manager)
@ -244,7 +239,7 @@ class twig extends \phpbb\template\base
{ {
// namespaces cannot contain / // namespaces cannot contain /
$namespace = str_replace('/', '_', $ext_namespace); $namespace = str_replace('/', '_', $ext_namespace);
$paths = array(); $paths = [];
foreach ($names as $template_dir) foreach ($names as $template_dir)
{ {
@ -285,11 +280,11 @@ class twig extends \phpbb\template\base
if ($is_valid_dir) if ($is_valid_dir)
{ {
// Add the base style directory as a safe directory // Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($ext_style_path); $this->loader->addSafeDirectory($ext_style_path);
} }
} }
$this->twig->getLoader()->setPaths($paths, $namespace); $this->loader->setPaths($paths, $namespace);
} }
} }
@ -345,10 +340,10 @@ class twig extends \phpbb\template\base
$vars = array_merge( $vars = array_merge(
$context_vars['.'][0], // To get normal vars $context_vars['.'][0], // To get normal vars
array( [
'definition' => new \phpbb\template\twig\definition(), 'definition' => new \phpbb\template\twig\definition(),
'loops' => $context_vars, // To get loops 'loops' => $context_vars, // To get loops
) ]
); );
if ($this->user instanceof \phpbb\user) if ($this->user instanceof \phpbb\user)
@ -373,6 +368,6 @@ class twig extends \phpbb\template\base
*/ */
public function get_source_file_for_handle($handle) public function get_source_file_for_handle($handle)
{ {
return $this->twig->getLoader()->getCacheKey($this->get_filename_from_handle($handle)); return $this->loader->getCacheKey($this->get_filename_from_handle($handle));
} }
} }