-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Allow to omit the event name when registering listeners #33851
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
Conversation
cc @yceruto who has worked on the previous PR. |
49a313c
to
58b316d
Compare
src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php
Outdated
Show resolved
Hide resolved
58b316d
to
f6b6966
Compare
My 2cts: when and only when the "event" attribute is missing:
|
Fine by me, although personally I'd probably use the attribute or a subscriber if I wanted to change the priority. services:
App\EventListener\:
resource: ../src/EventListener/*
tags: [kernel.event_listener]
App\EventListener\VeryImportantListener:
tags: [{name: kernel.event_listener, priority: 1337}]
Makes sense, but I'd prefer to throw here. The developer should know that we don't want to process a piece of configuration they gave us.
That would render the following configuration invalid, that would work with my current implementation. services:
App\EventListener\MyListener:
tags:
- {name: kernel.event_listener, method: onFoo}
- {name: kernel.event_listener, method: onBar} What would be the reason for that limitation? |
I insist on this one as that allows configuring a directory with the tag, defaulting to listeners, but allowing to augment them to subscribers, all that without touching the configuration files. Fine for the rest. |
f6b6966
to
da25786
Compare
Fair point!
All right then. We can discuss/implement the functionality around |
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.
(with minor wording suggestion)
src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php
Outdated
Show resolved
Hide resolved
da25786
to
6f32584
Compare
Thank you @derrabus. |
…gistering listeners (derrabus) This PR was merged into the 4.4 branch. Discussion ---------- [EventDispatcher] Allow to omit the event name when registering listeners | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #33453 (kind of) | License | MIT | Doc PR | TODO After #30801 and #33485, this is another attempt at taking advantage of FQCN events for simplifying the registration of event listeners by inferring the event name from the parameter type declaration of the listener. This is my last attempt, I promise. 🙈 This time, I'd like to make the `event` attribute of the `kernel.event_listener` tag optional. This would allow us to build listeners like the following one without adding any attributes to the `kernel.event_listener` tag. ```php namespace App\EventListener; final class MyRequestListener { public function __invoke(RequestEvent $event): void { // do something } } ``` This in turn allows us to register a whole namespace of such listeners without having to configure each listener individually: ```YAML services: App\EventListener\: resource: ../src/EventListener/* tags: [kernel.event_listener] ``` Commits ------- 6f32584 [EventDispatcher] Allow to omit the event name when registering listeners.
Can it be autowired with some class MyListener implements EventListenerInterface, PrioritizedEventInterface
{
public function __invove(SomeEvent $event) {}
public static function getPriority()
{
return 10;
}
} This way we avoid |
Listeners can be autowired and they don't require any interface for this. What you probably meant is autoconfiguration: The First of all, we cannot create an interface that includes the Secondly, my intention was to keep this feature as simple as possible because we already have event subscribers for more complex setups. Subscribers offer that kind of autoconfiguration and they also allow you to specify a priority. What you can do in your application code is create an
No, it wouldn't (because we cannot add Related: Haehnchen/idea-php-symfony2-plugin#1366
No, you would only see the the listeners that implement the marker interface. The charm of event listeners (compared to subscribers) is that they don't require any interface. |
You are right, my bad. I already (ab)use it in Kernel like this and still confuse names in a rush:
Sorry, I meant unused method
Exactly. We already have marker interfaces: https://github.com/symfony/messenger/blob/master/Handler/MessageHandlerInterface.php I think event listeners should do the same. Anyway I can always create my own like I already do but was hoping for standardized version. |
Registered listeners with:
Then tried to use listener for
Using SF 4.4.5 |
@reanim8ed you've probably spotted a bug, but can you create a new issue to discuss about it? https://github.com/symfony/symfony/issues/new?labels=Bug&template=1_Bug_report.md |
Sure, created here: 36840 |
No, if you don't specify the event, only |
After #30801 and #33485, this is another attempt at taking advantage of FQCN events for simplifying the registration of event listeners by inferring the event name from the parameter type declaration of the listener. This is my last attempt, I promise. 🙈
This time, I'd like to make the
event
attribute of thekernel.event_listener
tag optional. This would allow us to build listeners like the following one without adding any attributes to thekernel.event_listener
tag.This in turn allows us to register a whole namespace of such listeners without having to configure each listener individually: