10000 [DI] Allow for invokable event listeners by ro0NL · Pull Request #25275 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Allow for invokable event listeners #25275

8000
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 8000 src/Symfony/Component/EventDispatcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* added support for invokable event listeners tagged with `kernel.event_listener` by default

4.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

$event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);

if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) {
$event['method'] = '__invoke';
}
}

$definition->addMethodCall('addListener', array($event['event'], array(new ServiceClosureArgument(new Reference($id)), $event['method']), $priority));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,47 @@ public function testEventSubscriberUnresolvableClassName()
$registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($container);
}

public function testInvokableEventListener()
{
$container = new ContainerBuilder();
$container->register('foo', \stdClass::class)->addTag('kernel.event_listener', array('event' => 'foo.bar'));
$container->register('bar', InvokableListenerService::class)->addTag('kernel.event_listener', array('event' => 'foo.bar'));
$container->register('baz', InvokableListenerService::class)->addTag('kernel.event_listener', array('event' => 'event'));
$container->register('event_dispatcher', \stdClass::class);

$registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($container);

$definition = $container->getDefinition('event_dispatcher');
$expectedCalls = array(
array(
'addListener',
array(
'foo.bar',
array(new ServiceClosureArgument(new Reference('foo')), 'onFooBar'),
0,
),
),
array(
'addListener',
array(
'foo.bar',
array(new ServiceClosureArgument(new Reference('bar')), '__invoke'),
0,
),
),
array(
'addListener',
array(
'event',
array(new ServiceClosureArgument(new Reference('baz')), 'onEvent'),
0,
),
),
);
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
}
}

class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expand All @@ -177,3 +218,14 @@ public static function getSubscribedEvents()
);
}
}

class InvokableListenerService
{
public function __invoke()
{
}

public function onEvent()
{
}
}
0