diff --git a/Debug/WrappedListener.php b/Debug/WrappedListener.php index af257ff..5871578 100644 --- a/Debug/WrappedListener.php +++ b/Debug/WrappedListener.php @@ -112,10 +112,12 @@ public function __invoke(object $event, string $eventName, EventDispatcherInterf $e = $this->stopwatch->start($this->name, 'event_listener'); - ($this->optimizedListener ?? $this->listener)($event, $eventName, $dispatcher); - - if ($e->isStarted()) { - $e->stop(); + try { + ($this->optimizedListener ?? $this->listener)($event, $eventName, $dispatcher); + } finally { + if ($e->isStarted()) { + $e->stop(); + } } if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { diff --git a/DependencyInjection/RegisterListenersPass.php b/DependencyInjection/RegisterListenersPass.php index 51159d2..57b7bf9 100644 --- a/DependencyInjection/RegisterListenersPass.php +++ b/DependencyInjection/RegisterListenersPass.php @@ -93,7 +93,7 @@ public function process(ContainerBuilder $container) $dispatcherDefinition = $globalDispatcherDefinition; if (isset($event['dispatcher'])) { - $dispatcherDefinition = $container->getDefinition($event['dispatcher']); + $dispatcherDefinition = $container->findDefinition($event['dispatcher']); } $dispatcherDefinition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]); @@ -132,7 +132,7 @@ public function process(ContainerBuilder $container) continue; } - $dispatcherDefinitions[$attributes['dispatcher']] = $container->getDefinition($attributes['dispatcher']); + $dispatcherDefinitions[$attributes['dispatcher']] = $container->findDefinition($attributes['dispatcher']); } if (!$dispatcherDefinitions) { diff --git a/LICENSE b/LICENSE index 0083704..0138f8f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2004-2023 Fabien Potencier +Copyright (c) 2004-present Fabien Potencier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Tests/Debug/WrappedListenerTest.php b/Tests/Debug/WrappedListenerTest.php index bda9966..f4885ec 100644 --- a/Tests/Debug/WrappedListenerTest.php +++ b/Tests/Debug/WrappedListenerTest.php @@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\Debug\WrappedListener; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Component\Stopwatch\StopwatchEvent; class WrappedListenerTest extends TestCase { @@ -28,7 +29,7 @@ public function testListenerDescription($listener, $expected) $this->assertStringMatchesFormat($expected, $wrappedListener->getPretty()); } - public function provideListenersToDescribe() + public static function provideListenersToDescribe() { return [ [new FooListener(), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::__invoke'], @@ -43,6 +44,25 @@ public function provideListenersToDescribe() [[#[\Closure(name: FooListener::class)] static fn () => new FooListener(), 'listen'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'], ]; } + + public function testStopwatchEventIsStoppedWhenListenerThrows() + { + $stopwatchEvent = $this->createMock(StopwatchEvent::class); + $stopwatchEvent->expects(self::once())->method('isStarted')->willReturn(true); + $stopwatchEvent->expects(self::once())->method('stop'); + + $stopwatch = $this->createStub(Stopwatch::class); + $stopwatch->method('start')->willReturn($stopwatchEvent); + + $dispatcher = $this->createStub(EventDispatcherInterface::class); + + $wrappedListener = new WrappedListener(function () { throw new \Exception(); }, null, $stopwatch, $dispatcher); + + try { + $wrappedListener(new \stdClass(), 'foo', $dispatcher); + } catch (\Exception $ex) { + } + } } class FooListener