[EventDispatcher] Add fluid interface on dispatch()

This commit is contained in:
Drak 2012-03-10 09:55:57 +05:45
parent 4bb65c7057
commit 876cf96452
4 changed files with 13 additions and 5 deletions

View File

@ -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

View File

@ -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;
}
/**

View File

@ -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);

View File

@ -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()