diff --git a/Debug/TraceableEventDispatcher.php b/Debug/TraceableEventDispatcher.php index 8330ce1..48c2531 100644 --- a/Debug/TraceableEventDispatcher.php +++ b/Debug/TraceableEventDispatcher.php @@ -43,6 +43,7 @@ public function __construct( protected Stopwatch $stopwatch, protected ?LoggerInterface $logger = null, private ?RequestStack $requestStack = null, + protected readonly ?\Closure $disabled = null, ) { } @@ -103,6 +104,9 @@ public function hasListeners(?string $eventName = null): bool public function dispatch(object $event, ?string $eventName = null): object { + if ($this->disabled?->__invoke()) { + return $this->dispatcher->dispatch($event, $eventName); + } $eventName ??= $event::class; $this->callStack ??= new \SplObjectStorage(); @@ -255,7 +259,7 @@ private function preProcess(string $eventName): void $this->wrappedListeners[$eventName][] = $wrappedListener; $this->dispatcher->removeListener($eventName, $listener); $this->dispatcher->addListener($eventName, $wrappedListener, $priority); - $this->callStack->attach($wrappedListener, [$eventName, $this->currentRequestHash]); + $this->callStack[$wrappedListener] = [$eventName, $this->currentRequestHash]; } } @@ -279,7 +283,7 @@ private function postProcess(string $eventName): void if ($listener->wasCalled()) { $this->logger?->debug('Notified event "{event}" to listener "{listener}".', $context); } else { - $this->callStack->detach($listener); + unset($this->callStack[$listener]); } if (null !== $this->logger && $skipped) { diff --git a/Tests/Debug/TraceableEventDispatcherTest.php b/Tests/Debug/TraceableEventDispatcherTest.php index ba8a131..cf640a3 100644 --- a/Tests/Debug/TraceableEventDispatcherTest.php +++ b/Tests/Debug/TraceableEventDispatcherTest.php @@ -182,7 +182,7 @@ public function testItReturnsNoOrphanedEventsWhenCreated() { $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $events = $tdispatcher->getOrphanedEvents(); - $this->assertEmpty($events); + $this->assertSame([], $events); } public function testItReturnsOrphanedEventsAfterDispatch() @@ -200,7 +200,7 @@ public function testItDoesNotReturnHandledEvents() $tdispatcher->addListener('foo', function () {}); $tdispatcher->dispatch(new Event(), 'foo'); $events = $tdispatcher->getOrphanedEvents(); - $this->assertEmpty($events); + $this->assertSame([], $events); } public function testLogger()