vendor/symfony/security-bundle/Security/FirewallMap.php line 54

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 Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Http\FirewallMapInterface;
  14. /**
  15.  * This is a lazy-loading firewall map implementation.
  16.  *
  17.  * Listeners will only be initialized if we really need them.
  18.  *
  19.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20.  */
  21. class FirewallMap implements FirewallMapInterface
  22. {
  23.     private $container;
  24.     private $map;
  25.     private $contexts;
  26.     public function __construct(ContainerInterface $containeriterable $map)
  27.     {
  28.         $this->container $container;
  29.         $this->map $map;
  30.         $this->contexts = new \SplObjectStorage();
  31.     }
  32.     public function getListeners(Request $request)
  33.     {
  34.         $context $this->getFirewallContext($request);
  35.         if (null === $context) {
  36.             return [[], nullnull];
  37.         }
  38.         return [$context->getListeners(), $context->getExceptionListener(), $context->getLogoutListener()];
  39.     }
  40.     /**
  41.      * @return FirewallConfig|null
  42.      */
  43.     public function getFirewallConfig(Request $request)
  44.     {
  45.         $context $this->getFirewallContext($request);
  46.         if (null === $context) {
  47.             return;
  48.         }
  49.         return $context->getConfig();
  50.     }
  51.     /**
  52.      * @return FirewallContext
  53.      */
  54.     private function getFirewallContext(Request $request)
  55.     {
  56.         if ($request->attributes->has('_firewall_context')) {
  57.             $storedContextId $request->attributes->get('_firewall_context');
  58.             foreach ($this->map as $contextId => $requestMatcher) {
  59.                 if ($contextId === $storedContextId) {
  60.                     return $this->container->get($contextId);
  61.                 }
  62.             }
  63.             $request->attributes->remove('_firewall_context');
  64.         }
  65.         foreach ($this->map as $contextId => $requestMatcher) {
  66.             if (null === $requestMatcher || $requestMatcher->matches($request)) {
  67.                 $request->attributes->set('_firewall_context'$contextId);
  68.                 return $this->container->get($contextId);
  69.             }
  70.         }
  71.     }
  72. }