-
-
Notifications
You must be signed in to change notification settings - Fork 944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ticket/15214] Add event & functionality for assigning template event priority #6571
base: master
Are you sure you want to change the base?
Conversation
… 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
Example event usage: (unsure about the name) public function twig_tokenparser_constructor($event)
{
$template_event_priority_array = $event['template_event_priority_array'];
$template_event_priority_array += [
'toxyy_postformtemplates' => [
'event/overall_header_head_append' => 1000
]
];
$event['template_event_priority_array'] = $template_event_priority_array;
} With this, my template event is called before any others. Normal behavior would have it placed on the bottom of this list. As stated in the commit message, priority works like in Symfony events, the higher number will be compiled first. If two events have the same priority, then the last one added will be executed last - this differs from what's stated in my initial commit message, sorry about that, I was thinking about it wrong. Reason being is that for an array: [
'0' => 'first_event',
'1' => 'second_event',
'2' => 'third_event',
'3' => 'fourth_event'
] if your priority is [
'0' => 'first_event',
'1' => 'second_event',
'2' => 'priority_set_event',
'3' => 'third_event',
'4' => 'fourth_event'
] And then if another event has a priority of [
'0' => 'first_event',
'1' => 'second_event',
'2' => 'another_priority_set_event',
'3' => 'priority_set_event'.
'4' => 'third_event',
'5' => 'fourth_event'
] And because the higher numbers get executed first (krsort), it will get compiled after the previously set template event for the same priority. This is an edge case though. Not sure how Symfony handles this. Template events by default are parsed in alphabetical order by extension namespace. If no one uses the event, then template events will execute in the same order as they do now. |
Add new dispatch parameter to the template\twig\extension call PHPBB3-15214
Add new dispatch parameter to the template\twig\extension call PHPBB3-15214
Add new dispatch parameter to the template\twig\extension calls PHPBB3-15214
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
I don't know if the compiler object's write methods change the state of the compiler object itself. If so then Also unsure if it's necessary to set Also unsure about proper code formatting for the anonymous functions, I don't have a phpstorm license so I can't run it through that to check, and there are no anonymous functions as far as I can find within core so I'm not sure if they're accounted for with code styling. Hopefully this is fine on the 3.3 branch? 😄 |
It doesn't need to be set at the top and makes more sense to set it at the bottom where it's used. Also removes a redundant check PHPBB3-15214
extract($this->phpbb_dispatcher->trigger_event('core.twig_tokenparser_constructor', compact($vars))); | ||
} | ||
|
||
$this->template_event_priority_array = $template_event_priority_array; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably makes sense to unset($template_event_priority_array);
as it's not used anymore.
Adds similar usage examples like the event core.permissions has PHPBB3-15214
All I did was change what's within a docblock comment... that test should not have failed |
Make docblock look a bit cleaner and restart the tests PHPBB3-15214
That's random network issue, unrelated. |
No matter, I think the format of this docblock update is cleaner anyways |
To sum my all above comments, I feel like simplified all the stuff and below code works just fine. public function compile(\Twig\Compiler $compiler)
{
$compiler->addDebugInfo($this);
$location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
$template_events = [];
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;
}
krsort($template_events);
foreach ($template_events as $priority => $events)
{
foreach ($events as $ext_namespace)
{
if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html'))
{
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('@{$ext_namespace}/{$location}.html')->display(\$context);\n")
->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n");
if ($this->environment->isDebug())
{
$compiler
->outdent()
->write("}\n\n");
}
}
}
}
} |
I guess the issue with the commit messages here is an empty line needed between the explanation line and |
There are also some other failures like here: I'll do a force push to fix the messages in a bit. |
…tests PHPBB3-15214
Putting this here will have the loop run the same amount of times as default. Without this, the loop runs too many extra times. PHPBB3-15214
@rxu Yeah looks like it doesn't like the added tests right now I'm guessing that it's because |
Added this when I initially tested the if block move. but I forgot to add it in the actual commit. If this isn't moved, no template events work, and the tests fail. PHPBB3-15214
That's weird as everything works when installing both of the test extensions on a local test board and yaml for both of EDIT: I guess I've got what's the issue, let me test something. |
PHPBB3-15214
@rxu That solved the errors we were seeing before, but we've got a new set of failures 😓 |
I'm afraid Win+PHP 8.3 errors aren't related to this PR. Will dig into it a bit. |
Tests passed earlier in this PR so I'm not sure what changed. |
Also, I've been thinking. Currently, all events are added to the same priority of 0 in the order that they would originally be run. I'm not sure how Symfony events work exactly, but would there be any scenario in which someone would want to place a template event in the middle of those? Technically, that would mean that they cannot all be at priority key 0. That is also installation dependent, as the priority keys would change based on what extensions you have enabled, assuming multiple extensions use the same events. Would it be beneficial to somehow make it so you could say "if extension X is installed, make my template event compile before theirs"? That could be solved with a priority key, yes, but lets say you also want all other extensions to execute before yours as well - meaning you specifically want your template event to compile right before extension X's event of the same name (though anyone else doing the same would run into default behavior, of course). Let's take navlinks for example. Maybe you want to place your link right before the other nav links of extension X, but you don't want it to be the first links added. With a priority key, the only way to get it before extension X would be to make it compile first, meaning your links would be first. That doesn't seem versatile. Is what I'm explaining making sense, and is that a valid use case to try to add functionality for? |
I'm not sure I fully got the point in question. |
Ah yes you could do that. I guess that case would be solved then. |
And the error is:
That's weird again as this template event shouldn't be involved there. |
If that said that it was for Why is it looking in |
The issue revealed by this PR is the twig compiled cache folder was never purged in Windows test environment. Will send a fix PR to this PR soon. |
Purge Twig compiled cache in Windows. Set appropriate folder access control options to do that. PHPBB3-15214
😄 |
Hmm. Tests were passing successfully here https://github.com/rxu/phpbb3/actions/runs/7157430146 on Win+PHP 8.3 with this code. One more weirdness :) Okay, will look into it furher. |
@toxyy Could you try adding these 3 lines to |
Adding per rxu's recommendation PHPBB3-15214
@rxu same error 😄 |
Nice. EDIT: the problem is that twig cache folder is not being purged under Windows for some reason. I'm out of ideas for now. |
@marc1706 maybe you have some ideas? Not sure who else has worked with the tests a lot |
PHPBB3-15214
@rxu It worked this time 😄 thanks for the help |
Great. The issue is that after the |
As noted on discord, the PR might result in BC breaks for some and adds quite a bit of changes to behavior and introduces new functionality so this shouldn't aim for 3.3.x but rather 4.0. |
@toxyy mind to rebase vs |
@rxu there are a lot of conflicts that I'll need to go through, but sure. I also need to change the event's since version. |
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
Checklist:
Tracker ticket:
https://tracker.phpbb.com/browse/PHPBB3-15214