Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2018
Merged

[DI] Allow for invokable event listeners #25275

merged 1 commit into from
Jan 19, 2018

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Dec 2, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #...
License MIT
Doc PR symfony/symfony-docs#...

Inspired by #24637 / #25259. This adds invokable support for event listeners :)

Some\Foo:
    tags: [{ name: kernel.event_listener, event: kernel.request }]
class Foo {
    public function __invoke(GetResponseEvent $event) { }
}

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@Simperfit
Copy link
Contributor

I will use this one !

@@ -68,6 +68,10 @@ public function process(ContainerBuilder $container)
'/[^a-z0-9]/i',
), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
Copy link
Contributor

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.

Copy link
Contributor Author

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.

public function testInvokableEventListener()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->addTag('kernel.event_listener', array('event' => 'foo.bar'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\stdClass::class

@@ -68,6 +68,10 @@ public function process(ContainerBuilder $container)
'/[^a-z0-9]/i',
), function ($matches) { return strtoupper($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')) {
Copy link
Member

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 nicolas-grekas added this to the 4.1 milestone Dec 4, 2017
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Dec 29, 2017

I'm "-0": I don't see the benefit of this. Naming methods helps the reader, and is not a strong requirement.

@ro0NL
Copy link
Contributor Author

ro0NL commented Dec 29, 2017

Naming methods helps the reader

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
Copy link
Member

fabpot commented Jan 19, 2018

Thank you @ro0NL.

@fabpot fabpot merged commit fa5b7eb into symfony:master Jan 19, 2018
fabpot added a commit that referenced this pull request Jan 19, 2018
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 :)

```yaml
Some\Foo:
    tags: [{ name: kernel.event_listener, event: kernel.request }]
```

```php
class Foo {
    public function __invoke(GetResponseEvent $event) { }
}
```

Commits
-------

fa5b7eb [DI] Allow for invokable event listeners
@ro0NL ro0NL deleted the eventlistener branch January 28, 2018 14:53
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.