Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39a89a7

Browse files
committed
[EventDispatcher] Add EventListenerCaller
1 parent 93139f6 commit 39a89a7

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class EventDispatcher implements EventDispatcherInterface
2929
{
3030
private $listeners = array();
3131
private $sorted = array();
32+
private $listenerCaller;
3233

3334
/**
3435
* {@inheritdoc}
@@ -155,6 +156,11 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
155156
}
156157
}
157158

159+
public function setListenerCaller(EventListenerCallerInterface $listenerCaller)
160+
{
161+
$this->listenerCaller = $listenerCaller;
162+
}
163+
158164
/**
159165
* Triggers the listeners of an event.
160166
*
@@ -167,12 +173,27 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
167173
*/
168174
protected function doDispatch($listeners, $eventName, Event $event)
169175
{
176+
$listenerCaller = $this->getListenerCaller();
177+
170178
foreach ($listeners as $listener) {
171179
if ($event->isPropagationStopped()) {
172180
break;
173181
}
174-
call_user_func($listener, $event, $eventName, $this);
182+
183+
$listenerCaller->call($listener, $event, $eventName, $this);
184+
}
185+
}
186+
187+
/**
188+
* @return EventListenerCallerInterface
189+
*/
190+
private function getListenerCaller()
191+
{
192+
if (null === $this->listenerCaller) {
193+
$this->listenerCaller = new StandardEventListenerCaller();
175194
}
195+
196+
return $this->listenerCaller;
176197
}
177198

178199
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher;
13+
14+
/**
15+
*
16+
*/
17+
interface EventListenerCallerInterface
18+
{
19+
/**
20+
* @param callable $listener
21+
* @param Event $event
22+
* @param string $eventName
23+
* @param EventDispatcherInterface $eventDispatcher
24+
*/
25+
public function call(callable $listener, Event $event, $eventName, EventDispatcherInterface $eventDispatcher);
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher;
13+
14+
/**
15+
*
16+
*/
17+
final class StandardEventListenerCaller implements EventListenerCallerInterface
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function call(callable $listener, Event $event, $eventName, EventDispatcherInterface $eventDispatcher)
23+
{
24+
$listener($event, $eventName, $eventDispatcher);
25+
}
26+
}

src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,35 @@
1010
*/
1111

1212
namespace Symfony\Component\EventDispatcher\Tests;
13-
13+
use Symfony\Component\EventDispatcher\Event;
1414
use Symfony\Component\EventDispatcher\EventDispatcher;
15+
use Symfony\Component\EventDispatcher\EventListenerCallerInterface;
1516

1617
class EventDispatcherTest extends AbstractEventDispatcherTest
1718
{
1819
protected function createEventDispatcher()
1920
{
2021
return new EventDispatcher();
2122
}
23+
24+
public function testCustomEventListenerCaller()
25+
{
26+
$listenerCallerMock = $this->getMockForAbstractClass(EventListenerCallerInterface::class);
27+
$listenerCallerMock
28+
->expects($this->once())
29+
->method('call')
30+
->will($this->returnCallback(function ($listener, Event $event) {
31+
$listener($event);
32+
}));
33+
34+
$dispatcher = new EventDispatcher();
35+
$dispatcher->setListenerCaller($listenerCallerMock);
36+
$dispatcher->addListener('foo', function (Event $event) {
37+
$event->stopPropagation();
38+
});
39+
40+
$event = $dispatcher->dispatch('foo', new Event());
41+
42+
$this->assertTrue($event->isPropagationStopped());
43+
}
2244
}

0 commit comments

Comments
 (0)