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

Skip to content

[EventDispatcher] Add listener caller extension point #18873

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 22 additions & 1 deletion src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class EventDispatcher implements EventDispatcherInterface
{
private $listeners = array();
private $sorted = array();
private $listenerCaller;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -155,6 +156,11 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
}
}

public function setListenerCaller(EventListenerCallerInterface $listenerCaller)
{
$this->listenerCaller = $listenerCaller;
}

/**
* Triggers the listeners of an event.
*
Expand All @@ -167,12 +173,27 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
*/
protected function doDispatch($listeners, $eventName, Event $event)
{
$listenerCaller = $this->getListenerCaller();

foreach ($listeners as $listener) {
if ($event->isPropagationStopped()) {
break;
}
call_user_func($listener, $event, $eventName, $this);

$listenerCaller->call($listener, $event, $eventName, $this);
}
}

/**
* @return EventListenerCallerInterface
*/
private function getListenerCaller()
{
if (null === $this->listenerCaller) {
$this->listenerCaller = new StandardEventListenerCaller();
}

return $this->listenerCaller;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher;

/**
*
*/
interface EventListenerCallerInterface
{
/**
* @param callable $listener
* @param Event $event
* @param string $eventName
* @param EventDispatcherInterface $eventDispatcher
*/
public function call(callable $listener, Event $event, $eventName, EventDispatcherInterface $eventDispatcher);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher;

/**
*
*/
final class StandardEventListenerCaller implements EventListenerCallerInterface
{
/**
* {@inheritdoc}
*/
public function call(callable $listener, Event $event, $eventName, EventDispatcherInterface $eventDispatcher)
{
call_user_func($listener, $event, $eventName, $eventDispatcher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,35 @@

namespace Symfony\Component\EventDispatcher\Tests;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventListenerCallerInterface;

class EventDispatcherTest extends AbstractEventDispatcherTest
{
protected function createEventDispatcher()
{
return new EventDispatcher();
}

public function testCustomEventListenerCaller()
{
$listenerCallerMock = $this->getMockForAbstractClass(EventListenerCallerInterface::class);
$listenerCallerMock
->expects($this->once())
->method('call')
->will($this->returnCallback(function ($listener, Event $event) {
$listener($event);
}));

$dispatcher = new EventDispatcher();
$dispatcher->setListenerCaller($listenerCallerMock);
$dispatcher->addListener('foo', function (Event $event) {
$event->stopPropagation();
});

$event = $dispatcher->dispatch('foo', new Event());

$this->assertTrue($event->isPropagationStopped());
}
}