-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[EventDispatcher] Add InvokableEventSubscriberTrait helper trait for single-event subscription #33485
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
|
I'm sorry but I don't think this is an idea we should pursue. [edit: this comment is now obsolete] |
derrabus
left a comment
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.
Good idea. I would use that class instantly. 👍
Tests for the error cases are missing:
- Missing event parameter
- Missing/invalid type declaration
- Missing
__invoke()
For the same reasons, we could ban the abstract Also, developers can look into this class and learn how they can build subscribers without it. The reflection part aside, it's pretty straightforward. |
|
All this is exactly the same as writing: final public static function getSubscribedEvents(): array
{
return [MyEvent::class => ['__invoke', 0]];
}Saving writing this is not worth the drawbacks to me. |
|
What about a new interface for single-event subscription? interface EventListenerInterface
{
public static function getEventName();
public static function getMethodName();
public static function getPriority();
}and with auto-configuration would make the current event listener approach even easier maybe? |
|
Related to #33485 (comment), the |
c20950c to
d347385
Compare
|
Talking with @nicolas-grekas on Slack, their architectural objections can be removed using traits, done!, so let's focus on the teachability objections now :) |
|
I will add more tests when the objections have been removed, I hope that :) |
59ade0a to
badd322
Compare
|
If you're ok with my last review, we could name the trait |
1a83a39 to
6031c96
Compare
|
@nicolas-grekas comments addressed where possible, how does it look to you now? |
6031c96 to
a0c0feb
Compare
a020d1f to
3936532
Compare
src/Symfony/Component/EventDispatcher/InvokableEventSubscriberTrait.php
Outdated
Show resolved
Hide resolved
|
Update Removed class MyEventSubscriber implements EventSubscriberInterface
{
use InvokableEventSubscriberTrait;
public function __invoke(MyEvent $event): void
{
// ...
}
}If it does not fit your needs, because the event name is not the FQCN or it has a different priority, then you should not use this feature and would implement the |
3936532 to
39f2088
Compare
src/Symfony/Component/EventDispatcher/InvokableEventSubscriberTrait.php
Outdated
Show resolved
Hide resolved
401e7e4 to
61af258
Compare
src/Symfony/Component/EventDispatcher/InvokableEventSubscriberTrait.php
Outdated
Show resolved
Hide resolved
b25aa93 to
c3e9e91
Compare
src/Symfony/Component/EventDispatcher/InvokableEventSubscriberTrait.php
Outdated
Show resolved
Hide resolved
nicolas-grekas
left a comment
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 a request to improve the description in the docblock)
src/Symfony/Component/EventDispatcher/InvokableEventSubscriberTrait.php
Outdated
Show resolved
Hide resolved
98980e2 to
d1f0aea
Compare
|
For using this you have to use a given trait + implement a given interface + eventually declare some static properties - vs implementing Given this basically allows for autoconfigured event listeners, couldn't we just add a marker interface instead? That could allow to keep the reflection-based logic in the compiler pass only and could, I think, reduce the learning curve. |
|
@chalasr I'm personally against adding a new abstraction when we already have all we need. That'd be another option, thus another choice to make for ppl, but learn about them before, document it, etc. |
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.
$listenerEvent and $listenerPriority is also a new concept people need to learn now and it will split knowledge and expectations of developers. I'd be ok to have this helper trait but without those static properties. If someone needs to adjust that, they should use the common and establisted way of implementing getSubscribedEvents
Also those static properties can easily introduce bugs because if you have a typo, it won't be even detectable by static analysis or at container compilation. It would just be buggy at runtime.
|
The more we talk about this one, the more I think we should not add this to core. I'm now 👎 . |
|
Let's close then, it's not worth it for me anymore. |
…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.
Everything is in the linked issue.