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/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php b/src/Symfony/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php index b88625870e..0ce875bcee 100644 --- a/src/Symfony/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php +++ b/src/Symfony/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php @@ -164,7 +164,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher { $this->lazyLoad($eventName); - parent::dispatch($eventName, $event); + return parent::dispatch($eventName, $event); } public function getContainer() diff --git a/src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php b/src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php index c2116ea663..d198fc9308 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php @@ -47,6 +47,9 @@ class TraceableEventDispatcher extends ContainerAwareEventDispatcher implements $this->called = array(); } + /** + * {@inheritdoc} + */ public function dispatch($eventName, Event $event = null) { switch ($eventName) { @@ -87,6 +90,8 @@ class TraceableEventDispatcher extends ContainerAwareEventDispatcher implements $this->updateProfile($token); break; } + + return $event; } /** 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()