[ticket/13804] Make template's user dependency optional

PHPBB3-13804
This commit is contained in:
MateBartus 2015-05-01 19:04:21 +02:00
parent de687f32ed
commit 7b301e22f3
14 changed files with 72 additions and 29 deletions

View file

@ -186,21 +186,6 @@ services:
request_stack: request_stack:
class: Symfony\Component\HttpFoundation\RequestStack class: Symfony\Component\HttpFoundation\RequestStack
template:
class: phpbb\template\twig\twig
arguments:
- @path_helper
- @config
- @user
- @template_context
- @template.twig.environment
- %core.template.cache_path%
- @template.twig.extensions.collection
- @ext.manager
template_context:
class: phpbb\template\context
upload_imagesize: upload_imagesize:
class: fastImageSize\fastImageSize class: fastImageSize\fastImageSize

View file

@ -47,3 +47,18 @@ services:
template.twig.extensions.debug: template.twig.extensions.debug:
class: Twig_Extension_Debug class: Twig_Extension_Debug
template:
class: phpbb\template\twig\twig
arguments:
- @path_helper
- @config
- @template_context
- @template.twig.environment
- %core.template.cache_path%
- @user
- @template.twig.extensions.collection
- @ext.manager
template_context:
class: phpbb\template\context

View file

@ -138,7 +138,6 @@ class bbcode
$template = new \phpbb\template\twig\twig( $template = new \phpbb\template\twig\twig(
$phpbb_container->get('path_helper'), $phpbb_container->get('path_helper'),
$phpbb_container->get('config'), $phpbb_container->get('config'),
$phpbb_container->get('user'),
new \phpbb\template\context(), new \phpbb\template\context(),
new \phpbb\template\twig\environment( new \phpbb\template\twig\environment(
$phpbb_container->get('config'), $phpbb_container->get('config'),
@ -152,6 +151,7 @@ class bbcode
) )
), ),
$phpbb_container->getParameter('core.root_path') . 'cache/', $phpbb_container->getParameter('core.root_path') . 'cache/',
$phpbb_container->get('user'),
$phpbb_container->get('template.twig.extensions.collection'), $phpbb_container->get('template.twig.extensions.collection'),
$phpbb_extension_manager $phpbb_extension_manager
); );

View file

@ -633,7 +633,6 @@ class messenger
$this->template = new \phpbb\template\twig\twig( $this->template = new \phpbb\template\twig\twig(
$phpbb_container->get('path_helper'), $phpbb_container->get('path_helper'),
$phpbb_container->get('config'), $phpbb_container->get('config'),
$phpbb_container->get('user'),
new \phpbb\template\context(), new \phpbb\template\context(),
new \phpbb\template\twig\environment( new \phpbb\template\twig\environment(
$phpbb_container->get('config'), $phpbb_container->get('config'),
@ -647,6 +646,7 @@ class messenger
) )
), ),
$phpbb_container->getParameter('core.root_path') . 'cache/', $phpbb_container->getParameter('core.root_path') . 'cache/',
$phpbb_container->get('user'),
$phpbb_container->get('template.twig.extensions.collection'), $phpbb_container->get('template.twig.extensions.collection'),
$phpbb_extension_manager $phpbb_extension_manager
); );

View file

@ -138,6 +138,7 @@ $phpbb_container_builder->set_custom_parameters(array(
$phpbb_container = $phpbb_container_builder->get_container(); $phpbb_container = $phpbb_container_builder->get_container();
$phpbb_container->register('dbal.conn.driver')->setSynthetic(true); $phpbb_container->register('dbal.conn.driver')->setSynthetic(true);
$phpbb_container->register('template.twig.environment')->setSynthetic(true); $phpbb_container->register('template.twig.environment')->setSynthetic(true);
$phpbb_container->register('language.loader')->setSynthetic(true);
$phpbb_container->compile(); $phpbb_container->compile();
$phpbb_class_loader->set_cache($phpbb_container->get('cache.driver')); $phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));
@ -288,16 +289,18 @@ $twig_environment = new \phpbb\template\twig\environment(
$phpbb_container->get('template.twig.loader') $phpbb_container->get('template.twig.loader')
); );
$language_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
$phpbb_container->set('template.twig.environment', $twig_environment); $phpbb_container->set('template.twig.environment', $twig_environment);
$phpbb_container->set('language.loader', $language_loader);
$twig_context = new \phpbb\template\context(); $twig_context = new \phpbb\template\context();
$template = new \phpbb\template\twig\twig( $template = new \phpbb\template\twig\twig(
$phpbb_path_helper, $phpbb_path_helper,
$config, $config,
$user,
$twig_context, $twig_context,
$twig_environment, $twig_environment,
$cache_path, $cache_path,
array(new \phpbb\template\twig\extension($twig_context, $user)) $user,
array($phpbb_container->get('template.twig.extensions.phpbb'))
); );
$paths = array($phpbb_root_path . 'install/update/new/adm/style', $phpbb_admin_path . 'style'); $paths = array($phpbb_root_path . 'install/update/new/adm/style', $phpbb_admin_path . 'style');

