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

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