From 633bbc9c6d42785233ea10165a081c7c32795d1c Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 11 Jan 2017 22:50:50 +0700 Subject: [PATCH 1/3] [ticket/14990] Add core events to the Twig environment PHPBB3-14990 --- .../default/container/services_twig.yml | 1 + phpBB/phpbb/template/twig/environment.php | 32 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index 3ca6d62c07..132b1a3df8 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -12,6 +12,7 @@ services: - '@ext.manager' - '@template.twig.loader' - [] + - '@dispatcher' calls: - [setLexer, ['@template.twig.lexer']] diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index 179412a2e3..b2b4f990dd 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -32,6 +32,11 @@ class environment extends \Twig_Environment /** @var \phpbb\extension\manager */ protected $extension_manager; + /** + * @var \phpbb\event\dispatcher_interface + */ + protected $phpbb_dispatcher; + /** @var string */ protected $phpbb_root_path; @@ -54,14 +59,16 @@ class environment extends \Twig_Environment * @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 + * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object */ - public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array()) + public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array(), \phpbb\event\dispatcher_interface $phpbb_dispatcher = null) { $this->phpbb_config = $phpbb_config; $this->filesystem = $filesystem; $this->phpbb_path_helper = $path_helper; $this->extension_manager = $extension_manager; + $this->phpbb_dispatcher = $phpbb_dispatcher; $this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path(); $this->web_root_path = $this->phpbb_path_helper->get_web_root_path(); @@ -202,8 +209,31 @@ class environment extends \Twig_Environment $context['definition']->set('STYLESHEETS', '__STYLESHEETS_' . $placeholder_salt . '__'); } + /** + * Allow changing the template output stream before rendering + * + * @event core.twig_environment_render_template_before + * @var array $context Array with template variables + * @var string $name The template name + * @since 3.2.1-RC1 + */ + $vars = array('context', 'name'); + extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_before', compact($vars))); + $output = parent::render($name, $context); + /** + * Allow changing the template output stream after rendering + * + * @event core.twig_environment_render_template_before + * @var array $context Array with template variables + * @var string $name The template name + * @var string $output Rendered template output stream + * @since 3.2.1-RC1 + */ + $vars = array('context', 'name', 'output'); + extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_after', compact($vars))); + return $this->inject_assets($output, $placeholder_salt); } From 1ea114ca20bd4613420284d7bfc4c92ab0a817b4 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 11 Jan 2017 23:53:12 +0700 Subject: [PATCH 2/3] [ticket/14990] Fix event name, email parsing, installer and dispatcher calls PHPBB3-14990 --- phpBB/config/installer/container/services.yml | 1 + phpBB/includes/functions_messenger.php | 6 ++-- phpBB/phpbb/template/twig/environment.php | 32 +++++++++++-------- tests/email/email_parsing_test.php | 3 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/phpBB/config/installer/container/services.yml b/phpBB/config/installer/container/services.yml index c16547c649..7bab8f7835 100644 --- a/phpBB/config/installer/container/services.yml +++ b/phpBB/config/installer/container/services.yml @@ -85,6 +85,7 @@ services: - null - '@template.twig.loader' - [] + - null calls: - [setLexer, ['@template.twig.lexer']] diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 757a49003b..983cc91688 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -656,7 +656,7 @@ class messenger */ protected function setup_template() { - global $phpbb_container; + global $phpbb_container, $phpbb_dispatcher; if ($this->template instanceof \phpbb\template\template) { @@ -671,7 +671,9 @@ class messenger $phpbb_container->get('ext.manager'), new \phpbb\template\twig\loader( $phpbb_container->get('filesystem') - ) + ), + array(), + $phpbb_dispatcher ); $template_environment->setLexer($phpbb_container->get('template.twig.lexer')); diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index b2b4f990dd..d9e84b042e 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -32,9 +32,7 @@ class environment extends \Twig_Environment /** @var \phpbb\extension\manager */ protected $extension_manager; - /** - * @var \phpbb\event\dispatcher_interface - */ + /** @var \phpbb\event\dispatcher_interface */ protected $phpbb_dispatcher; /** @var string */ @@ -61,7 +59,7 @@ class environment extends \Twig_Environment * @param array $options Array of options to pass to Twig * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object */ - public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array(), \phpbb\event\dispatcher_interface $phpbb_dispatcher = null) + public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array(), \phpbb\event\dispatcher_interface $phpbb_dispatcher = null) { $this->phpbb_config = $phpbb_config; @@ -213,26 +211,32 @@ class environment extends \Twig_Environment * Allow changing the template output stream before rendering * * @event core.twig_environment_render_template_before - * @var array $context Array with template variables - * @var string $name The template name + * @var array context Array with template variables + * @var string name The template name * @since 3.2.1-RC1 */ - $vars = array('context', 'name'); - extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_before', compact($vars))); + if ($this->phpbb_dispatcher) + { + $vars = array('context', 'name'); + extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_before', compact($vars))); + } $output = parent::render($name, $context); /** * Allow changing the template output stream after rendering * - * @event core.twig_environment_render_template_before - * @var array $context Array with template variables - * @var string $name The template name - * @var string $output Rendered template output stream + * @event core.twig_environment_render_template_after + * @var array context Array with template variables + * @var string name The template name + * @var string output Rendered template output stream * @since 3.2.1-RC1 */ - $vars = array('context', 'name', 'output'); - extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_after', compact($vars))); + if ($this->phpbb_dispatcher) + { + $vars = array('context', 'name', 'output'); + extract($this->phpbb_dispatcher->trigger_event('core.twig_environment_render_template_after', compact($vars))); + } return $this->inject_assets($output, $placeholder_salt); } diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index 351a38514f..a8366cf076 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -86,7 +86,8 @@ class phpbb_email_parsing_test extends phpbb_test_case 'debug' => false, 'auto_reload' => true, 'autoescape' => false, - ) + ), + new \phpbb\event\dispatcher($phpbb_container) ); $twig->addExtension($twig_extension); $phpbb_container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); From fcc8e155ec309669bebbf6e0370cecfe64c95193 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 16 Apr 2017 20:53:59 +0700 Subject: [PATCH 3/3] [ticket/14990] Move dispatcher object to the front of the options array PHPBB3-14990 --- phpBB/config/default/container/services_twig.yml | 2 +- phpBB/config/installer/container/services.yml | 2 +- phpBB/includes/functions_messenger.php | 4 ++-- phpBB/phpbb/template/twig/environment.php | 4 ++-- tests/controller/common_helper_route.php | 1 + tests/email/email_parsing_test.php | 4 ++-- tests/extension/metadata_manager_test.php | 3 +-- tests/template/template_allfolder_test.php | 1 + tests/template/template_events_test.php | 1 + tests/template/template_includecss_test.php | 1 + tests/template/template_test_case.php | 1 + tests/template/template_test_case_with_tree.php | 1 + 12 files changed, 15 insertions(+), 10 deletions(-) diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index 132b1a3df8..a9b5b6d4cd 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -11,8 +11,8 @@ services: - '%core.template.cache_path%' - '@ext.manager' - '@template.twig.loader' - - [] - '@dispatcher' + - [] calls: - [setLexer, ['@template.twig.lexer']] diff --git a/phpBB/config/installer/container/services.yml b/phpBB/config/installer/container/services.yml index 7bab8f7835..7203c0ab10 100644 --- a/phpBB/config/installer/container/services.yml +++ b/phpBB/config/installer/container/services.yml @@ -84,8 +84,8 @@ services: - '%core.template.cache_path%' - null - '@template.twig.loader' - - [] - null + - [] calls: - [setLexer, ['@template.twig.lexer']] diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 983cc91688..802246c1c5 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -672,8 +672,8 @@ class messenger new \phpbb\template\twig\loader( $phpbb_container->get('filesystem') ), - array(), - $phpbb_dispatcher + $phpbb_dispatcher, + array() ); $template_environment->setLexer($phpbb_container->get('template.twig.lexer')); diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index d9e84b042e..ac4b16e457 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -56,10 +56,10 @@ class environment extends \Twig_Environment * @param string $cache_path The path to the cache directory * @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 * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object + * @param array $options Array of options to pass to Twig */ - public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array(), \phpbb\event\dispatcher_interface $phpbb_dispatcher = null) + public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, \phpbb\event\dispatcher_interface $phpbb_dispatcher = null, $options = array()) { $this->phpbb_config = $phpbb_config; diff --git a/tests/controller/common_helper_route.php b/tests/controller/common_helper_route.php index 367c15a667..ea2bc042b1 100644 --- a/tests/controller/common_helper_route.php +++ b/tests/controller/common_helper_route.php @@ -114,6 +114,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case $cache_path, null, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false, diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index a8366cf076..8fdfe3035e 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -81,13 +81,13 @@ class phpbb_email_parsing_test extends phpbb_test_case $cache_path, null, new \phpbb\template\twig\loader($filesystem, ''), + new \phpbb\event\dispatcher($phpbb_container), array( 'cache' => false, 'debug' => false, 'auto_reload' => true, 'autoescape' => false, - ), - new \phpbb\event\dispatcher($phpbb_container) + ) ); $twig->addExtension($twig_extension); $phpbb_container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 8ef41f3673..533da68c57 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -70,6 +70,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $cache_path, null, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false, @@ -78,8 +79,6 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case ) ); - $container = new phpbb_mock_container_builder(); - $this->migrator = new \phpbb\db\migrator( $container, $this->config, diff --git a/tests/template/template_allfolder_test.php b/tests/template/template_allfolder_test.php index 9a0a42fabd..63a6ef08ea 100644 --- a/tests/template/template_allfolder_test.php +++ b/tests/template/template_allfolder_test.php @@ -67,6 +67,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case $cache_path, $this->extension_manager, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false, diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index e245c90aee..3a93c91e11 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -161,6 +161,7 @@ Zeta test event in all', $cache_path, $this->extension_manager, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false, diff --git a/tests/template/template_includecss_test.php b/tests/template/template_includecss_test.php index 764652c9c2..bc871aa612 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -53,6 +53,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $cache_path, null, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false, diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index e52d3b76dd..8adbafb1b2 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -105,6 +105,7 @@ class phpbb_template_template_test_case extends phpbb_test_case $cache_path, null, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false, diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index ff35d16120..75e3918f44 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -48,6 +48,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $cache_path, null, $loader, + new \phpbb\event\dispatcher($container), array( 'cache' => false, 'debug' => false,