vendor/symfony/security-http/Firewall.php line 51

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\Component\Security\Http;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  14. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  15. use Symfony\Component\HttpKernel\Event\RequestEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Security\Http\Firewall\AccessListener;
  18. use Symfony\Component\Security\Http\Firewall\LogoutListener;
  19. /**
  20.  * Firewall uses a FirewallMap to register security listeners for the given
  21.  * request.
  22.  *
  23.  * It allows for different security strategies within the same application
  24.  * (a Basic authentication for the /api, and a web based authentication for
  25.  * everything else for instance).
  26.  *
  27.  * @author Fabien Potencier <fabien@symfony.com>
  28.  */
  29. class Firewall implements EventSubscriberInterface
  30. {
  31.     private $map;
  32.     private $dispatcher;
  33.     private $exceptionListeners;
  34.     public function __construct(FirewallMapInterface $mapEventDispatcherInterface $dispatcher)
  35.     {
  36.         // the type-hint will be updated to the "EventDispatcherInterface" from symfony/contracts in 5.0
  37.         $this->map $map;
  38.         $this->dispatcher $dispatcher;
  39.         $this->exceptionListeners = new \SplObjectStorage();
  40.     }
  41.     /**
  42.      * @internal since Symfony 4.3
  43.      */
  44.     public function onKernelRequest(GetResponseEvent $event)
  45.     {
  46.         if (!$event->isMasterRequest()) {
  47.             return;
  48.         }
  49.         // register listeners for this firewall
  50.         $listeners $this->map->getListeners($event->getRequest());
  51.         if (!== \count($listeners)) {
  52.             @trigger_error(sprintf('Not returning an array of 3 elements from %s::getListeners() is deprecated since Symfony 4.2, the 3rd element must be an instance of %s or null.'FirewallMapInterface::class, LogoutListener::class), E_USER_DEPRECATED);
  53.             $listeners[2] = null;
  54.         }
  55.         $authenticationListeners $listeners[0];
  56.         $exceptionListener $listeners[1];
  57.         $logoutListener $listeners[2];
  58.         if (null !== $exceptionListener) {
  59.             $this->exceptionListeners[$event->getRequest()] = $exceptionListener;
  60.             $exceptionListener->register($this->dispatcher);
  61.         }
  62.         $authenticationListeners = function () use ($authenticationListeners$logoutListener) {
  63.             $accessListener null;
  64.             foreach ($authenticationListeners as $listener) {
  65.                 if ($listener instanceof AccessListener) {
  66.                     $accessListener $listener;
  67.                     continue;
  68.                 }
  69.                 yield $listener;
  70.             }
  71.             if (null !== $logoutListener) {
  72.                 yield $logoutListener;
  73.             }
  74.             if (null !== $accessListener) {
  75.                 yield $accessListener;
  76.             }
  77.         };
  78.         if ($event instanceof RequestEvent) {
  79.             $this->callListeners($event$authenticationListeners());
  80.         } else {
  81.             $this->handleRequest($event$authenticationListeners());
  82.         }
  83.     }
  84.     /**
  85.      * @internal since Symfony 4.3
  86.      */
  87.     public function onKernelFinishRequest(FinishRequestEvent $event)
  88.     {
  89.         $request $event->getRequest();
  90.         if (isset($this->exceptionListeners[$request])) {
  91.             $this->exceptionListeners[$request]->unregister($this->dispatcher);
  92.             unset($this->exceptionListeners[$request]);
  93.         }
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public static function getSubscribedEvents()
  99.     {
  100.         return [
  101.             KernelEvents::REQUEST => ['onKernelRequest'8],
  102.             KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
  103.         ];
  104.     }
  105.     protected function callListeners(RequestEvent $eventiterable $listeners)
  106.     {
  107.         $this->handleRequest($event$listeners);
  108.     }
  109.     /**
  110.      * @deprecated since Symfony 4.3, use callListeners instead
  111.      */
  112.     protected function handleRequest(GetResponseEvent $event$listeners)
  113.     {
  114.         foreach ($listeners as $listener) {
  115.             if (\is_callable($listener)) {
  116.                 $listener($event);
  117.             } else {
  118.                 @trigger_error(sprintf('Calling the "%s::handle()" method from the firewall is deprecated since Symfony 4.3, implement "__invoke()" instead.', \get_class($this)), E_USER_DEPRECATED);
  119.                 $listener->handle($event);
  120.             }
  121.             if ($event->hasResponse()) {
  122.                 break;
  123.             }
  124.         }
  125.     }
  126. }