vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall.php line 56

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