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

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\Core\Authorization\Voter\AuthenticatedVoter;
  14. use Symfony\Component\Security\Core\Exception\LazyResponseException;
  15. use Symfony\Component\Security\Http\AccessMapInterface;
  16. use Symfony\Component\Security\Http\Event\LazyResponseEvent;
  17. use Symfony\Component\Security\Http\Firewall\AccessListener;
  18. use Symfony\Component\Security\Http\Firewall\ExceptionListener;
  19. use Symfony\Component\Security\Http\Firewall\LogoutListener;
  20. /**
  21.  * Lazily calls authentication listeners when actually required by the access listener.
  22.  *
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  */
  25. class LazyFirewallContext extends FirewallContext
  26. {
  27.     private $accessListener;
  28.     private $tokenStorage;
  29.     private $map;
  30.     public function __construct(iterable $listeners, ?ExceptionListener $exceptionListener, ?LogoutListener $logoutListener, ?FirewallConfig $configAccessListener $accessListenerTokenStorage $tokenStorageAccessMapInterface $map)
  31.     {
  32.         parent::__construct($listeners$exceptionListener$logoutListener$config);
  33.         $this->accessListener $accessListener;
  34.         $this->tokenStorage $tokenStorage;
  35.         $this->map $map;
  36.     }
  37.     public function getListeners(): iterable
  38.     {
  39.         return [$this];
  40.     }
  41.     public function __invoke(RequestEvent $event)
  42.     {
  43.         $this->tokenStorage->setInitializer(function () use ($event) {
  44.             $event = new LazyResponseEvent($event);
  45.             foreach (parent::getListeners() as $listener) {
  46.                 $listener($event);
  47.             }
  48.         });
  49.         try {
  50.             [$attributes] = $this->map->getPatterns($event->getRequest());
  51.             if ($attributes && [AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] !== $attributes) {
  52.                 ($this->accessListener)($event);
  53.             }
  54.         } catch (LazyResponseException $e) {
  55.             $event->setResponse($e->getResponse());
  56.         }
  57.     }
  58. }