From d07aeb00d8b047f0fefd52c9b3da874fad663395 Mon Sep 17 00:00:00 2001 From: toxyy Date: Sun, 3 Dec 2023 16:32:02 -0500 Subject: [PATCH 01/16] [ticket/15214] Add event & functionality for assigning template event priority Event added to allow template events to be assigned priority per extension, event location chosen so that it only fires once. Twig node event class refactored to allow template event priority assignment, compile calls are deferred until all locations are processed per extension namespace. Priority precedence mirrors Symfony priority, with higher numbers being placed at the beginning of the array. Duplicate priority assignment will currently have the later events compiled before the others. PHPBB3-15214 --- .../default/container/services_twig.yml | 1 + phpBB/phpbb/template/twig/extension.php | 9 +++- phpBB/phpbb/template/twig/node/event.php | 49 +++++++++++++++++-- .../phpbb/template/twig/tokenparser/event.php | 28 ++++++++++- 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index e7a7155deb..c10dfc577c 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -40,6 +40,7 @@ services: - '@template_context' - '@template.twig.environment' - '@language' + - '@dispatcher' tags: - { name: twig.extension } diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 982601e41b..9f2bfebb9f 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -28,18 +28,23 @@ class extension extends \Twig\Extension\AbstractExtension /** @var \phpbb\language\language */ protected $language; + /** @var \phpbb\event\dispatcher_interface */ + protected $phpbb_dispatcher; + /** * Constructor * * @param \phpbb\template\context $context * @param \phpbb\template\twig\environment $environment * @param \phpbb\language\language $language + * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher */ - public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language) + public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language, \phpbb\event\dispatcher_interface $phpbb_dispatcher) { $this->context = $context; $this->environment = $environment; $this->language = $language; + $this->phpbb_dispatcher = $phpbb_dispatcher; } /** @@ -64,7 +69,7 @@ class extension extends \Twig\Extension\AbstractExtension new \phpbb\template\twig\tokenparser\includeparser, new \phpbb\template\twig\tokenparser\includejs, new \phpbb\template\twig\tokenparser\includecss, - new \phpbb\template\twig\tokenparser\event($this->environment), + new \phpbb\template\twig\tokenparser\event($this->environment, $this->phpbb_dispatcher), ); } diff --git a/phpBB/phpbb/template/twig/node/event.php b/phpBB/phpbb/template/twig/node/event.php index 4eddcbcf38..6b77549b67 100644 --- a/phpBB/phpbb/template/twig/node/event.php +++ b/phpBB/phpbb/template/twig/node/event.php @@ -24,9 +24,13 @@ class event extends \Twig\Node\Node /** @var \phpbb\template\twig\environment */ protected $environment; - public function __construct(\Twig\Node\Expression\AbstractExpression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null) + /** @var array */ + protected $template_event_priority_array; + + public function __construct(\Twig\Node\Expression\AbstractExpression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null, $template_event_priority_array = []) { $this->environment = $environment; + $this->template_event_priority_array = $template_event_priority_array; parent::__construct(array('expr' => $expr), array(), $lineno, $tag); } @@ -42,17 +46,26 @@ class event extends \Twig\Node\Node $location = $this->listener_directory . $this->getNode('expr')->getAttribute('name'); + $compiler_steps = []; + foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path) { $ext_namespace = str_replace('/', '_', $ext_namespace); + if (isset($this->template_event_priority_array[$ext_namespace][$location])) + { + $priority_key = $this->template_event_priority_array[$ext_namespace][$location]; + } + + $compiler_calls = []; + if ($this->environment->isDebug()) { // If debug mode is enabled, lets check for new/removed EVENT // templates on page load rather than at compile. This is // slower, but makes developing extensions easier (no need to // purge the cache when a new event template file is added) - $compiler + $compiler_calls[] = fn() => $compiler ->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n") ->indent() ; @@ -60,7 +73,7 @@ class event extends \Twig\Node\Node if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) { - $compiler + $compiler_calls[] = fn() => $compiler ->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n") // We set the namespace lookup order to be this extension first, then the main path @@ -72,11 +85,39 @@ class event extends \Twig\Node\Node if ($this->environment->isDebug()) { - $compiler + $compiler_calls[] = fn() => $compiler ->outdent() ->write("}\n\n") ; } + + if (!empty($compiler_calls)) + { + if (isset($priority_key)) + { + if (array_key_exists($priority_key, $compiler_steps)) + { + array_splice($compiler_steps, $priority_key, 0, [$compiler_calls]); + } + else + { + $compiler_steps[$priority_key] = $compiler_calls; + } + } + else + { + array_unshift($compiler_steps, $compiler_calls); + } + } + } + + krsort($compiler_steps); + foreach ($compiler_steps as $ext_namespace_steps) + { + foreach ($ext_namespace_steps as $step) + { + $step(); + } } } } diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index 7b9742cc95..3dadc7e3b3 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -18,14 +18,38 @@ class event extends \Twig\TokenParser\AbstractTokenParser /** @var \phpbb\template\twig\environment */ protected $environment; + /** @var \phpbb\event\dispatcher_interface */ + protected $phpbb_dispatcher; + + /** @var array */ + protected $template_event_priority_array; + /** * Constructor * * @param \phpbb\template\twig\environment $environment */ - public function __construct(\phpbb\template\twig\environment $environment) + public function __construct(\phpbb\template\twig\environment $environment, \phpbb\event\dispatcher_interface $phpbb_dispatcher = null) { $this->environment = $environment; + $this->phpbb_dispatcher = $phpbb_dispatcher; + + $template_event_priority_array = []; + + /** + * Allow assigning priority to template events + * + * @event core.twig_tokenparser_constructor + * @var array template_event_priority_array Array with template event priority assignments per extension namespace + * @since 3.3.12-RC1 + */ + if ($this->phpbb_dispatcher) + { + $vars = array('template_event_priority_array'); + extract($this->phpbb_dispatcher->trigger_event('core.twig_tokenparser_constructor', compact($vars))); + } + + $this->template_event_priority_array = $template_event_priority_array; } /** @@ -42,7 +66,7 @@ class event extends \Twig\TokenParser\AbstractTokenParser $stream = $this->parser->getStream(); $stream->expect(\Twig\Token::BLOCK_END_TYPE); - return new \phpbb\template\twig\node\event($expr, $this->environment, $token->getLine(), $this->getTag()); + return new \phpbb\template\twig\node\event($expr, $this->environment, $token->getLine(), $this->getTag(), $this->template_event_priority_array); } /** From d934c8c4b78dc6704d2b64e0c0ab767ae7427570 Mon Sep 17 00:00:00 2001 From: toxyy Date: Sun, 3 Dec 2023 16:52:15 -0500 Subject: [PATCH 02/16] [ticket/15214] Test fix for test_helper_url_no_rewrite Add new dispatch parameter to the template\twig\extension call PHPBB3-15214 --- tests/controller/common_helper_route.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/controller/common_helper_route.php b/tests/controller/common_helper_route.php index 0cd52b7d8b..19e4bad2f1 100644 --- a/tests/controller/common_helper_route.php +++ b/tests/controller/common_helper_route.php @@ -119,6 +119,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_ $assets_bag = new \phpbb\template\assets_bag(); $context = new \phpbb\template\context(); $loader = new \phpbb\template\twig\loader(''); + $this->dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $this->config, @@ -127,7 +128,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_ $cache_path, null, $loader, - new \phpbb\event\dispatcher(), + $this->dispatcher, array( 'cache' => false, 'debug' => false, @@ -135,7 +136,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_ 'autoescape' => false, ) ); - $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, $twig, $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, $twig, $this->user, $this->dispatcher))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->extension_manager = new phpbb_mock_extension_manager( From 59b482a22288c78724bae7d949da16fd99bc7bf3 Mon Sep 17 00:00:00 2001 From: toxyy Date: Sun, 3 Dec 2023 16:57:24 -0500 Subject: [PATCH 03/16] [ticket/15214] Test fix for test_bbcode_firstpass Add new dispatch parameter to the template\twig\extension call PHPBB3-15214 --- tests/email/email_parsing_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index fc1d1ef3be..7dbf802ee5 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -87,7 +87,7 @@ class phpbb_email_parsing_test extends phpbb_test_case 'autoescape' => false, ) ); - $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang); + $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang, $dispatcher); $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); From 71fe9d60c4fe41733a76a61ecf62478e4c3063bc Mon Sep 17 00:00:00 2001 From: toxyy Date: Sun, 3 Dec 2023 17:30:29 -0500 Subject: [PATCH 04/16] [ticket/15214] Add fixes for various other tests Add new dispatch parameter to the template\twig\extension calls PHPBB3-15214 --- tests/extension/metadata_manager_test.php | 2 +- tests/template/extension_test.php | 5 +++-- tests/template/template_allfolder_test.php | 5 +++-- tests/template/template_events_test.php | 5 +++-- tests/template/template_includecss_test.php | 5 +++-- tests/template/template_test_case.php | 6 ++++-- tests/template/template_test_case_with_tree.php | 5 +++-- 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 095efd3a69..48566096c6 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -117,7 +117,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $lang = new \phpbb\language\language($lang_loader); $this->user = new \phpbb\user($lang, '\phpbb\datetime'); - $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, $twig, $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, $twig, $this->user, $phpbb_dispatcher))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); } diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php index 5caae111ca..a762c9e026 100644 --- a/tests/template/extension_test.php +++ b/tests/template/extension_test.php @@ -102,6 +102,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case $loader = new \phpbb\template\twig\loader([]); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -110,7 +111,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case $cache_path, null, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, [ 'cache' => false, 'debug' => false, @@ -126,7 +127,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case $cache_path, $this->user, [ - new \phpbb\template\twig\extension($context, $twig, $this->lang), + new \phpbb\template\twig\extension($context, $twig, $this->lang, $dispatcher), new \phpbb\template\twig\extension\avatar($avatar_helper), new \phpbb\template\twig\extension\config($config), new \phpbb\template\twig\extension\icon($this->user), diff --git a/tests/template/template_allfolder_test.php b/tests/template/template_allfolder_test.php index a495f8aff8..88f23d4570 100644 --- a/tests/template/template_allfolder_test.php +++ b/tests/template/template_allfolder_test.php @@ -60,6 +60,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -68,7 +69,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case $cache_path, $this->extension_manager, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, array( 'cache' => false, 'debug' => false, @@ -76,7 +77,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $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, $twig, $this->user, $dispatcher)), $this->extension_manager); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template_path = $this->test_path . '/templates'; diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index a9b1d39451..3f005ae6ec 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -154,6 +154,7 @@ Zeta test event in all', $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -162,7 +163,7 @@ Zeta test event in all', $cache_path, $this->extension_manager, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, array( 'cache' => false, 'debug' => false, @@ -170,7 +171,7 @@ Zeta test event in all', 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $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, $twig, $this->user, $dispatcher)), $this->extension_manager); $twig->setLexer(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_includecss_test.php b/tests/template/template_includecss_test.php index a11f627c6f..603fa6c08c 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -46,6 +46,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -54,7 +55,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $cache_path, null, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, array( 'cache' => false, 'debug' => false, @@ -69,7 +70,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $twig, $cache_path, $this->user, - array(new \phpbb\template\twig\extension($context, $twig, $this->user)), + array(new \phpbb\template\twig\extension($context, $twig, $this->user, $dispatcher)), new phpbb_mock_extension_manager( __DIR__ . '/', array( diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 5224e24b71..da9280c6eb 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -97,6 +97,7 @@ class phpbb_template_template_test_case extends phpbb_test_case $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -105,7 +106,7 @@ class phpbb_template_template_test_case extends phpbb_test_case $cache_path, null, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, array( 'cache' => false, 'debug' => false, @@ -113,7 +114,8 @@ class phpbb_template_template_test_case extends phpbb_test_case 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $lang))); + + $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $lang, $dispatcher))); $twig->setLexer(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 214d3503c8..637a1af04a 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -47,6 +47,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -55,7 +56,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $cache_path, null, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, array( 'cache' => false, 'debug' => false, @@ -63,7 +64,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat 'autoescape' => false, ) ); - $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, $twig, $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, $twig, $this->user, $dispatcher))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); } From 82a5e20f3eaa9abef62fb97badfc8170ae6ea02d Mon Sep 17 00:00:00 2001 From: toxyy Date: Sun, 3 Dec 2023 17:44:38 -0500 Subject: [PATCH 05/16] [ticket/15214] Replace arrow functions with anonymous functions Arrow functions aren't added until PHP 7.4, so we can't use them yet. Anonymous functions have been added since PHP 5.3 PHPBB3-15214 --- phpBB/phpbb/template/twig/node/event.php | 86 ++++++++---------------- 1 file changed, 29 insertions(+), 57 deletions(-) diff --git a/phpBB/phpbb/template/twig/node/event.php b/phpBB/phpbb/template/twig/node/event.php index 6b77549b67..badd1d5f62 100644 --- a/phpBB/phpbb/template/twig/node/event.php +++ b/phpBB/phpbb/template/twig/node/event.php @@ -46,77 +46,49 @@ class event extends \Twig\Node\Node $location = $this->listener_directory . $this->getNode('expr')->getAttribute('name'); - $compiler_steps = []; + $template_events = []; + // Group and sort extension template events in according to their priority (0 by default if not set) foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path) { $ext_namespace = str_replace('/', '_', $ext_namespace); - - if (isset($this->template_event_priority_array[$ext_namespace][$location])) - { - $priority_key = $this->template_event_priority_array[$ext_namespace][$location]; - } - - $compiler_calls = []; - - if ($this->environment->isDebug()) - { - // If debug mode is enabled, lets check for new/removed EVENT - // templates on page load rather than at compile. This is - // slower, but makes developing extensions easier (no need to - // purge the cache when a new event template file is added) - $compiler_calls[] = fn() => $compiler - ->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n") - ->indent() - ; - } - if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) { - $compiler_calls[] = fn() => $compiler + $priority_key = $this->template_event_priority_array[$ext_namespace][$location] ?? 0; + $template_events[$priority_key][] = $ext_namespace; + } + } + krsort($template_events); + + foreach ($template_events as $events) + { + foreach ($events as $ext_namespace) + { + if ($this->environment->isDebug()) + { + // If debug mode is enabled, lets check for new/removed EVENT + // templates on page load rather than at compile. This is + // slower, but makes developing extensions easier (no need to + // purge the cache when a new event template file is added) + $compiler + ->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n") + ->indent(); + } + + $compiler ->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n") // We set the namespace lookup order to be this extension first, then the main path ->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n") ->write("\$this->env->loadTemplate(\$this->env->getTemplateClass('@{$ext_namespace}/{$location}.html'), '@{$ext_namespace}/{$location}.html')->display(\$context);\n") - ->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n") - ; - } + ->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n"); - if ($this->environment->isDebug()) - { - $compiler_calls[] = fn() => $compiler - ->outdent() - ->write("}\n\n") - ; - } - - if (!empty($compiler_calls)) - { - if (isset($priority_key)) + if ($this->environment->isDebug()) { - if (array_key_exists($priority_key, $compiler_steps)) - { - array_splice($compiler_steps, $priority_key, 0, [$compiler_calls]); - } - else - { - $compiler_steps[$priority_key] = $compiler_calls; - } + $compiler + ->outdent() + ->write("}\n\n"); } - else - { - array_unshift($compiler_steps, $compiler_calls); - } - } - } - - krsort($compiler_steps); - foreach ($compiler_steps as $ext_namespace_steps) - { - foreach ($ext_namespace_steps as $step) - { - $step(); } } } From 0eb98d51e2326a21ff04efe8113fb32ba3a8ef38 Mon Sep 17 00:00:00 2001 From: toxyy Date: Tue, 5 Dec 2023 03:07:12 -0500 Subject: [PATCH 06/16] [ticket/15214] Provide usage example within event docblock Adds similar usage examples like the event core.permissions has PHPBB3-15214 --- phpBB/phpbb/template/twig/tokenparser/event.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index 3dadc7e3b3..bfc2c573fa 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -40,7 +40,15 @@ class event extends \Twig\TokenParser\AbstractTokenParser * Allow assigning priority to template events * * @event core.twig_tokenparser_constructor - * @var array template_event_priority_array Array with template event priority assignments per extension namespace + * @var array template_event_priority_array Array with template event priority assignments per extension namespace, usage: + * '_' => array( + * 'event/' => priority_number, + * ), + * Example: + * 'phpbb_viglink' => array( + * 'event/acp_help_phpbb_stats_after' => 80, + * 'event/overall_footer_after' => 100, + * ), * @since 3.3.12-RC1 */ if ($this->phpbb_dispatcher) From cb47d78d265711307370fada7522bac93eea9848 Mon Sep 17 00:00:00 2001 From: toxyy Date: Tue, 5 Dec 2023 03:32:57 -0500 Subject: [PATCH 07/16] [ticket/15214] Update block, restart tests Make docblock look a bit cleaner and restart the tests PHPBB3-15214 --- phpBB/phpbb/template/twig/tokenparser/event.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index bfc2c573fa..7c11d32b69 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -38,9 +38,10 @@ class event extends \Twig\TokenParser\AbstractTokenParser /** * Allow assigning priority to template events - * + *f * @event core.twig_tokenparser_constructor - * @var array template_event_priority_array Array with template event priority assignments per extension namespace, usage: + * @var array template_event_priority_array Array with template event priority assignments per extension namespace + * Usage: * '_' => array( * 'event/' => priority_number, * ), From 4a00212f2d3a3d39d56f8c940420fd2edf5bc308 Mon Sep 17 00:00:00 2001 From: rxu Date: Fri, 8 Dec 2023 18:47:56 +0700 Subject: [PATCH 08/16] [ticket/15214] Optimize event node code and add template event order tests PHPBB3-15214 --- .../extension_template_event_order_test.php | 86 +++++++++++++++++++ .../fixtures/ext/foo/bar/config/services.yml | 6 ++ .../foo/bar/event/template_event_order.php | 38 ++++++++ .../bar/event/template_event_order_higher.php | 38 ++++++++ .../navbar_header_quick_links_after.html | 1 + .../fixtures/ext/foo/foo/config/services.yml | 5 ++ .../foo/foo/event/template_event_order.php | 38 ++++++++ .../foo/event/template_event_order_lower.php | 38 ++++++++ .../navbar_header_quick_links_after.html | 1 + 9 files changed, 251 insertions(+) create mode 100644 tests/functional/extension_template_event_order_test.php create mode 100644 tests/functional/fixtures/ext/foo/bar/event/template_event_order.php create mode 100644 tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php create mode 100644 tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/event/navbar_header_quick_links_after.html create mode 100644 tests/functional/fixtures/ext/foo/foo/event/template_event_order.php create mode 100644 tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php create mode 100644 tests/functional/fixtures/ext/foo/foo/styles/prosilver/template/event/navbar_header_quick_links_after.html diff --git a/tests/functional/extension_template_event_order_test.php b/tests/functional/extension_template_event_order_test.php new file mode 100644 index 0000000000..c931c95bc1 --- /dev/null +++ b/tests/functional/extension_template_event_order_test.php @@ -0,0 +1,86 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +/** +* @group functional +*/ +class phpbb_functional_extension_template_event_order_test extends phpbb_functional_test_case +{ + protected $phpbb_extension_manager; + + static private $helper; + + static protected $fixtures = [ + './', + ]; + + static public function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + self::$helper = new phpbb_test_case_helpers(__CLASS__); + self::$helper->copy_ext_fixtures(__DIR__ . '/fixtures/ext/', self::$fixtures); + } + + static public function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + + self::$helper->restore_original_ext_dir(); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->phpbb_extension_manager = $this->get_extension_manager(); + + $this->purge_cache(); + } + + /** + * Check a controller for extension foo/bar. + */ + public function test_template_event_order() + { + global $phpbb_root_path; + + $this->phpbb_extension_manager->enable('foo/bar'); + $this->phpbb_extension_manager->enable('foo/foo'); + $crawler = self::request('GET', 'index.php'); + $quick_links_menu = $crawler->filter('ul[role="menu"]')->eq(0); + $quick_links_menu_nodes_count = (int) $quick_links_menu->filter('li')->count(); + // Ensure foo/foo template event goes before foo/bar one + $this->assertStringContainsString('FOO_FOO_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 2)->filter('span')->text()); + $this->assertStringContainsString('FOO_BAR_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 1)->filter('span')->text()); + + // Change template events order to default, put foo/bar event before foo/foo one + $this->phpbb_extension_manager->disable('foo/bar'); + $this->phpbb_extension_manager->disable('foo/foo'); + $this->assertTrue(copy(__DIR__ . '/fixtures/ext/foo/bar/event/template_event_order_higher.php', $phpbb_root_path . 'ext/foo/bar/event/template_event_order.php')); + $this->assertTrue(copy(__DIR__ . '/fixtures/ext/foo/foo/event/template_event_order_lower.php', $phpbb_root_path . 'ext/foo/foo/event/template_event_order.php')); + $this->phpbb_extension_manager->enable('foo/bar'); + $this->phpbb_extension_manager->enable('foo/foo'); + $this->purge_cache(); + sleep(3); + $crawler = self::request('GET', 'index.php'); + $quick_links_menu = $crawler->filter('ul[role="menu"]')->eq(0); + $quick_links_menu_nodes_count = (int) $quick_links_menu->filter('li')->count(); + // Ensure foo/foo template event goes before foo/bar one + $this->assertStringContainsString('FOO_BAR_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 2)->filter('span')->text()); + $this->assertStringContainsString('FOO_FOO_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 1)->filter('span')->text()); + + $this->phpbb_extension_manager->purge('foo/bar'); + $this->phpbb_extension_manager->purge('foo/foo'); + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/config/services.yml b/tests/functional/fixtures/ext/foo/bar/config/services.yml index 495c775a1f..cac5f9cd76 100644 --- a/tests/functional/fixtures/ext/foo/bar/config/services.yml +++ b/tests/functional/fixtures/ext/foo/bar/config/services.yml @@ -14,7 +14,13 @@ services: class: foo\bar\event\permission tags: - { name: event.listener } + foo_bar.listener.user_setup: class: foo\bar\event\user_setup tags: - { name: event.listener } + + foo_bar.listener.template_event_order: + class: foo\bar\event\template_event_order + tags: + - { name: event.listener } diff --git a/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php b/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php new file mode 100644 index 0000000000..9cb6a9c71b --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php @@ -0,0 +1,38 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace foo\bar\event; + +/** +* Event listener +*/ +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class template_event_order implements EventSubscriberInterface +{ + static public function getSubscribedEvents() + { + return array( + 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + ); + } + + public function set_template_event_priority($event) + { + $template_event_priority_array = $event['template_event_priority_array']; + $template_event_priority_array['foo_bar'] = [ + 'event/navbar_header_quick_links_after' => -1, + ]; + $event['template_event_priority_array'] = $template_event_priority_array; + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php b/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php new file mode 100644 index 0000000000..43d5c05be3 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php @@ -0,0 +1,38 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace foo\bar\event; + +/** +* Event listener +*/ +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class template_event_order implements EventSubscriberInterface +{ + static public function getSubscribedEvents() + { + return array( + 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + ); + } + + public function set_template_event_priority($event) + { + $template_event_priority_array = $event['template_event_priority_array']; + $template_event_priority_array['foo_bar'] = [ + 'event/navbar_header_quick_links_after' => 1, + ]; + $event['template_event_priority_array'] = $template_event_priority_array; + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/event/navbar_header_quick_links_after.html b/tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/event/navbar_header_quick_links_after.html new file mode 100644 index 0000000000..40be5d2b38 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/event/navbar_header_quick_links_after.html @@ -0,0 +1 @@ +
  • {{ lang('FOO_BAR_QUICK_LINK') }}
  • \ No newline at end of file diff --git a/tests/functional/fixtures/ext/foo/foo/config/services.yml b/tests/functional/fixtures/ext/foo/foo/config/services.yml index b3c7719715..fc10b65e56 100644 --- a/tests/functional/fixtures/ext/foo/foo/config/services.yml +++ b/tests/functional/fixtures/ext/foo/foo/config/services.yml @@ -1,3 +1,8 @@ services: foo_foo.controller: class: foo\foo\controller\controller + + foo_foo.listener.template_event_order: + class: foo\foo\event\template_event_order + tags: + - { name: event.listener } diff --git a/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php b/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php new file mode 100644 index 0000000000..cba0340e16 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php @@ -0,0 +1,38 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace foo\foo\event; + +/** +* Event listener +*/ +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class template_event_order implements EventSubscriberInterface +{ + static public function getSubscribedEvents() + { + return array( + 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + ); + } + + public function set_template_event_priority($event) + { + $template_event_priority_array = $event['template_event_priority_array']; + $template_event_priority_array['foo_bar'] = [ + 'event/navbar_header_quick_links_after' => 1, + ]; + $event['template_event_priority_array'] = $template_event_priority_array; + } +} diff --git a/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php b/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php new file mode 100644 index 0000000000..6cb1566417 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php @@ -0,0 +1,38 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace foo\foo\event; + +/** +* Event listener +*/ +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class template_event_order implements EventSubscriberInterface +{ + static public function getSubscribedEvents() + { + return array( + 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + ); + } + + public function set_template_event_priority($event) + { + $template_event_priority_array = $event['template_event_priority_array']; + $template_event_priority_array['foo_bar'] = [ + 'event/navbar_header_quick_links_after' => -1, + ]; + $event['template_event_priority_array'] = $template_event_priority_array; + } +} diff --git a/tests/functional/fixtures/ext/foo/foo/styles/prosilver/template/event/navbar_header_quick_links_after.html b/tests/functional/fixtures/ext/foo/foo/styles/prosilver/template/event/navbar_header_quick_links_after.html new file mode 100644 index 0000000000..932c93caff --- /dev/null +++ b/tests/functional/fixtures/ext/foo/foo/styles/prosilver/template/event/navbar_header_quick_links_after.html @@ -0,0 +1 @@ +
  • {{ lang('FOO_FOO_QUICK_LINK') }}
  • \ No newline at end of file From 09fd86ffb04999a868e48dc0944ec627abd6916c Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 10 Dec 2023 00:37:48 +0700 Subject: [PATCH 09/16] [ticket/15214] Fix test foo/foo extension listener PHPBB3-15214 --- .../fixtures/ext/foo/foo/event/template_event_order.php | 2 +- .../fixtures/ext/foo/foo/event/template_event_order_lower.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php b/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php index cba0340e16..28652c23fc 100644 --- a/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php +++ b/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php @@ -30,7 +30,7 @@ class template_event_order implements EventSubscriberInterface public function set_template_event_priority($event) { $template_event_priority_array = $event['template_event_priority_array']; - $template_event_priority_array['foo_bar'] = [ + $template_event_priority_array['foo_foo'] = [ 'event/navbar_header_quick_links_after' => 1, ]; $event['template_event_priority_array'] = $template_event_priority_array; diff --git a/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php b/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php index 6cb1566417..e360209481 100644 --- a/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php +++ b/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php @@ -30,7 +30,7 @@ class template_event_order implements EventSubscriberInterface public function set_template_event_priority($event) { $template_event_priority_array = $event['template_event_priority_array']; - $template_event_priority_array['foo_bar'] = [ + $template_event_priority_array['foo_foo'] = [ 'event/navbar_header_quick_links_after' => -1, ]; $event['template_event_priority_array'] = $template_event_priority_array; From 18bae795f0766bdc2ad76c25b3f7f8680ddcc887 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 10 Dec 2023 14:02:30 +0700 Subject: [PATCH 10/16] [ticket/15214] Fix Windows tests Purge Twig compiled cache in Windows. Set appropriate folder access control options to do that. PHPBB3-15214 --- .github/workflows/tests.yml | 4 +--- tests/functional/extension_global_lang_test.php | 16 ++++------------ .../extension_permission_lang_test.php | 9 +++++++++ .../extension_template_event_order_test.php | 7 +++++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 28d64bc69a..9ae4b0bfe7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -558,9 +558,7 @@ jobs: New-WebHandler -Name "PHP-FastCGI" -Path "*.php" -Modules FastCgiModule -ScriptProcessor "C:\tools\php\php-cgi.exe" -Verb '*' -ResourceType Either iisreset NET START W3SVC - mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\test" - mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\installer" - icacls "${env:GITHUB_WORKSPACE}\phpBB\cache" /grant Users:F /T + icacls "${env:GITHUB_WORKSPACE}\phpBB\cache" /grant "Users:(OI)(CI)F" /T icacls "${env:GITHUB_WORKSPACE}\phpBB\files" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\store" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\ext" /grant Users:F /T diff --git a/tests/functional/extension_global_lang_test.php b/tests/functional/extension_global_lang_test.php index cf2d9dbf29..85275d73b0 100644 --- a/tests/functional/extension_global_lang_test.php +++ b/tests/functional/extension_global_lang_test.php @@ -47,20 +47,10 @@ class phpbb_functional_extension_global_lang_test extends phpbb_functional_test_ $this->purge_cache(); } - protected function tearDown(): void - { - $this->uninstall_ext('foo/bar'); - - parent::tearDown(); - } - - protected static function setup_extensions() - { - return ['foo/bar']; - } - public function test_load_extension_lang_globally() { + $this->phpbb_extension_manager->enable('foo/bar'); + // The board index, which should contain an overwritten translation $crawler = self::request('GET', 'index.php'); @@ -69,5 +59,7 @@ class phpbb_functional_extension_global_lang_test extends phpbb_functional_test_ // language from ext/foo/bar/language/en/foo_global.php $this->assertStringContainsString('Overwritten by foo', $crawler->filter('.skiplink')->text()); + + $this->phpbb_extension_manager->purge('foo/bar'); } } diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 9516e351df..0aad34f28a 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -39,6 +39,13 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t self::$helper->restore_original_ext_dir(); } + protected function tearDown(): void + { + $this->purge_cache(); + + parent::tearDown(); + } + protected function setUp(): void { parent::setUp(); @@ -76,5 +83,7 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t // language from ext/foo/bar/language/en/permissions_foo.php $this->assertStringContainsString('Can view foobar', $crawler->filter('body')->text()); + + $this->phpbb_extension_manager->purge('foo/bar'); } } diff --git a/tests/functional/extension_template_event_order_test.php b/tests/functional/extension_template_event_order_test.php index c931c95bc1..c77cb7b89f 100644 --- a/tests/functional/extension_template_event_order_test.php +++ b/tests/functional/extension_template_event_order_test.php @@ -39,6 +39,13 @@ class phpbb_functional_extension_template_event_order_test extends phpbb_functio self::$helper->restore_original_ext_dir(); } + protected function tearDown(): void + { + $this->purge_cache(); + + parent::tearDown(); + } + protected function setUp(): void { parent::setUp(); From 84e7e34a660a28206a98f9e17b2c86d35424f210 Mon Sep 17 00:00:00 2001 From: toxyy Date: Sun, 10 Dec 2023 11:47:36 -0500 Subject: [PATCH 11/16] [ticket/15214] Fix tests again Adding per rxu's recommendation PHPBB3-15214 --- .github/workflows/tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9ae4b0bfe7..411f586721 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -559,6 +559,9 @@ jobs: iisreset NET START W3SVC icacls "${env:GITHUB_WORKSPACE}\phpBB\cache" /grant "Users:(OI)(CI)F" /T + mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\test" + mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\test\twig" + mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\installer" icacls "${env:GITHUB_WORKSPACE}\phpBB\files" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\store" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\ext" /grant Users:F /T From 8338ff9e56c9eaacfe9b170787ffb8e6ecc6ff54 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 10 Dec 2023 23:24:11 +0700 Subject: [PATCH 12/16] [ticket/15214] Fix Windows tests PHPBB3-15214 --- .github/workflows/tests.yml | 3 +-- tests/functional/extension_permission_lang_test.php | 7 ------- tests/functional/extension_template_event_order_test.php | 7 ------- tests/test_framework/phpbb_functional_test_case.php | 3 +++ 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 411f586721..28d64bc69a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -558,10 +558,9 @@ jobs: New-WebHandler -Name "PHP-FastCGI" -Path "*.php" -Modules FastCgiModule -ScriptProcessor "C:\tools\php\php-cgi.exe" -Verb '*' -ResourceType Either iisreset NET START W3SVC - icacls "${env:GITHUB_WORKSPACE}\phpBB\cache" /grant "Users:(OI)(CI)F" /T mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\test" - mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\test\twig" mkdir "${env:GITHUB_WORKSPACE}\phpBB\cache\installer" + icacls "${env:GITHUB_WORKSPACE}\phpBB\cache" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\files" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\store" /grant Users:F /T icacls "${env:GITHUB_WORKSPACE}\phpBB\ext" /grant Users:F /T diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 0aad34f28a..13812a08b6 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -39,13 +39,6 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t self::$helper->restore_original_ext_dir(); } - protected function tearDown(): void - { - $this->purge_cache(); - - parent::tearDown(); - } - protected function setUp(): void { parent::setUp(); diff --git a/tests/functional/extension_template_event_order_test.php b/tests/functional/extension_template_event_order_test.php index c77cb7b89f..c931c95bc1 100644 --- a/tests/functional/extension_template_event_order_test.php +++ b/tests/functional/extension_template_event_order_test.php @@ -39,13 +39,6 @@ class phpbb_functional_extension_template_event_order_test extends phpbb_functio self::$helper->restore_original_ext_dir(); } - protected function tearDown(): void - { - $this->purge_cache(); - - parent::tearDown(); - } - protected function setUp(): void { parent::setUp(); diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index c42e0aa60b..3e0b7b32a8 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -146,6 +146,9 @@ class phpbb_functional_test_case extends phpbb_test_case // Close the database connections again this test $this->db->sql_close(); } + + $this->purge_cache(); + sleep(3); // Give it some time to delete all the files correctly } /** From 5e0dc9ef2edf9742aa5f73a356f6b89551ebac5e Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 5 Jun 2024 21:58:05 +0700 Subject: [PATCH 13/16] [ticket/15214] Fix rebasing and some other issues PHPBB3-15214 --- .../phpbb/template/twig/tokenparser/event.php | 40 +++++++++-------- tests/console/user/base.php | 2 +- .../functional/extension_controller_test.php | 2 - .../functional/extension_global_lang_test.php | 18 +++++--- tests/functional/extension_module_test.php | 2 - .../extension_permission_lang_test.php | 4 -- .../extension_template_event_order_test.php | 43 +++++++++++-------- tests/functional/metadata_manager_test.php | 2 - tests/template/twig_test.php | 5 ++- .../phpbb_functional_test_case.php | 7 +-- 10 files changed, 65 insertions(+), 60 deletions(-) diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index 7c11d32b69..19a876ec3f 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -35,30 +35,36 @@ class event extends \Twig\TokenParser\AbstractTokenParser $this->phpbb_dispatcher = $phpbb_dispatcher; $template_event_priority_array = []; - /** - * Allow assigning priority to template events - *f - * @event core.twig_tokenparser_constructor - * @var array template_event_priority_array Array with template event priority assignments per extension namespace - * Usage: - * '_' => array( - * 'event/' => priority_number, - * ), - * Example: - * 'phpbb_viglink' => array( - * 'event/acp_help_phpbb_stats_after' => 80, - * 'event/overall_footer_after' => 100, - * ), - * @since 3.3.12-RC1 - */ + * Allow assigning priority to template events + * + * The higher number - the higher tempate event listener priority value is. + * In case of equal priority values, corresponding template event listeners will be handled in default compilation order. + * If not set, template event listener priority will be assigned to the value of 0. + * + * @event core.twig_tokenparser_constructor + * @var array template_event_priority_array Array with template event priority assignments per extension namespace + * Usage: + * '_' => [ + * 'event/' => priority_number, + * ], + * + * Example: + * 'phpbb_viglink' => [ + * 'event/acp_help_phpbb_stats_after' => 80, + * 'event/overall_footer_after' => 100, + * ], + * + * @since 4.0.0-a1 + */ if ($this->phpbb_dispatcher) { - $vars = array('template_event_priority_array'); + $vars = ['template_event_priority_array']; extract($this->phpbb_dispatcher->trigger_event('core.twig_tokenparser_constructor', compact($vars))); } $this->template_event_priority_array = $template_event_priority_array; + unset($template_event_priority_array); } /** diff --git a/tests/console/user/base.php b/tests/console/user/base.php index 511cfff9a3..605493bd80 100644 --- a/tests/console/user/base.php +++ b/tests/console/user/base.php @@ -190,7 +190,7 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case 'autoescape' => false, ] ); - $twig_extension = new \phpbb\template\twig\extension($context, $twig, $this->language); + $twig_extension = new \phpbb\template\twig\extension($context, $twig, $this->language, $phpbb_dispatcher); $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); diff --git a/tests/functional/extension_controller_test.php b/tests/functional/extension_controller_test.php index 5c42a2353b..4cc5e2d2a9 100644 --- a/tests/functional/extension_controller_test.php +++ b/tests/functional/extension_controller_test.php @@ -16,8 +16,6 @@ */ class phpbb_functional_extension_controller_test extends phpbb_functional_test_case { - protected $phpbb_extension_manager; - private static $helper; protected static $fixtures = array( diff --git a/tests/functional/extension_global_lang_test.php b/tests/functional/extension_global_lang_test.php index 85275d73b0..1fb1d053e3 100644 --- a/tests/functional/extension_global_lang_test.php +++ b/tests/functional/extension_global_lang_test.php @@ -16,8 +16,6 @@ */ class phpbb_functional_extension_global_lang_test extends phpbb_functional_test_case { - protected $phpbb_extension_manager; - private static $helper; protected static $fixtures = array( @@ -47,10 +45,20 @@ class phpbb_functional_extension_global_lang_test extends phpbb_functional_test_ $this->purge_cache(); } + protected function tearDown(): void + { + $this->uninstall_ext('foo/bar'); + + parent::tearDown(); + } + + protected static function setup_extensions() + { + return ['foo/bar']; + } + public function test_load_extension_lang_globally() { - $this->phpbb_extension_manager->enable('foo/bar'); - // The board index, which should contain an overwritten translation $crawler = self::request('GET', 'index.php'); @@ -59,7 +67,5 @@ class phpbb_functional_extension_global_lang_test extends phpbb_functional_test_ // language from ext/foo/bar/language/en/foo_global.php $this->assertStringContainsString('Overwritten by foo', $crawler->filter('.skiplink')->text()); - - $this->phpbb_extension_manager->purge('foo/bar'); } } diff --git a/tests/functional/extension_module_test.php b/tests/functional/extension_module_test.php index 0e56e43c78..08ec7e5012 100644 --- a/tests/functional/extension_module_test.php +++ b/tests/functional/extension_module_test.php @@ -17,8 +17,6 @@ require_once __DIR__ . '/../../phpBB/includes/acp/acp_modules.php'; */ class phpbb_functional_extension_module_test extends phpbb_functional_test_case { - protected $phpbb_extension_manager; - private static $helper; protected static $fixtures = array( diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 13812a08b6..e7dcb4ebcd 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -16,8 +16,6 @@ */ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_test_case { - protected $phpbb_extension_manager; - private static $helper; protected static $fixtures = array( @@ -76,7 +74,5 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t // language from ext/foo/bar/language/en/permissions_foo.php $this->assertStringContainsString('Can view foobar', $crawler->filter('body')->text()); - - $this->phpbb_extension_manager->purge('foo/bar'); } } diff --git a/tests/functional/extension_template_event_order_test.php b/tests/functional/extension_template_event_order_test.php index c931c95bc1..44586fc719 100644 --- a/tests/functional/extension_template_event_order_test.php +++ b/tests/functional/extension_template_event_order_test.php @@ -16,8 +16,6 @@ */ class phpbb_functional_extension_template_event_order_test extends phpbb_functional_test_case { - protected $phpbb_extension_manager; - static private $helper; static protected $fixtures = [ @@ -43,11 +41,22 @@ class phpbb_functional_extension_template_event_order_test extends phpbb_functio { parent::setUp(); - $this->phpbb_extension_manager = $this->get_extension_manager(); - $this->purge_cache(); } + protected function tearDown(): void + { + $this->uninstall_ext('foo/bar'); + $this->uninstall_ext('foo/foo'); + + parent::tearDown(); + } + + protected static function setup_extensions() + { + return ['foo/bar', 'foo/foo']; + } + /** * Check a controller for extension foo/bar. */ @@ -55,32 +64,28 @@ class phpbb_functional_extension_template_event_order_test extends phpbb_functio { global $phpbb_root_path; - $this->phpbb_extension_manager->enable('foo/bar'); - $this->phpbb_extension_manager->enable('foo/foo'); $crawler = self::request('GET', 'index.php'); $quick_links_menu = $crawler->filter('ul[role="menu"]')->eq(0); $quick_links_menu_nodes_count = (int) $quick_links_menu->filter('li')->count(); // Ensure foo/foo template event goes before foo/bar one - $this->assertStringContainsString('FOO_FOO_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 2)->filter('span')->text()); - $this->assertStringContainsString('FOO_BAR_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 1)->filter('span')->text()); + $this->assertStringContainsString('FOO_FOO_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 4)->filter('span')->text()); + $this->assertStringContainsString('FOO_BAR_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 3)->filter('span')->text()); // Change template events order to default, put foo/bar event before foo/foo one - $this->phpbb_extension_manager->disable('foo/bar'); - $this->phpbb_extension_manager->disable('foo/foo'); + $this->disable_ext('foo/bar'); + $this->disable_ext('foo/foo'); + $this->assertTrue(copy(__DIR__ . '/fixtures/ext/foo/bar/event/template_event_order_higher.php', $phpbb_root_path . 'ext/foo/bar/event/template_event_order.php')); $this->assertTrue(copy(__DIR__ . '/fixtures/ext/foo/foo/event/template_event_order_lower.php', $phpbb_root_path . 'ext/foo/foo/event/template_event_order.php')); - $this->phpbb_extension_manager->enable('foo/bar'); - $this->phpbb_extension_manager->enable('foo/foo'); - $this->purge_cache(); - sleep(3); + + $this->install_ext('foo/bar'); + $this->install_ext('foo/foo'); + $crawler = self::request('GET', 'index.php'); $quick_links_menu = $crawler->filter('ul[role="menu"]')->eq(0); $quick_links_menu_nodes_count = (int) $quick_links_menu->filter('li')->count(); // Ensure foo/foo template event goes before foo/bar one - $this->assertStringContainsString('FOO_BAR_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 2)->filter('span')->text()); - $this->assertStringContainsString('FOO_FOO_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 1)->filter('span')->text()); - - $this->phpbb_extension_manager->purge('foo/bar'); - $this->phpbb_extension_manager->purge('foo/foo'); + $this->assertStringContainsString('FOO_BAR_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 4)->filter('span')->text()); + $this->assertStringContainsString('FOO_FOO_QUICK_LINK', $quick_links_menu->filter('li')->eq($quick_links_menu_nodes_count - 3)->filter('span')->text()); } } diff --git a/tests/functional/metadata_manager_test.php b/tests/functional/metadata_manager_test.php index 8b4eb8fde5..206a72b2eb 100644 --- a/tests/functional/metadata_manager_test.php +++ b/tests/functional/metadata_manager_test.php @@ -16,8 +16,6 @@ */ class phpbb_functional_metadata_manager_test extends phpbb_functional_test_case { - protected $phpbb_extension_manager; - private static $helper; protected static $fixtures = array( diff --git a/tests/template/twig_test.php b/tests/template/twig_test.php index 69280caa15..a3bb071b0f 100644 --- a/tests/template/twig_test.php +++ b/tests/template/twig_test.php @@ -65,6 +65,7 @@ class twig_test extends \phpbb_test_case $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); + $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -73,7 +74,7 @@ class twig_test extends \phpbb_test_case $cache_path, null, $loader, - new \phpbb\event\dispatcher(), + $dispatcher, [ 'cache' => false, 'debug' => false, @@ -81,7 +82,7 @@ class twig_test extends \phpbb_test_case 'autoescape' => false, ] ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $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, $twig, $this->user, $dispatcher))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); } diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 3e0b7b32a8..7a1d7afa78 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -146,9 +146,6 @@ class phpbb_functional_test_case extends phpbb_test_case // Close the database connections again this test $this->db->sql_close(); } - - $this->purge_cache(); - sleep(3); // Give it some time to delete all the files correctly } /** @@ -410,7 +407,7 @@ class phpbb_functional_test_case extends phpbb_test_case 'autoescape' => false, ] ); - $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang); + $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang, $phpbb_dispatcher); $container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($container); @@ -647,7 +644,7 @@ class phpbb_functional_test_case extends phpbb_test_case $meta_refresh = $crawler->filter('meta[http-equiv="refresh"]'); - // Wait for extension to be fully enabled + // Wait for extension to be fully disabled while (count($meta_refresh)) { preg_match('#url=.+/(adm+.+)#', $meta_refresh->attr('content'), $match); From 3a5247d01b8678bb48afc6c94dbbba9140c2e90f Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 20 May 2025 16:54:03 +0700 Subject: [PATCH 14/16] [ticket/15214] Get event dispatcher from environment rather than as dependency Also this will allow to significantly reduce unrelated tests changes. PHPBB3-15214 --- phpBB/config/default/container/services_twig.yml | 1 - phpBB/phpbb/template/twig/environment.php | 10 ++++++++++ phpBB/phpbb/template/twig/extension.php | 9 ++------- phpBB/phpbb/template/twig/tokenparser/event.php | 4 ++-- tests/console/user/base.php | 2 +- tests/controller/common_helper_route.php | 5 ++--- tests/email/email_parsing_test.php | 2 +- tests/extension/metadata_manager_test.php | 2 +- tests/template/extension_test.php | 5 ++--- tests/template/template_allfolder_test.php | 5 ++--- tests/template/template_events_test.php | 5 ++--- tests/template/template_includecss_test.php | 5 ++--- tests/template/template_test_case.php | 6 ++---- tests/template/template_test_case_with_tree.php | 5 ++--- tests/template/twig_test.php | 5 ++--- tests/test_framework/phpbb_functional_test_case.php | 2 +- 16 files changed, 34 insertions(+), 39 deletions(-) diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index c10dfc577c..e7a7155deb 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -40,7 +40,6 @@ services: - '@template_context' - '@template.twig.environment' - '@language' - - '@dispatcher' tags: - { name: twig.extension } diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index a5af650c66..68351076fd 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -161,6 +161,16 @@ class environment extends \Twig\Environment return $this->assets_bag; } + /** + * Gets the event dispatcher instance + * + * @return dispatcher_interface + */ + public function get_phpbb_dispatcher() + { + return $this->phpbb_dispatcher; + } + /** * Get the namespace look up order * diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 9f2bfebb9f..982601e41b 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -28,23 +28,18 @@ class extension extends \Twig\Extension\AbstractExtension /** @var \phpbb\language\language */ protected $language; - /** @var \phpbb\event\dispatcher_interface */ - protected $phpbb_dispatcher; - /** * Constructor * * @param \phpbb\template\context $context * @param \phpbb\template\twig\environment $environment * @param \phpbb\language\language $language - * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher */ - public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language, \phpbb\event\dispatcher_interface $phpbb_dispatcher) + public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language) { $this->context = $context; $this->environment = $environment; $this->language = $language; - $this->phpbb_dispatcher = $phpbb_dispatcher; } /** @@ -69,7 +64,7 @@ class extension extends \Twig\Extension\AbstractExtension new \phpbb\template\twig\tokenparser\includeparser, new \phpbb\template\twig\tokenparser\includejs, new \phpbb\template\twig\tokenparser\includecss, - new \phpbb\template\twig\tokenparser\event($this->environment, $this->phpbb_dispatcher), + new \phpbb\template\twig\tokenparser\event($this->environment), ); } diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index 19a876ec3f..2fb096d454 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -29,10 +29,10 @@ class event extends \Twig\TokenParser\AbstractTokenParser * * @param \phpbb\template\twig\environment $environment */ - public function __construct(\phpbb\template\twig\environment $environment, \phpbb\event\dispatcher_interface $phpbb_dispatcher = null) + public function __construct(\phpbb\template\twig\environment $environment) { $this->environment = $environment; - $this->phpbb_dispatcher = $phpbb_dispatcher; + $this->phpbb_dispatcher = $this->environment->get_phpbb_dispatcher(); $template_event_priority_array = []; /** diff --git a/tests/console/user/base.php b/tests/console/user/base.php index 605493bd80..511cfff9a3 100644 --- a/tests/console/user/base.php +++ b/tests/console/user/base.php @@ -190,7 +190,7 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case 'autoescape' => false, ] ); - $twig_extension = new \phpbb\template\twig\extension($context, $twig, $this->language, $phpbb_dispatcher); + $twig_extension = new \phpbb\template\twig\extension($context, $twig, $this->language); $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); diff --git a/tests/controller/common_helper_route.php b/tests/controller/common_helper_route.php index 19e4bad2f1..0cd52b7d8b 100644 --- a/tests/controller/common_helper_route.php +++ b/tests/controller/common_helper_route.php @@ -119,7 +119,6 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_ $assets_bag = new \phpbb\template\assets_bag(); $context = new \phpbb\template\context(); $loader = new \phpbb\template\twig\loader(''); - $this->dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $this->config, @@ -128,7 +127,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_ $cache_path, null, $loader, - $this->dispatcher, + new \phpbb\event\dispatcher(), array( 'cache' => false, 'debug' => false, @@ -136,7 +135,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_database_test_ 'autoescape' => false, ) ); - $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, $twig, $this->user, $this->dispatcher))); + $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, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->extension_manager = new phpbb_mock_extension_manager( diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index 7dbf802ee5..fc1d1ef3be 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -87,7 +87,7 @@ class phpbb_email_parsing_test extends phpbb_test_case 'autoescape' => false, ) ); - $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang, $dispatcher); + $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang); $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 48566096c6..095efd3a69 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -117,7 +117,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $lang = new \phpbb\language\language($lang_loader); $this->user = new \phpbb\user($lang, '\phpbb\datetime'); - $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, $twig, $this->user, $phpbb_dispatcher))); + $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, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); } diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php index a762c9e026..5caae111ca 100644 --- a/tests/template/extension_test.php +++ b/tests/template/extension_test.php @@ -102,7 +102,6 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case $loader = new \phpbb\template\twig\loader([]); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -111,7 +110,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case $cache_path, null, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), [ 'cache' => false, 'debug' => false, @@ -127,7 +126,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case $cache_path, $this->user, [ - new \phpbb\template\twig\extension($context, $twig, $this->lang, $dispatcher), + new \phpbb\template\twig\extension($context, $twig, $this->lang), new \phpbb\template\twig\extension\avatar($avatar_helper), new \phpbb\template\twig\extension\config($config), new \phpbb\template\twig\extension\icon($this->user), diff --git a/tests/template/template_allfolder_test.php b/tests/template/template_allfolder_test.php index 88f23d4570..a495f8aff8 100644 --- a/tests/template/template_allfolder_test.php +++ b/tests/template/template_allfolder_test.php @@ -60,7 +60,6 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -69,7 +68,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case $cache_path, $this->extension_manager, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), array( 'cache' => false, 'debug' => false, @@ -77,7 +76,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user, $dispatcher)), $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, $twig, $this->user)), $this->extension_manager); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template_path = $this->test_path . '/templates'; diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index 3f005ae6ec..a9b1d39451 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -154,7 +154,6 @@ Zeta test event in all', $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -163,7 +162,7 @@ Zeta test event in all', $cache_path, $this->extension_manager, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), array( 'cache' => false, 'debug' => false, @@ -171,7 +170,7 @@ Zeta test event in all', 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user, $dispatcher)), $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, $twig, $this->user)), $this->extension_manager); $twig->setLexer(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_includecss_test.php b/tests/template/template_includecss_test.php index 603fa6c08c..a11f627c6f 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -46,7 +46,6 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -55,7 +54,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $cache_path, null, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), array( 'cache' => false, 'debug' => false, @@ -70,7 +69,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $twig, $cache_path, $this->user, - array(new \phpbb\template\twig\extension($context, $twig, $this->user, $dispatcher)), + array(new \phpbb\template\twig\extension($context, $twig, $this->user)), new phpbb_mock_extension_manager( __DIR__ . '/', array( diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index da9280c6eb..5224e24b71 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -97,7 +97,6 @@ class phpbb_template_template_test_case extends phpbb_test_case $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -106,7 +105,7 @@ class phpbb_template_template_test_case extends phpbb_test_case $cache_path, null, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), array( 'cache' => false, 'debug' => false, @@ -114,8 +113,7 @@ class phpbb_template_template_test_case extends phpbb_test_case 'autoescape' => false, ) ); - - $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $lang, $dispatcher))); + $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $lang))); $twig->setLexer(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 637a1af04a..214d3503c8 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -47,7 +47,6 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -56,7 +55,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $cache_path, null, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), array( 'cache' => false, 'debug' => false, @@ -64,7 +63,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat 'autoescape' => false, ) ); - $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, $twig, $this->user, $dispatcher))); + $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, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); } diff --git a/tests/template/twig_test.php b/tests/template/twig_test.php index a3bb071b0f..69280caa15 100644 --- a/tests/template/twig_test.php +++ b/tests/template/twig_test.php @@ -65,7 +65,6 @@ class twig_test extends \phpbb_test_case $loader = new \phpbb\template\twig\loader(''); $log = new \phpbb\log\dummy(); $assets_bag = new \phpbb\template\assets_bag(); - $dispatcher = new \phpbb\event\dispatcher(); $twig = new \phpbb\template\twig\environment( $assets_bag, $config, @@ -74,7 +73,7 @@ class twig_test extends \phpbb_test_case $cache_path, null, $loader, - $dispatcher, + new \phpbb\event\dispatcher(), [ 'cache' => false, 'debug' => false, @@ -82,7 +81,7 @@ class twig_test extends \phpbb_test_case 'autoescape' => false, ] ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user, $dispatcher))); + $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); } diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 7a1d7afa78..2ade7533cc 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -407,7 +407,7 @@ class phpbb_functional_test_case extends phpbb_test_case 'autoescape' => false, ] ); - $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang, $phpbb_dispatcher); + $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang); $container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($container); From 43cf7b73bdc7a59895cecc4c16bd14988de9a8c9 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 21 May 2025 10:47:27 +0700 Subject: [PATCH 15/16] [ticket/15214] Adjust event node logic PHPBB3-15214 --- phpBB/phpbb/template/twig/node/event.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/template/twig/node/event.php b/phpBB/phpbb/template/twig/node/event.php index badd1d5f62..4b506377a2 100644 --- a/phpBB/phpbb/template/twig/node/event.php +++ b/phpBB/phpbb/template/twig/node/event.php @@ -52,11 +52,8 @@ class event extends \Twig\Node\Node foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path) { $ext_namespace = str_replace('/', '_', $ext_namespace); - if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) - { - $priority_key = $this->template_event_priority_array[$ext_namespace][$location] ?? 0; - $template_events[$priority_key][] = $ext_namespace; - } + $priority_key = $this->template_event_priority_array[$ext_namespace][$location] ?? 0; + $template_events[$priority_key][] = $ext_namespace; } krsort($template_events); @@ -75,13 +72,16 @@ class event extends \Twig\Node\Node ->indent(); } - $compiler - ->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n") + if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) + { + $compiler + ->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n") - // We set the namespace lookup order to be this extension first, then the main path - ->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n") - ->write("\$this->env->loadTemplate(\$this->env->getTemplateClass('@{$ext_namespace}/{$location}.html'), '@{$ext_namespace}/{$location}.html')->display(\$context);\n") - ->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n"); + // We set the namespace lookup order to be this extension first, then the main path + ->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n") + ->write("\$this->env->loadTemplate(\$this->env->getTemplateClass('@{$ext_namespace}/{$location}.html'), '@{$ext_namespace}/{$location}.html')->display(\$context);\n") + ->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n"); + } if ($this->environment->isDebug()) { From ccbdfb49c7dbcbfd28d360f307ab7c2ef79f998a Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 21 May 2025 11:25:35 +0700 Subject: [PATCH 16/16] [ticket/15214] Adjust core event name and docblock PHPBB3-15214 --- .../phpbb/template/twig/tokenparser/event.php | 26 ++++++++++++++----- .../foo/bar/event/template_event_order.php | 2 +- .../bar/event/template_event_order_higher.php | 2 +- .../foo/foo/event/template_event_order.php | 2 +- .../foo/event/template_event_order_lower.php | 2 +- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index 2fb096d454..4514e4c698 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -42,7 +42,7 @@ class event extends \Twig\TokenParser\AbstractTokenParser * In case of equal priority values, corresponding template event listeners will be handled in default compilation order. * If not set, template event listener priority will be assigned to the value of 0. * - * @event core.twig_tokenparser_constructor + * @event core.twig_event_tokenparser_constructor * @var array template_event_priority_array Array with template event priority assignments per extension namespace * Usage: * '_' => [ @@ -50,17 +50,31 @@ class event extends \Twig\TokenParser\AbstractTokenParser * ], * * Example: - * 'phpbb_viglink' => [ - * 'event/acp_help_phpbb_stats_after' => 80, - * 'event/overall_footer_after' => 100, - * ], + * class template_event_order implements EventSubscriberInterface + * { + * static public function getSubscribedEvents() + * { + * return [ + * 'core.twig_event_tokenparser_constructor' => 'set_template_event_priority', + * ]; + * } + * + * public function set_template_event_priority($event) + * { + * $template_event_priority_array = $event['template_event_priority_array']; + * $template_event_priority_array['vendor_name'] = [ + * 'event/navbar_header_quick_links_after' => -1, + * ]; + * $event['template_event_priority_array'] = $template_event_priority_array; + * } + * } * * @since 4.0.0-a1 */ if ($this->phpbb_dispatcher) { $vars = ['template_event_priority_array']; - extract($this->phpbb_dispatcher->trigger_event('core.twig_tokenparser_constructor', compact($vars))); + extract($this->phpbb_dispatcher->trigger_event('core.twig_event_tokenparser_constructor', compact($vars))); } $this->template_event_priority_array = $template_event_priority_array; diff --git a/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php b/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php index 9cb6a9c71b..16589591c9 100644 --- a/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php +++ b/tests/functional/fixtures/ext/foo/bar/event/template_event_order.php @@ -23,7 +23,7 @@ class template_event_order implements EventSubscriberInterface static public function getSubscribedEvents() { return array( - 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + 'core.twig_event_tokenparser_constructor' => 'set_template_event_priority', ); } diff --git a/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php b/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php index 43d5c05be3..6d3978a2ad 100644 --- a/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php +++ b/tests/functional/fixtures/ext/foo/bar/event/template_event_order_higher.php @@ -23,7 +23,7 @@ class template_event_order implements EventSubscriberInterface static public function getSubscribedEvents() { return array( - 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + 'core.twig_event_tokenparser_constructor' => 'set_template_event_priority', ); } diff --git a/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php b/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php index 28652c23fc..09045e918e 100644 --- a/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php +++ b/tests/functional/fixtures/ext/foo/foo/event/template_event_order.php @@ -23,7 +23,7 @@ class template_event_order implements EventSubscriberInterface static public function getSubscribedEvents() { return array( - 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + 'core.twig_event_tokenparser_constructor' => 'set_template_event_priority', ); } diff --git a/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php b/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php index e360209481..d18eb85baf 100644 --- a/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php +++ b/tests/functional/fixtures/ext/foo/foo/event/template_event_order_lower.php @@ -23,7 +23,7 @@ class template_event_order implements EventSubscriberInterface static public function getSubscribedEvents() { return array( - 'core.twig_tokenparser_constructor' => 'set_template_event_priority', + 'core.twig_event_tokenparser_constructor' => 'set_template_event_priority', ); }