Skip to content

Commit

Permalink
allow TraceableEventDispatcher to reuse event instance in nested events
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Villemez committed Dec 12, 2013
1 parent 64cb514 commit 454ce16
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -39,6 +39,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
private $wrappedListeners;
private $firstCalledEvent;
private $id;
private $lastEventId = 0;

/**
* Constructor.
Expand Down Expand Up @@ -124,7 +125,7 @@ public function dispatch($eventName, Event $event = null)
$event = new Event();
}

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

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

Expand All @@ -139,7 +140,7 @@ public function dispatch($eventName, Event $event = null)
$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]);

Expand Down
Expand Up @@ -159,6 +159,22 @@ public function testDispatchNested()
$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());
Expand Down

0 comments on commit 454ce16

Please sign in to comment.