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
[DependencyInjection] Fix register event listeners compiler pass #36811
[DependencyInjection] Fix register event listeners compiler pass #36811
Conversation
/cc @dmaicher maybe? |
Oups, I meant /cc @derrabus actually |
What are the chances that this RFC got proposed at the same time as this PR? https://wiki.php.net/rfc/stable_sorting 😂 |
The PR removes the piece of code that removes the event aliases parameter from the container. @nicolas-grekas Do you recall why the event aliases parameter is removed from the container by the pass? It has been there ever since the aliases have been introduced with 75369da. |
Oh, yes: non-removed parameters end up being inlined in the dumped container and available at runtime. Since this is not desired, the parameter is removed. I still think this parameter should be removed before the container is dumped. @X-Coder264 any other idea that would preserve the removal? |
@nicolas-grekas The only other solution that I see is to make the constructor |
The default would be to use the current parameter, and you would pass |
The default would be |
Not possible, that would be a BC break. |
What would exactly break in that scenario? Changing the priority of the passes could also potentially break some applications. That's why I went with the original approach to just remove the parameter removal logic, because that is a 100% safe code change and like I've said in the original post I don't see any issue with the parameter being left in the compiled container. Yes, maybe it's not ideal but it does not hurt anyone if that parameter remains in the container. |
Aliases would break for ppl that use the pass directly. Another option: add a new compiler pass that gets a list of parameters to remove and register it last, after removing passes. That'd be a new feature of course, so that the fix on your side would be to adjust the priority. |
Or I can add a new constructor
By new feature you mean it'd have to go to |
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.
OK, let's go this way.
We might want to be able to deprecate fetching some parameters at runtime so that we can remove them later on.
Wouldn't that be a feature worth investigating? Parameters that are available at compile-time, but won't be compiled into the dumped container? |
Issue created at #36831 |
Thank you @X-Coder264. |
I've wanted to use the simpler event listener registration syntax (https://symfony.com/blog/new-in-symfony-4-4-simpler-event-listeners) in my project and it didn't work so I'm sending this fix.
We use the
KnpPaginatorBundle
bundle which also calls theRegisterListenersPass
compiler pass in order to register with the event dispatcher their custom tags for listeners and subscribers (knp_paginator.listener
andknp_paginator.subscriber
).Their compiler pass is
TYPE_BEFORE_REMOVING
and priority zero which is the same type and priority as the pass that gets added by FrameworkBundle. Since both the type and priority is the same the order of execution isundefined
(because that is how regular sort behaves in PHP which is used by default bykrsort
) and theRegisterListenersPass
currently removes theeventAliasesParameter
parameter from the container if it is set (which is set here). So what happens in my app is that the Knp compiler pass runs first, theevent_dispatcher.event_aliases
parameter is removed and then the FrameworkBundle registered compiler pass runs and since the aliases are not present anymore the events do not get aliased properly. The event dispatcher service in the compiled container looks like:instead of the expected
This means that my listener never gets called on the kernel request event.
Another potential fix would be to adjust the Knp compiler pass priority, but seeing as that would fix only that bundle (who knows how many bundles out there have the same problem) and that I don't see any drawback in letting the
event_dispatcher.event_aliases
parameter stay in the container I think that this is better to fix here.