10000 Remove callable firewall listeners support · symfony/symfony@afe20ea · GitHub
[go: up one dir, main page]

Skip to content

Commit afe20ea

Browse files
committed
Remove callable firewall listeners support
1 parent 3079218 commit afe20ea

14 files changed

+54
-193
lines changed

UPGRADE-8.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ Security
321321
+}
322322
```
323323

324+
* Remove callable firewall listeners support, extend `AbstractListener` or implement `FirewallListenerInterface` instead
325+
* Remove `AbstractListener::__invoke`
326+
* Remove `LazyFirewallContext::__invoke()`
327+
324328
Serializer
325329
----------
326330

src/Symfony/Bundle/SecurityBundle/Debug/TraceableFirewallListener.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Symfony\Bundle\SecurityBundle\Security\LazyFirewallContext;
1717
use Symfony\Component\HttpKernel\Event\RequestEvent;
1818
use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticatorManagerListener;
19-
use Symfony\Component\Security\Http\Firewall\AbstractListener;
20-
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
2119
use Symfony\Contracts\Service\ResetInterface;
2220

2321
/**
@@ -33,7 +31,7 @@ final class TraceableFirewallListener extends FirewallListener implements ResetI
3331
public function getWrappedListeners(): array
3432
{
3533
return array_map(
36-
static fn (WrappedListener|WrappedLazyListener $listener) => $listener->getInfo(),
34+
static fn (WrappedLazyListener $listener) => $listener->getInfo(),
3735
$this->wrappedListeners
3836
);
3937
}
@@ -62,10 +60,7 @@ protected function callListeners(RequestEvent $event, iterable $listeners): void
6260
if ($listener instanceof TraceableAuthenticatorManagerListener) {
6361
$contextAuthenticatorManagerListener ??= $listener;
6462
}
65-
$contextWrappedListeners[] = $listener instanceof FirewallListenerInterface
66-
? new WrappedLazyListener($listener)
67-
: new WrappedListener($listener)
68-
;
63+
$contextWrappedListeners[] = new WrappedLazyListener($listener) 341A ;
6964
}
7065
$this->listeners = $contextWrappedListeners;
7166
}, $listener, FirewallContext::class)();
@@ -78,23 +73,20 @@ protected function callListeners(RequestEvent $event, iterable $listeners): void
7873
if ($listener instanceof TraceableAuthenticatorManagerListener) {
7974
$this->authenticatorManagerListener ??= $listener;
8075
}
81-
$wrappedListener = $listener instanceof FirewallListenerInterface
82-
? new WrappedLazyListener($listener)
83-
: new WrappedListener($listener)
84-
;
76+
$wrappedListener = new WrappedLazyListener($listener);
8577
$this->wrappedListeners[] = $wrappedListener;
8678

8779
$requestListeners[] = $wrappedListener;
8880
}
8981
}
9082

9183
foreach ($requestListeners as $listener) {
92-
if (!$listener instanceof FirewallListenerInterface) {
93-
$listener($event);
94-
} elseif (false !== $listener->supports($event->getRequest())) {
95-
$listener->authenticate($event);
84+
if (false === $listener->supports($event->getRequest())) {
85+
continue;
9686
}
9787

88+
$listener->authenticate($event);
89+
9890
if ($event->hasResponse()) {
9991
break;
10092
}

src/Symfony/Bundle/SecurityBundle/Debug/TraceableListenerTrait.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/Symfony/Bundle/SecurityBundle/Debug/WrappedLazyListener.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace Symfony\Bundle\SecurityBundle\Debug;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\HttpKernel\Event\RequestEvent;
1617
use Symfony\Component\Security\Core\Exception\LazyResponseException;
18+
use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticatorManagerListener;
1719
use Symfony\Component\Security\Http\Firewall\AbstractListener;
1820
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
21+
use Symfony\Component\VarDumper\Caster\ClassStub;
1922

2023
/**
2124
* Wraps a lazy security listener.
@@ -26,7 +29,10 @@
2629
*/
2730
final class WrappedLazyListener extends AbstractListener
2831
{
29-
use TraceableListenerTrait;
32+
private ?Response $response = null;
33+
private FirewallListenerInterface $listener;
34+
private ?float $time = null;
35+
private ClassStub $stub;
3036

3137
public function __construct(FirewallListenerInterface $listener)
3238
{
@@ -54,4 +60,21 @@ public function authenticate(RequestEvent $event): void
5460

5561
$this->response = $event->getResponse();
5662
}
63+
64+
public function getInfo(): array
65+
{
66+
return [
67+
'response' => $this->response,
68+
'time' => $this->time,
69+
'stub' => $this->stub ??= new ClassStub($this->listener instanceof TraceableAuthenticatorManagerListener ? $this->listener->getAuthenticatorManagerListener()::class : $this->listener::class),
70+
];
71+
}
72+
73+
/**
74+
* Proxies all method calls to the original listener.
75+
*/
76+
public function __call(string $method, array $arguments): mixed
77+
{
78+
return $this->listener->{$method}(...$arguments);
79+
}
5780
}

src/Symfony/Bundle/SecurityBundle/Debug/WrappedListener.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Symfony/Bundle/SecurityBundle/Security/FirewallContext.php

Lines changed: 2 additions & 2 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class FirewallContext
2525
{
2626
/**
27-
* @param iterable<mixed, callable> $listeners
27+
* @param iterable<mixed, FirewallListenerInterface> $listeners
2828
*/
2929
public function __construct(
3030
private iterable $listeners,
@@ -40,7 +40,7 @@ public function getConfig(): ?FirewallConfig
4040
}
4141

4242
/**
43-
* @return iterable<mixed, FirewallListenerInterface|callable>
43+
* @return iterable<mixed, FirewallListenerInterface>
4444
*/
4545
public function getListeners(): iterable
4646
{

src/Symfony/Bundle/SecurityBundle/Security/LazyFirewallContext.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\HttpKernel\Event\RequestEvent;
1616
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1717
use Symfony\Component\Security\Http\Event\LazyResponseEvent;
18-
use Symfony\Component\Security\Http\Firewall\AbstractListener;
1918
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
2019
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
2120
use Symfony\Component\Security\Http\Firewall\LogoutListener;
@@ -54,20 +53,15 @@ public function authenticate(RequestEvent $event): void
5453
$lazy = $request->isMethodCacheable();
5554

5655
foreach (parent::getListeners() as $listener) {
57-
if (!$listener instanceof FirewallListenerInterface) {
58-
trigger_deprecation('symfony/security-http', '7.4', 'Using a callable as firewall listener is deprecated, extend "%s" or implement "%s" instead.', AbstractListener::class, FirewallListenerInterface::class);
59-
56+
if (false !== $supports = $listener->supports($request)) {
6057
$listeners[] = $listener;
61-
$lazy = false;
62-
} elseif (false !== $supports = $listener->supports($request)) {
63-
$listeners[] = [$listener, 'authenticate'];
6458
$lazy = $lazy && null === $supports;
6559
}
6660
}
6761

6862
if (!$lazy) {
6963
foreach ($listeners as $listener) {
70-
$listener($event);
64+
$listener->authenticate($event);
7165

7266
if ($event->hasResponse()) {
7367
return;
@@ -80,7 +74,7 @@ public function authenticate(RequestEvent $event): void
8074
$this->tokenStorage->setInitializer(function () use ($event, $listeners) {
8175
$event = new LazyResponseEvent($event);
8276
foreach ($listeners as $listener) {
83-
$listener($event);
77+
$listener->authenticate($event);
8478
}
8579
});
8680
}
@@ -89,14 +83,4 @@ public static function getPriority(): int
8983
{
9084
return 0;
9185
}
92-
93-
/**
94-
* @deprecated since Symfony 7.4, to be removed in 8.0
95-
*/
96-
public function __invoke(RequestEvent $event): void
97-
{
98-
trigger_deprecation('symfony/security-bundle', '7.4', 'The "%s()" method is deprecated since Symfony 7.4 and will be removed in 8.0.', __METHOD__);
99-
100-
$this->authenticate($event);
101-
}
10286
}

src/Symfony/Bundle/SecurityBundle/Tests/Debug/TraceableFirewallListenerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Security\Http\Firewall\AuthenticatorManagerListener;
3434
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
3535
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
36+
use Symfony\Component\VarDumper\Caster\ClassStub;
3637

3738
/**
3839
* @group time-sensitive
@@ -75,7 +76,8 @@ public function authenticate(RequestEvent $event): void
7576

7677
$listeners = $firewall->getWrappedListeners();
7778
$this->assertCount(1, $listeners);
78-
$this->assertSame($listener, $listeners[0]['stub']);
79+
$this->assertInstanceOf(ClassStub::class, $listeners[0]['stub']);
80+
$this->assertSame((string) new ClassStub($listener::class), (string) $listeners[0]['stub']);
7981
}
8082

8183
public function testOnKernelRequestRecordsAuthenticatorsInfo()

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"symfony/clock": "^7.4|^8.0",
2323
"symfony/config": "^7.4|^8.0",
2424
"symfony/dependency-injection": "^7.4|^8.0",
25-
"symfony/deprecation-contracts": "^2.5|^3",
2625
"symfony/event-dispatcher": "^7.4|^8.0",
2726
"symfony/http-kernel": "^7.4|^8.0",
2827
"symfony/http-foundation": "^7.4|^8.0",

src/Symfony/Component/Security/Http/Firewall.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1717
use Symfony\Component\HttpKernel\Event\RequestEvent;
1818
use Symfony\Component\HttpKernel\KernelEvents;
19-
use Symfony\Component\Security\Http\Firewall\AbstractListener;
2019
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
21-
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
2220
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2321

2422
/**
@@ -66,14 +64,12 @@ public function onKernelRequest(RequestEvent $event): void
6664
// Authentication listeners are pre-sorted by SortFirewallListenersPass
6765
$authenticationListeners = function () use ($authenticationListeners, $logoutListener) {
6866
if (null !== $logoutListener) {
69-
$logoutListenerPriority = $this->getListenerPriority($logoutListener);
67+
$logoutListenerPriority = $logoutListener::getPriority();
7068
}
7169

7270
foreach ($authenticationListeners as $listener) {
73-
$listenerPriority = $this->getListenerPriority($listener);
74-
7571
// Yielding the LogoutListener at the correct position
76-
if (null !== $logoutListener && $listenerPriority < $logoutListenerPriority) {
72+
if (null !== $logoutListener && $listener::getPriority() < $logoutListenerPriority) {
7773
yield $logoutListener;
7874
$logoutListener = null;
7975
}
@@ -111,22 +107,15 @@ public static function getSubscribedEvents(): array
111107
protected function callListeners(RequestEvent $event, iterable $listeners): void
112108
{
113109
foreach ($listeners as $listener) {
114-
if (!$listener instanceof FirewallListenerInterface) {
115-
trigger_deprecation('symfony/security-http', '7.4', 'Using a callable as firewall listener is deprecated, extend "%s" or implement "%s" instead.', AbstractListener::class, FirewallListenerInterface::class);
116-
117-
$listener($event);
118-
} elseif (false !== $listener->supports($event->getRequest())) {
119-
$listener->authenticate($event);
110+
if (false === $listener->supports($event->getRequest())) {
111+
continue;
120112
}
121113

114+
$listener->authenticate($event);
115+
122116
if ($event->hasResponse()) {
123117
break;
124118
}
125119
}
126120
}
127-
128-
private function getListenerPriority(object $listener): int
129-
{
130-
return $listener instanceof FirewallListenerInterface ? $listener->getPriority() : 0;
131-
}
132121
}

src/Symfony/Component/Security/Http/Firewall/AbstractListener.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@
2020
*/
2121
abstract class AbstractListener implements FirewallListenerInterface
2222
{
23-
/**
24-
* @deprecated since Symfony 7.4, to be removed in 8.0
25-
*/
26-
final public function __invoke(RequestEvent $event): void
27-
{
28-
trigger_deprecation('symfony/security-http', '7.4', 'The "%s()" method is deprecated since Symfony 7.4 and will be removed in 8.0.', __METHOD__);
29-
30-
if (false !== $this->supports($event->getRequest())) {
31-
$this->authenticate($event);
32-
}
33-
}
34-
3523
public static function getPriority(): int
3624
{
3725
return 0; // Default

0 commit comments

Comments
 (0)
0