vendor/symfony/security-bundle/Security/LazyFirewallContext.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\SecurityBundle\Security;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  13. use Symfony\Component\Security\Http\Event\LazyResponseEvent;
  14. use Symfony\Component\Security\Http\Firewall\AbstractListener;
  15. use Symfony\Component\Security\Http\Firewall\ExceptionListener;
  16. use Symfony\Component\Security\Http\Firewall\LogoutListener;
  17. /**
  18.  * Lazily calls authentication listeners when actually required by the access listener.
  19.  *
  20.  * @author Nicolas Grekas <p@tchwork.com>
  21.  */
  22. class LazyFirewallContext extends FirewallContext
  23. {
  24.     private $tokenStorage;
  25.     public function __construct(iterable $listeners, ?ExceptionListener $exceptionListener, ?LogoutListener $logoutListener, ?FirewallConfig $configTokenStorage $tokenStorage)
  26.     {
  27.         parent::__construct($listeners$exceptionListener$logoutListener$config);
  28.         $this->tokenStorage $tokenStorage;
  29.     }
  30.     public function getListeners(): iterable
  31.     {
  32.         return [$this];
  33.     }
  34.     public function __invoke(RequestEvent $event)
  35.     {
  36.         $listeners = [];
  37.         $request $event->getRequest();
  38.         $lazy $request->isMethodCacheable();
  39.         foreach (parent::getListeners() as $listener) {
  40.             if (!\is_callable($listener)) {
  41.                 @trigger_error(sprintf('Calling the "%s::handle()" method from the firewall is deprecated since Symfony 4.3, extend "%s" instead.', \get_class($listener), AbstractListener::class), E_USER_DEPRECATED);
  42.                 $listeners[] = [$listener'handle'];
  43.                 $lazy false;
  44.             } elseif (!$lazy || !$listener instanceof AbstractListener) {
  45.                 $listeners[] = $listener;
  46.                 $lazy $lazy && $listener instanceof AbstractListener;
  47.             } elseif (false !== $supports $listener->supports($request)) {
  48.                 $listeners[] = [$listener'authenticate'];
  49.                 $lazy null === $supports;
  50.             }
  51.         }
  52.         if (!$lazy) {
  53.             foreach ($listeners as $listener) {
  54.                 $listener($event);
  55.                 if ($event->hasResponse()) {
  56.                     return;
  57.                 }
  58.             }
  59.             return;
  60.         }
  61.         $this->tokenStorage->setInitializer(function () use ($event$listeners) {
  62.             $event = new LazyResponseEvent($event);
  63.             foreach ($listeners as $listener) {
  64.                 $listener($event);
  65.             }
  66.         });
  67.     }
  68. }