8000 [EventDispatcher] Add fluid interface on dispatch() · symfony/symfony@876cf96 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 876cf96

Browse files
author
Drak
committed
[EventDispatcher] Add fluid interface on dispatch()
1 parent 4bb65c7 commit 876cf96

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

CHANGELOG-2.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
182182

183183
* added a reference to the EventDispatcher on the Event
184184
* added a reference to the Event name on the event
185+
* added fluid interface to the dispatch() method which now returns the Event object
185186

186187
### Filesystem
187188

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ class EventDispatcher implements EventDispatcherInterface
3939
*/
4040
public function dispatch($eventName, Event $event = null)
4141
{
42-
if (!isset($this->listeners[$eventName])) {
43-
return;
44-
}
45-
4642
if (null === $event) {
4743
$event = new Event();
4844
}
4945

5046
$event->setDispatcher($this);
5147
$event->setName($eventName);
5248

49+
if (!isset($this->listeners[$eventName])) {
50+
return $event;
51+
}
52+
5353
$this->doDispatch($this->getListeners($eventName), $eventName, $event);
54+
55+
return $event;
5456
}
5557

5658
/**

src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ interface EventDispatcherInterface
3131
* @param Event $event The event to pass to the event handlers/listeners.
3232
* If not supplied, an empty Event instance is created.
3333
*
34+
* @return Event
35+
*
3436
* @api
3537
*/
3638
function dispatch($eventName, Event $event = null);

tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,12 @@ public function testDispatch()
110110
$this->dispatcher->dispatch(self::preFoo);
111111
$this->assertTrue($this->listener->preFooInvoked);
112112
$this->assertFalse($this->listener->postFooInvoked);
113+
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
114+
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
113115
$event = new Event();
114-
$this->dispatcher->dispatch(self::preFoo, $event);
116+
$return = $this->dispatcher->dispatch(self::preFoo, $event);
115117
$this->assertEquals('pre.foo', $event->getName());
118+
$this->assertSame($event, $return);
116119
}
117120

118121
public function testDispatchForClosure()

0 commit comments

Comments
 (0)
0