Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DI] Allow for invokable event listeners#25275
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
dunglas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
❤️
Simperfit commentedDec 3, 2017
I will use this one ! |
| $event['method'] ='on'.preg_replace_callback(array( | ||
| '/(?<=\b)[a-z]/i', | ||
| '/[^a-z0-9]/i', | ||
| ),function ($matches) {returnstrtoupper($matches[0]); },$event['event']); |
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.
I didn't know there is such magic default logic.
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.
true, yet convenient enough to keep IMHO, or at least didnt want to spoil this feat. along with a deprecation.
| publicfunctiontestInvokableEventListener() | ||
| { | ||
| $container =newContainerBuilder(); | ||
| $container->register('foo','stdClass')->addTag('kernel.event_listener',array('event' =>'foo.bar')); |
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.
\stdClass::class
| ),function ($matches) {returnstrtoupper($matches[0]); },$event['event']); | ||
| $event['method'] =preg_replace('/[^a-z0-9]/i','',$event['method']); | ||
| if (null !== ($class =$container->getDefinition($id)->getClass()) && !method_exists($class,$event['method']) &&method_exists($class,'__invoke')) { |
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.
we should use $container->getReflectionClass() instead
this will fix the current logic that misses resource tracking
nicolas-grekas commentedDec 29, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I'm "-0": I don't see the benefit of this. Naming methods helps the reader, and is not a strong requirement. |
ro0NL commentedDec 29, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
naming classes too :) this allows me, the class author, to write IMHO pure event listeners and let users configure as usual. That is win-win. |
fabpot commentedJan 19, 2018
Thank you@ro0NL. |
This PR was merged into the 4.1-dev branch.Discussion----------[DI] Allow for invokable event listeners| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->| License | MIT| Doc PR | symfony/symfony-docs#... <!--highly recommended for new features-->Inspired by#24637 /#25259. This adds invokable support for event listeners :)```yamlSome\Foo: tags: [{ name: kernel.event_listener, event: kernel.request }]``````phpclass Foo { public function __invoke(GetResponseEvent $event) { }}```Commits-------fa5b7eb [DI] Allow for invokable event listeners
Uh oh!
There was an error while loading.Please reload this page.
Inspired by#24637 /#25259. This adds invokable support for event listeners :)