From df4c003f406e9f3012b0f3a6b4c53d7440018191 Mon Sep 17 00:00:00 2001 From: javer Date: Thu, 5 May 2022 14:00:32 +0300 Subject: [PATCH] [EventDispatcher] Fix removing listeners when using first-class callable syntax --- .../Debug/TraceableEventDispatcher.php | 6 ++-- .../EventDispatcher/EventDispatcher.php | 4 +-- .../Tests/EventDispatcherTest.php | 29 +++++++++++++++++++ .../Tests/Firewall/ContextListenerTest.php | 24 +++++++++++++++ .../Tests/Firewall/ExceptionListenerTest.php | 13 +++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php index e79d1a8e304cc..56116cf44f5cc 100644 --- a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php @@ -75,7 +75,7 @@ public function removeListener($eventName, $listener) { if (isset($this->wrappedListeners[$eventName])) { foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) { - if ($wrappedListener->getWrappedListener() === $listener) { + if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) { $listener = $wrappedListener; unset($this->wrappedListeners[$eventName][$index]); break; @@ -110,8 +110,8 @@ public function getListenerPriority($eventName, $listener) // we might have wrapped listeners for the event (if called while dispatching) // in that case get the priority by wrapper if (isset($this->wrappedListeners[$eventName])) { - foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) { - if ($wrappedListener->getWrappedListener() === $listener) { + foreach ($this->wrappedListeners[$eventName] as $wrappedListener) { + if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) { return $this->dispatcher->getListenerPriority($eventName, $wrappedListener); } } diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcher.php b/src/Symfony/Component/EventDispatcher/EventDispatcher.php index 8b62227189624..4a8f6c6f121de 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcher.php @@ -122,7 +122,7 @@ public function getListenerPriority($eventName, $listener) $v[0] = $v[0](); $v[1] = $v[1] ?? '__invoke'; } - if ($v === $listener) { + if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) { return $priority; } } @@ -178,7 +178,7 @@ public function removeListener($eventName, $listener) $v[0] = $v[0](); $v[1] = $v[1] ?? '__invoke'; } - if ($v === $listener) { + if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) { unset($listeners[$k], $this->sorted[$eventName], $this->optimized[$eventName]); } } diff --git a/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php index 67e78ac25ffa1..9f4a43a33d278 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php @@ -421,6 +421,35 @@ public function testMutatingWhilePropagationIsStopped() $this->assertTrue($testLoaded); } + /** + * @requires PHP 8.1 + */ + public function testNamedClosures() + { + $listener = new TestEventListener(); + + $callback1 = \Closure::fromCallable($listener); + $callback2 = \Closure::fromCallable($listener); + $callback3 = \Closure::fromCallable(new TestEventListener()); + + $this->assertNotSame($callback1, $callback2); + $this->assertNotSame($callback1, $callback3); + $this->assertNotSame($callback2, $callback3); + $this->assertTrue($callback1 == $callback2); + $this->assertFalse($callback1 == $callback3); + + $this->dispatcher->addListener('foo', $callback1, 3); + $this->dispatcher->addListener('foo', $callback2, 2); + $this->dispatcher->addListener('foo', $callback3, 1); + + $this->assertSame(3, $this->dispatcher->getListenerPriority('foo', $callback1)); + $this->assertSame(3, $this->dispatcher->getListenerPriority('foo', $callback2)); + + $this->dispatcher->removeListener('foo', $callback1); + + $this->assertSame(['foo' => [$callback3]], $this->dispatcher->getListeners()); + } + /** * @group legacy * @expectedDeprecation Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead. diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php index 7c60132bc055d..f3d9793c72de6 100644 --- a/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php @@ -342,6 +342,30 @@ public function testWithPreviousNotStartedSession() $this->assertSame($usageIndex, $session->getUsageIndex()); } + public function testOnKernelResponseRemoveListener() + { + $tokenStorage = new TokenStorage(); + $tokenStorage->setToken(new UsernamePasswordToken('test1', 'pass1', 'phpunit', ['ROLE_USER'])); + + $request = new Request(); + $request->attributes->set('_security_firewall_run', '_security_session'); + + $session = new Session(new MockArraySessionStorage()); + $request->setSession($session); + + $dispatcher = new EventDispatcher(); + $httpKernel = $this->createMock(HttpKernelInterface::class); + + $listener = new ContextListener($tokenStorage, [], 'session', null, $dispatcher, null, \Closure::fromCallable([$tokenStorage, 'getToken'])); + $this->assertEmpty($dispatcher->getListeners()); + + $listener(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST)); + $this->assertNotEmpty($dispatcher->getListeners()); + + $listener->onKernelResponse(new ResponseEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, new Response())); + $this->assertEmpty($dispatcher->getListeners()); + } + protected function runSessionOnKernelResponse($newToken, $original = null) { $session = new Session(new MockArraySessionStorage()); diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php index 27ad9897ea485..4f1f729ac944d 100644 --- a/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Security\Http\Tests\Firewall; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ExceptionEvent; @@ -170,6 +171,18 @@ public function testLogoutException() $this->assertEquals(403, $event->getThrowable()->getStatusCode()); } + public function testUnregister() + { + $listener = $this->createExceptionListener(); + $dispatcher = new EventDispatcher(); + + $listener->register($dispatcher); + $this->assertNotEmpty($dispatcher->getListeners()); + + $listener->unregister($dispatcher); + $this->assertEmpty($dispatcher->getListeners()); + } + public function getAccessDeniedExceptionProvider() { return [