8000 bug #9750 allow TraceableEventDispatcher to reuse event instance in n… · symfony/symfony@d490e5a · GitHub
[go: up one dir, main page]

Skip to content

Commit d490e5a

Browse files
committed
bug #9750 allow TraceableEventDispatcher to reuse event instance in nested events (evillemez)
This PR was merged into the 2.3 branch. Discussion ---------- allow TraceableEventDispatcher to reuse event instance in nested events | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9710, #9727 | License | MIT Commits ------- 454ce16 allow TraceableEventDispatcher to reuse event instance in nested events
2 parents cfe7d7f + 454ce16 commit d490e5a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
3939
private $wrappedListeners;
4040
private $firstCalledEvent;
4141
private $id;
42+
private $lastEventId = 0;
4243

4344
/**
4445
* Constructor.
@@ -124,7 +125,7 @@ public function dispatch($eventName, Event $event = null)
124125
$event = new Event();
125126
}
126127

127-
$this->id = spl_object_hash($event);
128+
$this->id = $eventId = ++$this->lastEventId;
128129

129130
$this->preDispatch($eventName, $event);
130131

@@ -139,7 +140,7 @@ public function dispatch($eventName, Event $event = null)
139140
$this->dispatcher->dispatch($eventName, $event);
140141

141142
// reset the id as another event might have been dispatched during the dispatching of this event
142-
$this->id = spl_object_hash($event);
143+
$this->id = $eventId;
143144

144145
unset($this->firstCalledEvent[$eventName]);
145146

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ public function testDispatchNested()
159159
$dispatcher->dispatch('foo');
160160
}
161161

162+
public function testDispatchReusedEventNested()
163+
{
164+
$nestedCall = false;
165+
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
166+
$dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
167+
$dispatcher->dispatch('bar', $e);
168+
});
169+
$dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
170+
$nestedCall = true;
171+
});
172+
173+
$this->assertFalse($nestedCall);
174+
$dispatcher->dispatch('foo');
175+
$this->assertTrue($nestedCall);
176+
}
177+
162178
public function testStopwatchSections()
163179
{
164180
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());

0 commit comments

Comments
 (0)
0