[ticket/15214] Address code review issues, add equal priority events test

PHPBB3-15214
This commit is contained in:
rxu 2025-06-15 01:00:37 +07:00
parent ccbdfb49c7
commit 1d7543c778
No known key found for this signature in database
GPG key ID: 955F0567380E586A
5 changed files with 64 additions and 81 deletions

View file

@ -46,20 +46,19 @@ class event extends \Twig\Node\Node
$location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
$template_events = [];
$template_event_listeners = [];
// 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);
$priority_key = $this->template_event_priority_array[$ext_namespace][$location] ?? 0;
$template_events[$priority_key][] = $ext_namespace;
$priority_key = intval($this->template_event_priority_array[$ext_namespace][$location] ?? 0);
$template_event_listeners[$priority_key][] = $ext_namespace;
}
krsort($template_events);
krsort($template_event_listeners);
foreach ($template_events as $events)
{
foreach ($events as $ext_namespace)
$template_event_listeners = array_merge(...$template_event_listeners);
foreach ($template_event_listeners as $ext_namespace)
{
if ($this->environment->isDebug())
{
@ -92,4 +91,3 @@ class event extends \Twig\Node\Node
}
}
}
}

View file

@ -18,9 +18,6 @@ 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;
@ -32,49 +29,21 @@ class event extends \Twig\TokenParser\AbstractTokenParser
public function __construct(\phpbb\template\twig\environment $environment)
{
$this->environment = $environment;
$this->phpbb_dispatcher = $this->environment->get_phpbb_dispatcher();
$phpbb_dispatcher = $this->environment->get_phpbb_dispatcher();
$template_event_priority_array = [];
/**
* 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.
* Allows assigning priority to template event listeners
*
* @event core.twig_event_tokenparser_constructor
* @var array template_event_priority_array Array with template event priority assignments per extension namespace
* Usage:
* '<author>_<extension_name>' => [
* 'event/<template_event_name>' => priority_number,
* ],
*
* Example:
* 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)
if ($phpbb_dispatcher)
{
$vars = ['template_event_priority_array'];
extract($this->phpbb_dispatcher->trigger_event('core.twig_event_tokenparser_constructor', compact($vars)));
extract($phpbb_dispatcher->trigger_event('core.twig_event_tokenparser_constructor', compact($vars)));
}
$this->template_event_priority_array = $template_event_priority_array;

View file

@ -58,9 +58,9 @@ class phpbb_functional_extension_template_event_order_test extends phpbb_functio
}
/**
* Check a controller for extension foo/bar.
* Check extensions template event listener prioritizing
*/
public function test_template_event_order()
public function test_different_template_event_priority()
{
global $phpbb_root_path;
@ -88,4 +88,18 @@ class phpbb_functional_extension_template_event_order_test extends phpbb_functio
$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());
}
/**
* Check extensions template event listener equal (default - 0) priority rendering
* Should render in the order of reading listener files from the filesystem
*/
public function test_same_template_event_priority()
{
global $phpbb_root_path;
$crawler = self::request('GET', 'index.php');
// Ensure foo/bar template event goes before foo/foo one (assuming they have been read from the filesystem in alphabetical order)
$this->assertStringContainsString('FOO_BAR_FORUMLIST_BODY_BEFORE', $crawler->filter('p[id*="forumlist_body_before"]')->eq(0)->text());
$this->assertStringContainsString('FOO_FOO_FORUMLIST_BODY_BEFORE', $crawler->filter('p[id*="forumlist_body_before"]')->eq(1)->text());
}
}

View file

@ -0,0 +1 @@
<p id="foo_bar_forumlist_body_before">{{ lang('FOO_BAR_FORUMLIST_BODY_BEFORE') }}</p>

View file

@ -0,0 +1 @@
<p id="foo_foo_forumlist_body_before">{{ lang('FOO_FOO_FORUMLIST_BODY_BEFORE') }}</p>