phpBB

Code Changes

File: phpbb/template/twig/twig.php

  Unmodified   Added   Modified   Removed
Line 12Line 12
*/

namespace phpbb\template\twig;

*/

namespace phpbb\template\twig;

 

use phpbb\template\exception\user_object_not_available;


/**
* Twig Template class.


/**
* Twig Template class.

Line 76Line 78
	*
* @param \phpbb\path_helper $path_helper
* @param \phpbb\config\config $config

	*
* @param \phpbb\path_helper $path_helper
* @param \phpbb\config\config $config

	* @param \phpbb\user $user

 
	* @param \phpbb\template\context $context template context

	* @param \phpbb\template\context $context template context

 
	* @param \phpbb\template\twig\environment $twig_environment
* @param string $cache_path
* @param \phpbb\user|null $user
* @param array|\ArrayAccess $extensions

	* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
*/

	* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
*/

	public function __construct(\phpbb\path_helper $path_helper, $config, $user, \phpbb\template\context $context, \phpbb\extension\manager $extension_manager = null)

	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)

	{
$this->path_helper = $path_helper;
$this->phpbb_root_path = $path_helper->get_phpbb_root_path();

	{
$this->path_helper = $path_helper;
$this->phpbb_root_path = $path_helper->get_phpbb_root_path();

Line 89Line 94
		$this->user = $user;
$this->context = $context;
$this->extension_manager = $extension_manager;

		$this->user = $user;
$this->context = $context;
$this->extension_manager = $extension_manager;

 
		$this->cachepath = $cache_path;
$this->twig = $twig_environment;





		$this->cachepath = $this->phpbb_root_path . 'cache/twig/';

// Initiate the loader, __main__ namespace paths will be setup later in set_style_names()
$loader = new \phpbb\template\twig\loader('');

$this->twig = new \phpbb\template\twig\environment(
$this->config,
$this->path_helper,
$this->extension_manager,
$loader,
array(
'cache' => (defined('IN_INSTALL')) ? false : $this->cachepath,
'debug' => defined('DEBUG'),
'auto_reload' => (bool) $this->config['load_tplcompile'],
'autoescape' => false,
)
);

$this->twig->addExtension(
new \phpbb\template\twig\extension(
$this->context,
$this->user
)
);

$lexer = new \phpbb\template\twig\lexer($this->twig);

$this->twig->setLexer($lexer);

		foreach ($extensions as $extension)
{
$this->twig->addExtension($extension);
}


























// 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/'))


// 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/'))

Line 145Line 128
	* Get the style tree of the style preferred by the current user
*
* @return array Style tree, most specific first

	* Get the style tree of the style preferred by the current user
*
* @return array Style tree, most specific first

 
	*
* @throws \phpbb\template\exception\user_object_not_available When user service was not set

	*/
public function get_user_style()
{

	*/
public function get_user_style()
{

 
		if ($this->user === null)
{
throw new user_object_not_available();
}


		$style_list = array(
$this->user->style['style_path'],
);

		$style_list = array(
$this->user->style['style_path'],
);

Line 189Line 179
			{
$path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
$template_path = $path . 'template/';

			{
$path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
$template_path = $path . 'template/';

 
				$theme_path = $path . 'theme/';





 
				$is_valid_dir = false;

				if (is_dir($template_path))

				if (is_dir($template_path))

 
				{
$is_valid_dir = true;
$paths[] = $template_path;
}
if (is_dir($theme_path))
{
$is_valid_dir = true;
$paths[] = $theme_path;
}

if ($is_valid_dir)

				{
// Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($path);

				{
// Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($path);


$paths[] = $template_path;

 
				}
}
}

				}
}
}

Line 253Line 254
						{
$ext_style_template_path = $ext_path . $template_dir['ext_path'];
$ext_style_path = dirname($ext_style_template_path);

						{
$ext_style_template_path = $ext_path . $template_dir['ext_path'];
$ext_style_path = dirname($ext_style_template_path);

 
							$ext_style_theme_path = $ext_style_path . 'theme/';

						}
else
{
$ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
$ext_style_template_path = $ext_style_path . 'template/';

						}
else
{
$ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
$ext_style_template_path = $ext_style_path . 'template/';

 
							$ext_style_theme_path = $ext_style_path . 'theme/';

						}
}
else
{
$ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
$ext_style_template_path = $ext_style_path . 'template/';

						}
}
else
{
$ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
$ext_style_template_path = $ext_style_path . 'template/';

 
						$ext_style_theme_path = $ext_style_path . 'theme/';

					}


					}


 
					$is_valid_dir = false;

					if (is_dir($ext_style_template_path))

					if (is_dir($ext_style_template_path))

 
					{
$is_valid_dir = true;
$paths[] = $ext_style_template_path;
}
if (is_dir($ext_style_theme_path))
{
$is_valid_dir = true;
$paths[] = $ext_style_theme_path;
}

if ($is_valid_dir)

					{
// Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($ext_style_path);

					{
// Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($ext_style_path);


$paths[] = $ext_style_template_path;

 
					}
}


					}
}


Line 321Line 335
			return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
}


			return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
}


		$this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle, $this->get_template_vars())));

		$this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars()));


return $this;
}


return $this;
}

Line 339Line 353
			$context_vars['.'][0], // To get normal vars
array(
'definition' => new \phpbb\template\twig\definition(),

			$context_vars['.'][0], // To get normal vars
array(
'definition' => new \phpbb\template\twig\definition(),

				'user'			=> $this->user,

 
				'loops'			=> $context_vars, // To get loops
)
);

				'loops'			=> $context_vars, // To get loops
)
);

 

if ($this->user instanceof \phpbb\user)
{
$vars['user'] = $this->user;
}


// cleanup
unset($vars['loops']['.']);


// cleanup
unset($vars['loops']['.']);

 

// Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops.
foreach ($vars['loops'] as $key => &$value)
{
$vars[$key] = $value;
}


return $vars;
}


return $vars;
}