View file

@ -0,0 +1,22 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\exception;
/**
* This exception is thrown when the user object was not set but it is required by the called method
*/
class user_object_not_available extends \phpbb\exception\runtime_exception
{
}

View file

@ -13,6 +13,8 @@
namespace phpbb\template\twig; namespace phpbb\template\twig;
use phpbb\template\exception\user_object_not_available;
/** /**
* Twig Template class. * Twig Template class.
*/ */
@ -76,14 +78,14 @@ class twig extends \phpbb\template\base
* *
* @param \phpbb\path_helper $path_helper * @param \phpbb\path_helper $path_helper
* @param \phpbb\config\config $config * @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 \phpbb\template\twig\environment $twig_environment
* @param string $cache_path * @param string $cache_path
* @param \phpbb\user|null $user
* @param array|\ArrayAccess $extensions * @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\template\twig\environment $twig_environment, $cache_path, $extensions = array(), \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->path_helper = $path_helper;
$this->phpbb_root_path = $path_helper->get_phpbb_root_path(); $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
@ -126,9 +128,16 @@ class twig extends \phpbb\template\base
* Get the style tree of the style preferred by the current user * Get the style tree of the style preferred by the current user
* *
* @return array Style tree, most specific first * @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( $style_list = array(
$this->user->style['style_path'], $this->user->style['style_path'],
); );
@ -344,11 +353,20 @@ class twig extends \phpbb\template\base
$context_vars['.'][0], // To get normal vars $context_vars['.'][0], // To get normal vars
array( array(
'definition' => new \phpbb\template\twig\definition(), '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 = array_merge(
$vars,
array(
'user' => $this->user,
)
);
}
// cleanup // cleanup
unset($vars['loops']['.']); unset($vars['loops']['.']);

View file

@ -107,7 +107,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case
'autoescape' => false, 'autoescape' => false,
) )
); );
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user))); $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->extension_manager = new phpbb_mock_extension_manager( $this->extension_manager = new phpbb_mock_extension_manager(

View file

@ -111,7 +111,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
$lang = new \phpbb\language\language($lang_loader); $lang = new \phpbb\language\language($lang_loader);
$this->user = new \phpbb\user($lang, '\phpbb\datetime'); $this->user = new \phpbb\user($lang, '\phpbb\datetime');
$this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user))); $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
} }

View file

@ -75,7 +75,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case
'autoescape' => false, 'autoescape' => false,
) )
); );
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager); $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager);
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template_path = $this->test_path . '/templates'; $this->template_path = $this->test_path . '/templates';

View file

@ -169,7 +169,7 @@ Zeta test event in all',
'autoescape' => false, 'autoescape' => false,
) )
); );
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager); $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager);
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path)); $this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path));

View file

@ -64,10 +64,10 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te
$this->template = new phpbb\template\twig\twig( $this->template = new phpbb\template\twig\twig(
$this->phpbb_path_helper, $this->phpbb_path_helper,
$config, $config,
$user,
$context, $context,
$twig, $twig,
$cache_path, $cache_path,
$this->user,
array(new \phpbb\template\twig\extension($context, $this->user)), array(new \phpbb\template\twig\extension($context, $this->user)),
new phpbb_mock_extension_manager( new phpbb_mock_extension_manager(
dirname(__FILE__) . '/', dirname(__FILE__) . '/',

View file

@ -115,7 +115,7 @@ class phpbb_template_template_test_case extends phpbb_test_case
'autoescape' => false, 'autoescape' => false,
) )
); );
$this->template = new phpbb\template\twig\twig($path_helper, $config, $user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user))); $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style('tests', $this->template_path); $this->template->set_custom_style('tests', $this->template_path);
} }

View file

@ -56,7 +56,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat
'autoescape' => false, 'autoescape' => false,
) )
); );
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user))); $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path));
} }