From 045a5ba703c87b7b42abb397c1ba4cbd9deffea4 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 2 Mar 2025 16:03:52 +0100 Subject: [PATCH 1/4] replace assertEmpty() with stricter assertions --- Tests/Debug/TraceableEventDispatcherTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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() From 497f73ac996a598c92409b44ac43b6690c4f666d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 18 Apr 2025 14:51:48 +0200 Subject: [PATCH 2/4] Don't enable tracing unless the profiler is enabled --- Debug/TraceableEventDispatcher.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Debug/TraceableEventDispatcher.php b/Debug/TraceableEventDispatcher.php index 8330ce1..cd71745 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(); From 307a09d8d7228d14a05e5e05b95fffdacab032b2 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 10 Jul 2025 09:12:18 +0200 Subject: [PATCH 3/4] CS fixes --- Debug/TraceableEventDispatcher.php | 2 +- DependencyInjection/RegisterListenersPass.php | 8 ++++---- GenericEvent.php | 2 +- Tests/DependencyInjection/RegisterListenersPassTest.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Debug/TraceableEventDispatcher.php b/Debug/TraceableEventDispatcher.php index 5ba83da..47a397c 100644 --- a/Debug/TraceableEventDispatcher.php +++ b/Debug/TraceableEventDispatcher.php @@ -127,7 +127,7 @@ public function dispatch(object $event, ?string $eventName = null): object $currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : ''; if (null !== $this->logger && $event instanceof StoppableEventInterface && $event->isPropagationStopped()) { - $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName)); + $this->logger->debug(\sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName)); } $this->preProcess($eventName); diff --git a/DependencyInjection/RegisterListenersPass.php b/DependencyInjection/RegisterListenersPass.php index 866f4e6..db119bb 100644 --- a/DependencyInjection/RegisterListenersPass.php +++ b/DependencyInjection/RegisterListenersPass.php @@ -91,7 +91,7 @@ public function process(ContainerBuilder $container) if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method'])) { if (!$r->hasMethod('__invoke')) { - throw new InvalidArgumentException(sprintf('None of the "%s" or "__invoke" methods exist for the service "%s". Please define the "method" attribute on "kernel.event_listener" tags.', $event['method'], $id)); + throw new InvalidArgumentException(\sprintf('None of the "%s" or "__invoke" methods exist for the service "%s". Please define the "method" attribute on "kernel.event_listener" tags.', $event['method'], $id)); } $event['method'] = '__invoke'; @@ -126,10 +126,10 @@ public function process(ContainerBuilder $container) $class = $def->getClass(); if (!$r = $container->getReflectionClass($class)) { - throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); + throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); } if (!$r->isSubclassOf(EventSubscriberInterface::class)) { - throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class)); + throw new InvalidArgumentException(\sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class)); } $class = $r->name; @@ -181,7 +181,7 @@ private function getEventFromTypeDeclaration(ContainerBuilder $container, string || $type->isBuiltin() || Event::class === ($name = $type->getName()) ) { - throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "kernel.event_listener" tags.', $id)); + throw new InvalidArgumentException(\sprintf('Service "%s" must define the "event" attribute on "kernel.event_listener" tags.', $id)); } return $name; diff --git a/GenericEvent.php b/GenericEvent.php index 0ccbbd8..3bfeca0 100644 --- a/GenericEvent.php +++ b/GenericEvent.php @@ -59,7 +59,7 @@ public function getArgument(string $key): mixed return $this->arguments[$key]; } - throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key)); + throw new \InvalidArgumentException(\sprintf('Argument "%s" not found.', $key)); } /** diff --git a/Tests/DependencyInjection/RegisterListenersPassTest.php b/Tests/DependencyInjection/RegisterListenersPassTest.php index c18d863..8c1c0e5 100644 --- a/Tests/DependencyInjection/RegisterListenersPassTest.php +++ b/Tests/DependencyInjection/RegisterListenersPassTest.php @@ -202,14 +202,14 @@ public function testInvokableEventListener() $container = new ContainerBuilder(); $container->setParameter('event_dispatcher.event_aliases', [AliasedEvent::class => 'aliased_event']); - $container->register('foo', \get_class(new class() { + $container->register('foo', \get_class(new class { public function onFooBar() { } }))->addTag('kernel.event_listener', ['event' => 'foo.bar']); $container->register('bar', InvokableListenerService::class)->addTag('kernel.event_listener', ['event' => 'foo.bar']); $container->register('baz', InvokableListenerService::class)->addTag('kernel.event_listener', ['event' => 'event']); - $container->register('zar', \get_class(new class() { + $container->register('zar', \get_class(new class { public function onFooBarZar() { } From b0cf3162020603587363f0551cd3be43958611ff Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 13 Aug 2025 10:17:00 +0200 Subject: [PATCH 4/4] Remove deprecated calls to deprecated methods of SplObjectStorage --- Debug/TraceableEventDispatcher.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Debug/TraceableEventDispatcher.php b/Debug/TraceableEventDispatcher.php index 47a397c..2faea04 100644 --- a/Debug/TraceableEventDispatcher.php +++ b/Debug/TraceableEventDispatcher.php @@ -279,7 +279,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]; } } @@ -303,7 +303,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) {