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

Skip to content

[EventDispatcher] Allow EventDispatcher to dispatch anonymous events #21962

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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;

/**
* Dispatch events without the need to specify an event name.
*
* @author Daniel Santamaría <[email protected]>
*/
interface AnonymousEventDispatcherInterface
{
/**
* Dispatches an event to all registered listeners.
*
* @param object $event The event to pass to the event handlers/listeners
*/
public function dispatchAnonymousEvent($event);
Copy link
Member

Choose a reason for hiding this comment

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

why removing the event name ? This limits the use cases, as it forces creating a new class for each place dispatching an event as the event class is used as event identifier.

}
55 changes: 54 additions & 1 deletion src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
* @author Fabien Potencier <[email protected]>
* @author Jordi Boggiano <[email protected]>
* @author Jordan Alliot <[email protected]>
* @author Daniel Santamaría <[email protected]>
*/
class EventDispatcher implements EventDispatcherInterface
class EventDispatcher implements EventDispatcherInterface, AnonymousEventDispatcherInterface, EventPropagationTrackerInterface
{
private $listeners = array();
private $sorted = array();
private $stoppedEvents = array();

/**
* {@inheritdoc}
Expand All @@ -46,6 +48,20 @@ public function dispatch($eventName, Event $event = null)
return $event;
}

/**
* {@inheritdoc}
*/
public function dispatchAnonymousEvent($event)
{
if (!is_object($event)) {
throw new \InvalidArgumentException(sprintf('$event must be an object ("%s" given)', gettype($event)));
}

if ($listeners = $this->getListeners(get_class($event))) {
$this->doAnonymousEventDispatch($listeners, $event);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -175,6 +191,25 @@ protected function doDispatch($listeners, $eventName, Event $event)
}
}

/**
* Triggers the listeners of an anonymous event.
*
* This method can be overridden to add functionality that is executed
* for each listener.
*
* @param callable[] $listeners The event listeners
* @param object $event The event object to pass to the event handlers/listeners
*/
protected function doAnonymousEventDispatch($listeners, $event)
{
foreach ($listeners as $listener) {
if ($this->isPropagationStopped($event)) {
break;
}
call_user_func($listener, $event, get_class($event), $this);
}
}

/**
* Sorts the internal list of listeners for the given event by priority.
*
Expand All @@ -185,4 +220,22 @@ private function sortListeners($eventName)
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
}

/**
* {@inheritdoc}
*/
public function isPropagationStopped($event)
{
return in_array($event, $this->stoppedEvents);
Copy link
Member

Choose a reason for hiding this comment

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

comparing the whole object graph for equality is a nightmare for performance

}

/**
* {@inheritdoc}
*/
public function stopPropagation($event)
{
if (!$this->isPropagationStopped($event)) {
$this->stoppedEvents[] = $event;
Copy link
Member

Choose a reason for hiding this comment

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

making the EventDispatcher stateful is a nogo (this is leaking memory by keeping a reference to the object btw)

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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;

/**
* Keeps track of events whose propagation has been stopped.
*
* @author Daniel Santamaría <[email protected]>
*/
interface EventPropagationTrackerInterface
{
/**
* Checks if the event propagation has been stopped.
*
* @param object $event
*
* @return bool
*/
public function isPropagationStopped($event);

/**
* Stops the event the propagation.
*
* @param object $event
*/
public function stopPropagation($event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventPropagationTrackerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

abstract class AbstractEventDispatcherTest extends TestCase
Expand All @@ -31,10 +32,13 @@ abstract class AbstractEventDispatcherTest extends TestCase

private $listener;

private $anonymousListener;

protected function setUp()
{
$this->dispatcher = $this->createEventDispatcher();
$this->listener = new TestEventListener();
$this->anonymousListener = new TestAnonymousEventListener($this->dispatcher);
}

protected function tearDown()
Expand Down Expand Up @@ -137,6 +141,38 @@ public function testDispatch()
$this->assertSame($event, $return);
}

public function testDispatchAnonymousEvent()
{
$this->dispatcher->addListener(PreFoo::class, array($this->anonymousListener, 'preFoo'));
$this->dispatcher->addListener(PostFoo::class, array($this->anonymousListener, 'postFoo'));
$this->dispatcher->dispatchAnonymousEvent(new PreFoo());
$this->assertTrue($this->anonymousListener->preFooInvoked);
$this->assertFalse($this->anonymousListener->postFooInvoked);
}

public function nonObjectProvider()
{
return array(
array(array(1, 2, 3, 4)),
array('foo'),
array(9),
array(3.14),
array(true),
array(fopen(__FILE__, 'r')),
);
}

/**
* @dataProvider nonObjectProvider
* @expectedException \InvalidArgumentException
*
* @param mixed $nonObject
*/
public function testDispatchNonObjectAnonymousEvent($nonObject)
{
$this->dispatcher->dispatchAnonymousEvent($nonObject);
}

public function testDispatchForClosure()
{
$invoked = 0;
Expand Down Expand Up @@ -372,3 +408,43 @@ public static function getSubscribedEvents()
));
}
}

class PreFoo
{
}

class PostFoo
{
}

class TestAnonymousEventListener
{
public $preFooInvoked = false;
public $postFooInvoked = false;
public $fooInvoked = false;
private $eventPropagationTracker;

public function __construct(EventPropagationTrackerInterface $eventPropagationTracker)
{
$this->eventPropagationTracker = $eventPropagationTracker;
}

/* Listener methods */

public function preFoo($e)
{
$this->preFooInvoked = true;
}

public function postFoo($e)
{
$this->postFooInvoked = true;

$this->eventPropagationTracker->stopPropagation($e);
}

public function foo($e)
{
$this->fooInvoked = true;
}
}