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

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\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Security\Http\Firewall\AccessListener;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. /**
  18.  * Firewall uses a FirewallMap to register security listeners for the given
  19.  * request.
  20.  *
  21.  * It allows for different security strategies within the same application
  22.  * (a Basic authentication for the /api, and a web based authentication for
  23.  * everything else for instance).
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  */
  27. class Firewall implements EventSubscriberInterface
  28. {
  29.     private $map;
  30.     private $dispatcher;
  31.     private $exceptionListeners;
  32.     public function __construct(FirewallMapInterface $mapEventDispatcherInterface $dispatcher)
  33.     {
  34.         $this->map $map;
  35.         $this->dispatcher $dispatcher;
  36.         $this->exceptionListeners = new \SplObjectStorage();
  37.     }
  38.     public function onKernelRequest(RequestEvent $event)
  39.     {
  40.         if (!$event->isMasterRequest()) {
  41.             return;
  42.         }
  43.         // register listeners for this firewall
  44.         $listeners $this->map->getListeners($event->getRequest());
  45.         $authenticationListeners $listeners[0];
  46.         $exceptionListener $listeners[1];
  47.         $logoutListener $listeners[2];
  48.         if (null !== $exceptionListener) {
  49.             $this->exceptionListeners[$event->getRequest()] = $exceptionListener;
  50.             $exceptionListener->register($this->dispatcher);
  51.         }
  52.         $authenticationListeners = function () use ($authenticationListeners$logoutListener) {
  53.             $accessListener null;
  54.             foreach ($authenticationListeners as $listener) {
  55.                 if ($listener instanceof AccessListener) {
  56.                     $accessListener $listener;
  57.                     continue;
  58.                 }
  59.                 yield $listener;
  60.             }
  61.             if (null !== $logoutListener) {
  62.                 yield $logoutListener;
  63.             }
  64.             if (null !== $accessListener) {
  65.                 yield $accessListener;
  66.             }
  67.         };
  68.         $this->callListeners($event$authenticationListeners());
  69.     }
  70.     public function onKernelFinishRequest(FinishRequestEvent $event)
  71.     {
  72.         $request $event->getRequest();
  73.         if (isset($this->exceptionListeners[$request])) {
  74.             $this->exceptionListeners[$request]->unregister($this->dispatcher);
  75.             unset($this->exceptionListeners[$request]);
  76.         }
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public static function getSubscribedEvents()
  82.     {
  83.         return [
  84.             KernelEvents::REQUEST => ['onKernelRequest'8],
  85.             KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
  86.         ];
  87.     }
  88.     protected function callListeners(RequestEvent $eventiterable $listeners)
  89.     {
  90.         foreach ($listeners as $listener) {
  91.             $listener($event);
  92.             if ($event->hasResponse()) {
  93.                 break;
  94.             }
  95.         }
  96.     }
  97. }