allow TraceableEventDispatcher to reuse event instance in nested events

This commit is contained in:
Evan Villemez 2013-12-05 17:33:29 -05:00
parent 64cb5149f0
commit 454ce16709
2 changed files with 19 additions and 2 deletions

View File

@ -39,6 +39,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
private $wrappedListeners;
private $firstCalledEvent;
private $id;
private $lastEventId = 0;
/**
* Constructor.
@ -124,7 +125,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
$event = new Event();
}
$this->id = spl_object_hash($event);
$this->id = $eventId = ++$this->lastEventId;
$this->preDispatch($eventName, $event);
@ -139,7 +140,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
$this->dispatcher->dispatch($eventName, $event);
// reset the id as another event might have been dispatched during the dispatching of this event
$this->id = spl_object_hash($event);
$this->id = $eventId;
unset($this->firstCalledEvent[$eventName]);

View File

@ -159,6 +159,22 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
$dispatcher->dispatch('foo');
}
public function testDispatchReusedEventNested()
{
$nestedCall = false;
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
$dispatcher->dispatch('bar', $e);
});
$dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
$nestedCall = true;
});
$this->assertFalse($nestedCall);
$dispatcher->dispatch('foo');
$this->assertTrue($nestedCall);
}
public function testStopwatchSections()
{
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());