diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 44769d74c5..9fc64c535a 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -182,6 +182,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * added a reference to the EventDispatcher on the Event * added a reference to the Event name on the event + * added fluid interface to the dispatch() method which now returns the Event object ### Filesystem diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcher.php b/src/Symfony/Component/EventDispatcher/EventDispatcher.php index 0c98301ad1..8f712f1300 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcher.php @@ -39,10 +39,6 @@ class EventDispatcher implements EventDispatcherInterface */ public function dispatch($eventName, Event $event = null) { - if (!isset($this->listeners[$eventName])) { - return; - } - if (null === $event) { $event = new Event(); } @@ -50,7 +46,13 @@ class EventDispatcher implements EventDispatcherInterface $event->setDispatcher($this); $event->setName($eventName); + if (!isset($this->listeners[$eventName])) { + return $event; + } + $this->doDispatch($this->getListeners($eventName), $eventName, $event); + + return $event; } /** diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php index e57214da81..f531ca502e 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php @@ -31,6 +31,8 @@ interface EventDispatcherInterface * @param Event $event The event to pass to the event handlers/listeners. * If not supplied, an empty Event instance is created. * + * @return Event + * * @api */ function dispatch($eventName, Event $event = null); diff --git a/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php b/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php index befa9efec5..3c3e1cca7d 100644 --- a/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php +++ b/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php @@ -110,9 +110,12 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase $this->dispatcher->dispatch(self::preFoo); $this->assertTrue($this->listener->preFooInvoked); $this->assertFalse($this->listener->postFooInvoked); + $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent')); + $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo)); $event = new Event(); - $this->dispatcher->dispatch(self::preFoo, $event); + $return = $this->dispatcher->dispatch(self::preFoo, $event); $this->assertEquals('pre.foo', $event->getName()); + $this->assertSame($event, $return); } public function testDispatchForClosure()