2013-06-10 12:59:47 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
* @copyright (c) 2013 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
namespace phpbb\template\twig;
|
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
/**
|
|
|
|
* Twig Template class.
|
|
|
|
* @package phpBB3
|
|
|
|
*/
|
2013-09-16 01:24:05 +02:00
|
|
|
class twig extends \phpbb\template\base
|
2013-06-10 12:59:47 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Path of the cache directory for the template
|
2013-07-02 12:22:42 -05:00
|
|
|
*
|
|
|
|
* Cannot be changed during runtime.
|
|
|
|
*
|
2013-06-10 12:59:47 -05:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-02 12:22:42 -05:00
|
|
|
private $cachepath = '';
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-09-13 10:58:03 -05:00
|
|
|
/**
|
2013-09-26 15:34:44 +02:00
|
|
|
* phpBB path helper
|
|
|
|
* @var \phpbb\path_helper
|
2013-09-13 10:58:03 -05:00
|
|
|
*/
|
2013-09-26 15:34:44 +02:00
|
|
|
protected $path_helper;
|
2013-09-13 10:58:03 -05:00
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
/**
|
|
|
|
* phpBB root path
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $phpbb_root_path;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP file extension
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $php_ext;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* phpBB config instance
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\config\config
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $config;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current user
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\user
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $user;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension manager.
|
|
|
|
*
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\extension\manager
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $extension_manager;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Twig Environment
|
|
|
|
*
|
|
|
|
* @var Twig_Environment
|
|
|
|
*/
|
|
|
|
protected $twig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2013-09-26 15:34:44 +02:00
|
|
|
* @param \phpbb\path_helper $path_helper
|
2013-09-10 14:01:09 +02:00
|
|
|
* @param \phpbb\config\config $config
|
|
|
|
* @param \phpbb\user $user
|
|
|
|
* @param \phpbb\template\context $context template context
|
|
|
|
* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-09-26 15:34:44 +02:00
|
|
|
public function __construct(\phpbb\path_helper $path_helper, $config, $user, \phpbb\template\context $context, \phpbb\extension\manager $extension_manager = null)
|
2013-06-10 12:59:47 -05:00
|
|
|
{
|
2013-09-26 15:34:44 +02:00
|
|
|
$this->path_helper = $path_helper;
|
|
|
|
$this->phpbb_root_path = $path_helper->get_phpbb_root_path();
|
|
|
|
$this->php_ext = $path_helper->get_php_ext();
|
2013-06-10 12:59:47 -05:00
|
|
|
$this->config = $config;
|
|
|
|
$this->user = $user;
|
2013-06-11 09:41:15 -05:00
|
|
|
$this->context = $context;
|
2013-06-10 12:59:47 -05:00
|
|
|
$this->extension_manager = $extension_manager;
|
|
|
|
|
2013-09-13 10:58:03 -05:00
|
|
|
$this->cachepath = $this->phpbb_root_path . 'cache/twig/';
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-06-18 10:37:25 -05:00
|
|
|
// Initiate the loader, __main__ namespace paths will be setup later in set_style_names()
|
2013-09-16 01:24:05 +02:00
|
|
|
$loader = new \phpbb\template\twig\loader('');
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
$this->twig = new \phpbb\template\twig\environment(
|
2013-06-25 14:58:55 -05:00
|
|
|
$this->config,
|
2014-01-27 18:51:39 -08:00
|
|
|
$this->extension_manager,
|
2013-09-26 15:34:44 +02:00
|
|
|
$this->path_helper,
|
2013-06-25 14:58:55 -05:00
|
|
|
$loader,
|
|
|
|
array(
|
2013-07-11 11:03:28 -05:00
|
|
|
'cache' => (defined('IN_INSTALL')) ? false : $this->cachepath,
|
2013-07-01 13:32:43 -05:00
|
|
|
'debug' => defined('DEBUG'),
|
2013-07-04 13:19:03 -05:00
|
|
|
'auto_reload' => (bool) $this->config['load_tplcompile'],
|
2013-06-25 14:58:55 -05:00
|
|
|
'autoescape' => false,
|
|
|
|
)
|
|
|
|
);
|
2013-06-24 22:37:58 -05:00
|
|
|
|
2013-07-01 09:32:21 -05:00
|
|
|
$this->twig->addExtension(
|
2013-09-10 14:01:09 +02:00
|
|
|
new \phpbb\template\twig\extension(
|
2013-07-02 12:17:56 -05:00
|
|
|
$this->context,
|
2013-07-01 09:32:21 -05:00
|
|
|
$this->user
|
|
|
|
)
|
|
|
|
);
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
$lexer = new \phpbb\template\twig\lexer($this->twig);
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
$this->twig->setLexer($lexer);
|
2013-07-24 13:04:27 -05:00
|
|
|
|
|
|
|
// Add admin namespace
|
2013-09-26 15:34:44 +02:00
|
|
|
if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))
|
2013-07-24 13:04:27 -05:00
|
|
|
{
|
2013-09-26 15:34:44 +02:00
|
|
|
$this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
|
2013-07-24 13:04:27 -05:00
|
|
|
}
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
2013-06-24 15:28:54 -05:00
|
|
|
/**
|
|
|
|
* Clear the cache
|
|
|
|
*
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return \phpbb\template\template
|
2013-06-24 15:28:54 -05:00
|
|
|
*/
|
|
|
|
public function clear_cache()
|
|
|
|
{
|
|
|
|
if (is_dir($this->cachepath))
|
|
|
|
{
|
|
|
|
$this->twig->clearCacheFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
/**
|
2013-07-24 12:24:35 -05:00
|
|
|
* Get the style tree of the style preferred by the current user
|
2013-06-10 12:59:47 -05:00
|
|
|
*
|
2013-07-24 12:24:35 -05:00
|
|
|
* @return array Style tree, most specific first
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-07-24 12:24:35 -05:00
|
|
|
public function get_user_style()
|
2013-06-10 12:59:47 -05:00
|
|
|
{
|
2013-07-24 12:24:35 -05:00
|
|
|
$style_list = array(
|
|
|
|
$this->user->style['style_path'],
|
|
|
|
);
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-07-24 12:24:35 -05:00
|
|
|
if ($this->user->style['style_parent_id'])
|
|
|
|
{
|
|
|
|
$style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $style_list;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-24 12:24:35 -05:00
|
|
|
* Set style location based on (current) user's chosen style.
|
2013-06-10 12:59:47 -05:00
|
|
|
*
|
2013-07-24 12:24:35 -05:00
|
|
|
* @param array $style_directories The directories to add style paths for
|
|
|
|
* E.g. array('ext/foo/bar/styles', 'styles')
|
|
|
|
* Default: array('styles') (phpBB's style directory)
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return \phpbb\template\template $this
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-07-24 12:24:35 -05:00
|
|
|
public function set_style($style_directories = array('styles'))
|
2013-06-10 12:59:47 -05:00
|
|
|
{
|
2013-07-24 12:42:37 -05:00
|
|
|
if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array())
|
|
|
|
{
|
|
|
|
// We should set up the core styles path since not already setup
|
|
|
|
$this->set_style();
|
|
|
|
}
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-07-24 12:34:22 -05:00
|
|
|
$names = $this->get_user_style();
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-07-24 12:24:35 -05:00
|
|
|
$paths = array();
|
|
|
|
foreach ($style_directories as $directory)
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-07-24 12:34:22 -05:00
|
|
|
foreach ($names as $name)
|
2013-07-24 12:24:35 -05:00
|
|
|
{
|
2013-09-02 11:25:36 -05:00
|
|
|
$path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
|
|
|
|
$template_path = $path . 'template/';
|
2013-07-24 12:24:35 -05:00
|
|
|
|
2013-09-02 11:25:36 -05:00
|
|
|
if (is_dir($template_path))
|
2013-07-24 12:24:35 -05:00
|
|
|
{
|
2013-09-02 11:25:36 -05:00
|
|
|
// Add the base style directory as a safe directory
|
|
|
|
$this->twig->getLoader()->addSafeDirectory($path);
|
|
|
|
|
|
|
|
$paths[] = $template_path;
|
2013-07-24 12:24:35 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 19:23:42 -05:00
|
|
|
}
|
2013-06-18 10:37:25 -05:00
|
|
|
|
2013-07-24 12:42:37 -05:00
|
|
|
// If we're setting up the main phpBB styles directory and the core
|
|
|
|
// namespace isn't setup yet, we will set it up now
|
|
|
|
if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array())
|
2013-06-25 19:23:42 -05:00
|
|
|
{
|
2013-07-24 12:42:37 -05:00
|
|
|
// Set up the core style paths namespace
|
|
|
|
$this->twig->getLoader()->setPaths($paths, 'core');
|
2013-06-25 19:23:42 -05:00
|
|
|
}
|
2013-06-24 15:28:54 -05:00
|
|
|
|
2013-07-24 13:25:20 -05:00
|
|
|
$this->set_custom_style($names, $paths);
|
2013-07-24 12:24:35 -05:00
|
|
|
|
2013-07-24 12:45:35 -05:00
|
|
|
return $this;
|
2013-07-24 12:24:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set custom style location (able to use directory outside of phpBB).
|
|
|
|
*
|
|
|
|
* Note: Templates are still compiled to phpBB's cache directory.
|
|
|
|
*
|
2013-07-24 12:55:41 -05:00
|
|
|
* @param string|array $names Array of names or string of name of template(s) in inheritance tree order, used by extensions.
|
2013-07-24 13:31:09 -05:00
|
|
|
* @param string|array or string $paths Array of style paths, relative to current root directory
|
2013-07-24 12:45:35 -05:00
|
|
|
* @return phpbb_template $this
|
2013-07-24 12:24:35 -05:00
|
|
|
*/
|
2013-07-24 13:25:20 -05:00
|
|
|
public function set_custom_style($names, $paths)
|
2013-07-24 12:24:35 -05:00
|
|
|
{
|
2013-07-24 13:26:55 -05:00
|
|
|
$paths = (is_string($paths)) ? array($paths) : $paths;
|
|
|
|
$names = (is_string($names)) ? array($names) : $names;
|
2013-07-24 12:24:35 -05:00
|
|
|
|
2013-06-18 10:37:25 -05:00
|
|
|
// Set as __main__ namespace
|
2013-07-24 13:25:20 -05:00
|
|
|
$this->twig->getLoader()->setPaths($paths);
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-06-25 19:23:42 -05:00
|
|
|
// Add all namespaces for all extensions
|
2013-09-10 14:01:09 +02:00
|
|
|
if ($this->extension_manager instanceof \phpbb\extension\manager)
|
2013-06-25 19:23:42 -05:00
|
|
|
{
|
2013-07-24 13:19:20 -05:00
|
|
|
$names[] = 'all';
|
2013-06-25 19:23:42 -05:00
|
|
|
|
|
|
|
foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-06-25 19:23:42 -05:00
|
|
|
// namespaces cannot contain /
|
|
|
|
$namespace = str_replace('/', '_', $ext_namespace);
|
|
|
|
$paths = array();
|
2013-06-18 10:37:25 -05:00
|
|
|
|
2013-07-24 13:19:20 -05:00
|
|
|
foreach ($names as $style_name)
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-09-02 11:25:36 -05:00
|
|
|
$ext_style_path = $ext_path . 'styles/' . $style_name . '/';
|
|
|
|
$ext_style_template_path = $ext_style_path . 'template/';
|
2013-06-25 19:23:42 -05:00
|
|
|
|
2013-09-02 11:25:36 -05:00
|
|
|
if (is_dir($ext_style_template_path))
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-09-02 11:25:36 -05:00
|
|
|
// Add the base style directory as a safe directory
|
|
|
|
$this->twig->getLoader()->addSafeDirectory($ext_style_path);
|
|
|
|
|
|
|
|
$paths[] = $ext_style_template_path;
|
2013-06-18 10:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 19:23:42 -05:00
|
|
|
|
|
|
|
$this->twig->getLoader()->setPaths($paths, $namespace);
|
2013-06-18 10:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 01:00:38 -05:00
|
|
|
return $this;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a template for provided handle.
|
|
|
|
*
|
|
|
|
* The template will be loaded and compiled, if necessary, first.
|
|
|
|
*
|
|
|
|
* This function calls hooks.
|
|
|
|
*
|
|
|
|
* @param string $handle Handle to display
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return \phpbb\template\template $this
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
|
|
|
public function display($handle)
|
|
|
|
{
|
|
|
|
$result = $this->call_hook($handle, __FUNCTION__);
|
|
|
|
if ($result !== false)
|
|
|
|
{
|
|
|
|
return $result[0];
|
|
|
|
}
|
|
|
|
|
2013-07-01 20:41:36 -05:00
|
|
|
$this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-07-01 21:44:00 -05:00
|
|
|
return $this;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the handle and assign the output to a template variable
|
|
|
|
* or return the compiled result.
|
|
|
|
*
|
|
|
|
* @param string $handle Handle to operate on
|
|
|
|
* @param string $template_var Template variable to assign compiled handle to
|
|
|
|
* @param bool $return_content If true return compiled handle, otherwise assign to $template_var
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
|
|
|
public function assign_display($handle, $template_var = '', $return_content = true)
|
|
|
|
{
|
|
|
|
if ($return_content)
|
|
|
|
{
|
2013-07-01 21:16:36 -05:00
|
|
|
return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-01 20:41:36 -05:00
|
|
|
$this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle, $this->get_template_vars())));
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-07-01 21:44:00 -05:00
|
|
|
return $this;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
2013-06-11 10:57:00 -05:00
|
|
|
/**
|
|
|
|
* Get template vars in a format Twig will use (from the context)
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-07-24 13:38:12 -05:00
|
|
|
protected function get_template_vars()
|
2013-06-11 09:41:15 -05:00
|
|
|
{
|
2013-07-01 09:36:03 -05:00
|
|
|
$context_vars = $this->context->get_data_ref();
|
2013-06-11 09:41:15 -05:00
|
|
|
|
2013-06-24 22:37:58 -05:00
|
|
|
$vars = array_merge(
|
2013-07-01 09:36:03 -05:00
|
|
|
$context_vars['.'][0], // To get normal vars
|
2013-06-29 11:07:10 -05:00
|
|
|
array(
|
2013-09-10 14:01:09 +02:00
|
|
|
'definition' => new \phpbb\template\twig\definition(),
|
2013-07-01 09:32:21 -05:00
|
|
|
'user' => $this->user,
|
2013-07-14 12:10:49 -05:00
|
|
|
'loops' => $context_vars, // To get loops
|
2013-06-29 11:07:10 -05:00
|
|
|
)
|
2013-06-24 22:37:58 -05:00
|
|
|
);
|
|
|
|
|
2013-06-11 09:41:15 -05:00
|
|
|
// cleanup
|
2013-07-14 12:10:49 -05:00
|
|
|
unset($vars['loops']['.']);
|
2013-06-11 09:41:15 -05:00
|
|
|
|
|
|
|
return $vars;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
2013-07-01 11:52:03 -05:00
|
|
|
|
2013-07-02 11:29:32 -05:00
|
|
|
/**
|
|
|
|
* Get path to template for handle (required for BBCode parser)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_source_file_for_handle($handle)
|
|
|
|
{
|
|
|
|
return $this->twig->getLoader()->getCacheKey($this->get_filename_from_handle($handle));
|
|
|
|
}
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|