Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fa5b7eb

Browse filesBrowse files
committed
[DI] Allow for invokable event listeners
1 parent 0032368 commit fa5b7eb
Copy full SHA for fa5b7eb

File tree

3 files changed

+61
-0
lines changed
Filter options

3 files changed

+61
-0
lines changed

‎src/Symfony/Component/EventDispatcher/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/EventDispatcher/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added support for invokable event listeners tagged with `kernel.event_listener` by default
8+
49
4.0.0
510
-----
611

‎src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public function process(ContainerBuilder $container)
6868
'/[^a-z0-9]/i',
6969
), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
7070
$event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
71+
72+
if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) {
73+
$event['method'] = '__invoke';
74+
}
7175
}
7276

7377
$definition->addMethodCall('addListener', array($event['event'], array(new ServiceClosureArgument(new Reference($id)), $event['method']), $priority));

‎src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,47 @@ public function testEventSubscriberUnresolvableClassName()
166166
$registerListenersPass = new RegisterListenersPass();
167167
$registerListenersPass->process($container);
168168
}
169+
170+
public function testInvokableEventListener()
171+
{
172+
$container = new ContainerBuilder();
173+
$container->register('foo', \stdClass::class)->addTag('kernel.event_listener', array('event' => 'foo.bar'));
174+
$container->register('bar', InvokableListenerService::class)->addTag('kernel.event_listener', array('event' => 'foo.bar'));
175+
$container->register('baz', InvokableListenerService::class)->addTag('kernel.event_listener', array('event' => 'event'));
176+
$container->register('event_dispatcher', \stdClass::class);
177+
178+
$registerListenersPass = new RegisterListenersPass();
179+
$registerListenersPass->process($container);
180+
181+
$definition = $container->getDefinition('event_dispatcher');
182+
$expectedCalls = array(
183+
array(
184+
'addListener',
185+
array(
186+
'foo.bar',
187+
array(new ServiceClosureArgument(new Reference('foo')), 'onFooBar'),
188+
0,
189+
),
190+
),
191+
array(
192+
'addListener',
193+
array(
194+
'foo.bar',
195+
array(new ServiceClosureArgument(new Reference('bar')), '__invoke'),
196+
0,
197+
),
198+
),
199+
array(
200+
'addListener',
201+
array(
202+
'event',
203+
array(new ServiceClosureArgument(new Reference('baz')), 'onEvent'),
204+
0,
205+
),
206+
),
207+
);
208+
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
209+
}
169210
}
170211

171212
class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
@@ -177,3 +218,14 @@ public static function getSubscribedEvents()
177218
);
178219
}
179220
}
221+
222+
class InvokableListenerService
223+
{
224+
public function __invoke()
225+
{
226+
}
227+
228+
public function onEvent()
229+
{
230+
}
231+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.