8000 merged branch drak/fluid_eventdispatcher (PR #3546) · symfony/symfony@fb053f6 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit fb053f6

Browse files
committed
merged branch drak/fluid_eventdispatcher (PR #3546)
Commits ------- ca70a35 [FrameworkBundle] Return Event 876cf96 [EventDispatcher] Add fluid interface on dispatch() Discussion ---------- [2.1][EventDispatcher] Add fluid interface on dispatch() Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - This patch allows for code like the following:- $response = $dispatcher->dispatch('foo', new FooEvent())->getResponse(); and if ($dispatcher->dispatch('foo')->isStoppedPropagation()) { // ... }
2 parents 1a2642a + ca70a35 commit fb053f6

File tree

6 files changed

+19
-6
lines changed

6 files changed

+19
-6
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/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function dispatch($eventName, Event $event = null)
164164
{
165165
$this->lazyLoad($eventName);
166166

167-
parent::dispatch($eventName, $event);
167+
return parent::dispatch($eventName, $event);
168168
}
169169

170170
public function getContainer()

src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function __construct(ContainerInterface $container, Stopwatch $stopwatch,
4747
$this->called = array();
4848
}
4949

50+
/**
51+
* {@inheritdoc}
52+
*/
5053
public function dispatch($eventName, Event $event = null)
5154
{
5255
switch ($eventName) {
@@ -87,6 +90,8 @@ public function dispatch($eventName, Event $event = null)
8790
$this->updateProfile($token);
8891
break;
8992
}
93+
94+
return $event;
9095
}
9196

9297
/**

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