8000 bug #12848 [EventDispatcher] Fixed #12845 adding a listener to an eve… · symfony/symfony@e1e4cfc · GitHub
[go: up one dir, main page]

Skip to content

Commit e1e4cfc

Browse files
committed
bug #12848 [EventDispatcher] Fixed #12845 adding a listener to an event that is currently being dispatched (Pieter Jordaan)
This PR was merged into the 2.5 branch. Discussion ---------- [EventDispatcher] Fixed #12845 adding a listener to an event that is currently being dispatched [EventDispatcher] Fixed adding listener when event is currently being dispatched | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12845 | License | MIT Commits ------- 0cbc2b0 Fixed #12845 adding a listener to an event that is currently being dispatched will not result into a fatal error in TraceableEventDispatcher [EventDispatcher]
2 parents b9f0dd1 + 0cbc2b0 commit e1e4cfc

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ private function postProcess($eventName)
224224
{
225225
$skipped = false;
226226
foreach ($this->dispatcher->getListeners($eventName) as $listener) {
227+
if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
228+
continue;
229+
}
227230
// Unwrap listener
228231
$this->dispatcher->removeListener($eventName, $listener);
229232
$this->dispatcher->addListener($eventName, $listener->getWrappedListener());

src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ public function testStopwatchStopControllerOnRequestEvent()
7575
$kernel->handle($request);
7676
}
7777

78+
public function testAddListenerNested()
79+
{
80+
$called1 = false;
81+
$called2 = false;
82+
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
83+
$dispatcher->addListener('my-event', function () use ($dispatcher, &$called1, &$called2) {
84+
$called1 = true;
85+
$dispatcher->addListener('my-event', function () use (&$called2) {
86+
$called2 = true;
87+
});
88+
});
89+
$dispatcher->dispatch('my-event');
90+
$this->assertTrue($called1);
91+
$this->assertFalse($called2);
92+
$dispatcher->dispatch('my-event');
93+
$this->assertTrue($called2);
94+
}
95+
7896
protected function getHttpKernel($dispatcher, $controller)
7997
{
8098
$resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');

0 commit comments

Comments
 (0)
0