diff options
author | Tristan Darricau <github@nicofuma.fr> | 2014-08-02 19:09:42 +0200 |
---|---|---|
committer | Tristan Darricau <github@nicofuma.fr> | 2014-08-09 16:13:22 +0200 |
commit | 192b8e2568f499aa2ead98a24d8dd5541b1f315a (patch) | |
tree | 0371c204bba34dd9eb480292de6144dbc021fa50 | |
parent | 56ca276dcf080972ad2c5ae411b37c392ffed24d (diff) | |
download | forums-192b8e2568f499aa2ead98a24d8dd5541b1f315a.tar forums-192b8e2568f499aa2ead98a24d8dd5541b1f315a.tar.gz forums-192b8e2568f499aa2ead98a24d8dd5541b1f315a.tar.bz2 forums-192b8e2568f499aa2ead98a24d8dd5541b1f315a.tar.xz forums-192b8e2568f499aa2ead98a24d8dd5541b1f315a.zip |
[ticket/11649] Expose Twig through the container
PHPBB3-11649
-rw-r--r-- | phpBB/config/services.yml | 4 | ||||
-rw-r--r-- | phpBB/config/twig.yml | 36 | ||||
-rw-r--r-- | phpBB/phpbb/template/twig/environment.php | 30 | ||||
-rw-r--r-- | phpBB/phpbb/template/twig/twig.php | 39 | ||||
-rw-r--r-- | tests/controller/helper_route_test.php | 23 | ||||
-rw-r--r-- | tests/extension/metadata_manager_test.php | 38 | ||||
-rw-r--r-- | tests/template/template_events_test.php | 23 | ||||
-rw-r--r-- | tests/template/template_test_case.php | 22 | ||||
-rw-r--r-- | tests/template/template_test_case_with_tree.php | 22 |
9 files changed, 191 insertions, 46 deletions
diff --git a/phpBB/config/services.yml b/phpBB/config/services.yml index a9f9f5ed19..8caf1af683 100644 --- a/phpBB/config/services.yml +++ b/phpBB/config/services.yml @@ -10,6 +10,7 @@ imports: - { resource: mimetype_guessers.yml } - { resource: passwords.yml } - { resource: profilefields.yml } + - { resource: twig.yml } services: acl.permissions: @@ -339,6 +340,9 @@ services: - @config - @user - @template_context + - @template.twig.environment + - %core.template.cache_path% + - @template.twig.extensions.collection - @ext.manager template_context: diff --git a/phpBB/config/twig.yml b/phpBB/config/twig.yml new file mode 100644 index 0000000000..eb3e6c0924 --- /dev/null +++ b/phpBB/config/twig.yml @@ -0,0 +1,36 @@ +parameters: + core.template.cache_path: %core.root_path%cache/twig/ + +services: + template.twig.environment: + class: phpbb\template\twig\environment + arguments: + - @config + - @path_helper + - @service_container + - %core.template.cache_path% + - @ext.manager + - @template.twig.loader + + template.twig.lexer: + class: phpbb\template\twig\lexer + arguments: + - @template.twig.environment + + template.twig.loader: + class: phpbb\template\twig\loader + + template.twig.extensions.collection: + class: phpbb\di\service_collection + arguments: + - @service_container + tags: + - { name: service_collection, tag: twig.extension } + + template.twig.extensions.phpbb: + class: phpbb\template\twig\extension + arguments: + - @template_context + - @user + tags: + - { name: twig.extension } diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index 476ffd935e..df586af9e5 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -13,6 +13,9 @@ namespace phpbb\template\twig; +use Twig_Lexer; +use Twig_LexerInterface; + class environment extends \Twig_Environment { /** @var \phpbb\config\config */ @@ -21,6 +24,9 @@ class environment extends \Twig_Environment /** @var \phpbb\path_helper */ protected $phpbb_path_helper; + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + /** @var \phpbb\extension\manager */ protected $extension_manager; @@ -38,24 +44,46 @@ class environment extends \Twig_Environment * * @param \phpbb\config\config $phpbb_config The phpBB configuration * @param \phpbb\path_helper $path_helper phpBB path helper + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \phpbb\extension\manager $extension_manager phpBB extension manager * @param \Twig_LoaderInterface $loader Twig loader interface * @param array $options Array of options to pass to Twig */ - public function __construct($phpbb_config, \phpbb\path_helper $path_helper, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array()) + public function __construct($phpbb_config, \phpbb\path_helper $path_helper, \Symfony\Component\DependencyInjection\ContainerInterface $container, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array()) { $this->phpbb_config = $phpbb_config; $this->phpbb_path_helper = $path_helper; $this->extension_manager = $extension_manager; + $this->container = $container; $this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path(); $this->web_root_path = $this->phpbb_path_helper->get_web_root_path(); + $options = array_merge(array( + 'cache' => (defined('IN_INSTALL')) ? false : $cache_path, + 'debug' => defined('DEBUG'), + 'auto_reload' => (bool) $this->phpbb_config['load_tplcompile'], + 'autoescape' => false, + ), $options); + return parent::__construct($loader, $options); } /** + * {@inheritdoc} + */ + public function getLexer() + { + if (null === $this->lexer) { + $this->lexer = $this->container->get('template.twig.lexer'); + } + + return $this->lexer; + } + + + /** * Get the list of enabled phpBB extensions * * Used in EVENT node diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index 5e2057f818..dd6117819a 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -78,9 +78,12 @@ class twig extends \phpbb\template\base * @param \phpbb\config\config $config * @param \phpbb\user $user * @param \phpbb\template\context $context template context + * @param \phpbb\template\twig\environment $twig_environment + * @param string $cache_path + * @param array|\ArrayAccess $extensions * @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, $user, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, $extensions = array(), \phpbb\extension\manager $extension_manager = null) { $this->path_helper = $path_helper; $this->phpbb_root_path = $path_helper->get_phpbb_root_path(); @@ -89,35 +92,13 @@ class twig extends \phpbb\template\base $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/')) diff --git a/tests/controller/helper_route_test.php b/tests/controller/helper_route_test.php index 206c3a4f0b..8113bd0018 100644 --- a/tests/controller/helper_route_test.php +++ b/tests/controller/helper_route_test.php @@ -31,7 +31,28 @@ class phpbb_controller_helper_route_test extends phpbb_test_case $phpEx ); $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); - $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $this->user, new \phpbb\template\context()); + + $container = new phpbb_mock_container_builder(); + $cache_path = $phpbb_root_path . 'cache/twig'; + $context = new \phpbb\template\context(); + $loader = new \phpbb\template\twig\loader(''); + $twig = new \phpbb\template\twig\environment( + $this->config, + $phpbb_path_helper, + $container, + $cache_path, + null, + $loader, + array( + 'cache' => false, + 'debug' => false, + 'auto_reload' => true, + 'autoescape' => false, + ) + ); + $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))); + $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); + $this->extension_manager = new phpbb_mock_extension_manager( dirname(__FILE__) . '/', array( diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 535e4fe0d5..fdb9deda53 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -47,20 +47,35 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $this->user = new \phpbb\user(); $this->table_prefix = 'phpbb_'; - $this->template = new \phpbb\template\twig\twig( - new \phpbb\path_helper( - new \phpbb\symfony_request( - new phpbb_mock_request() - ), - new \phpbb\filesystem(), - $this->getMock('\phpbb\request\request'), - $this->phpbb_root_path, - $this->phpEx + $container = new phpbb_mock_container_builder(); + $cache_path = $this->phpbb_root_path . 'cache/twig'; + $context = new \phpbb\template\context(); + $loader = new \phpbb\template\twig\loader(''); + $phpbb_path_helper =new \phpbb\path_helper( + new \phpbb\symfony_request( + new phpbb_mock_request() ), + new \phpbb\filesystem(), + $this->getMock('\phpbb\request\request'), + $this->phpbb_root_path, + $this->phpEx + ); + $twig = new \phpbb\template\twig\environment( $this->config, - $this->user, - new \phpbb\template\context() + $phpbb_path_helper, + $container, + $cache_path, + null, + $loader, + array( + 'cache' => false, + 'debug' => false, + 'auto_reload' => true, + 'autoescape' => false, + ) ); + $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))); + $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $this->migrator = new \phpbb\db\migrator( $this->config, @@ -73,7 +88,6 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case array(), new \phpbb\db\migration\helper() ); - $container = new phpbb_mock_container_builder(); $container->set('migrator', $this->migrator); $this->extension_manager = new \phpbb\extension\manager( diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index c415c969fe..e8ffea8d11 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -147,7 +147,28 @@ Zeta test event in all', $phpbb_root_path, $phpEx ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $user, new \phpbb\template\context, $this->extension_manager); + + $container = new phpbb_mock_container_builder(); + $cache_path = $phpbb_root_path . 'cache/twig'; + $context = new \phpbb\template\context(); + $loader = new \phpbb\template\twig\loader(''); + $twig = new \phpbb\template\twig\environment( + $config, + $path_helper, + $container, + $cache_path, + $this->extension_manager, + $loader, + array( + 'cache' => false, + 'debug' => false, + 'auto_reload' => true, + '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); + $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)); } } diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 83446b5352..1f46d6ff71 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -78,7 +78,27 @@ class phpbb_template_template_test_case extends phpbb_test_case ); $this->template_path = $this->test_path . '/templates'; - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $this->user, new \phpbb\template\context()); + + $container = new phpbb_mock_container_builder(); + $cache_path = $phpbb_root_path . 'cache/twig'; + $context = new \phpbb\template\context(); + $loader = new \phpbb\template\twig\loader(''); + $twig = new \phpbb\template\twig\environment( + $config, + $path_helper, + $container, + $cache_path, + null, + $loader, + array( + 'cache' => false, + 'debug' => false, + 'auto_reload' => true, + '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))); + $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', $this->template_path); } diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index 68ecc4b706..c634e2051a 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -34,7 +34,27 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $this->template_path = $this->test_path . '/templates'; $this->parent_template_path = $this->test_path . '/parent_templates'; - $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $user, new phpbb\template\context()); + + $container = new phpbb_mock_container_builder(); + $cache_path = $phpbb_root_path . 'cache/twig'; + $context = new \phpbb\template\context(); + $loader = new \phpbb\template\twig\loader(''); + $twig = new \phpbb\template\twig\environment( + $config, + $this->phpbb_path_helper, + $container, + $cache_path, + null, + $loader, + array( + 'cache' => false, + 'debug' => false, + 'auto_reload' => true, + '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))); + $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)); } } |