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

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 extends _FirewallMap implements FirewallMapInterface
  22. {
  23.     /**
  24.      * @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below
  25.      */
  26.     private $container;
  27.     /**
  28.      * @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below
  29.      */
  30.     private $map;
  31.     public function __construct(ContainerInterface $container$map)
  32.     {
  33.         parent::__construct($container$map);
  34.         $this->container $container;
  35.         $this->map $map;
  36.     }
  37.     /**
  38.      * @internal
  39.      */
  40.     public function __get($name)
  41.     {
  42.         if ('map' === $name || 'container' === $name) {
  43.             @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since Symfony 3.3 as it will be removed/private in 4.0.'__CLASS__$name), E_USER_DEPRECATED);
  44.             if ('map' === $name && $this->map instanceof \Traversable) {
  45.                 $this->map iterator_to_array($this->map);
  46.             }
  47.         }
  48.         return $this->$name;
  49.     }
  50.     /**
  51.      * @internal
  52.      */
  53.     public function __set($name$value)
  54.     {
  55.         if ('map' === $name || 'container' === $name) {
  56.             @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since Symfony 3.3 as it will be removed/private in 4.0.'__CLASS__$name), E_USER_DEPRECATED);
  57.             $set = \Closure::bind(function ($name$value) { $this->$name $value; }, $thisparent::class);
  58.             $set($name$value);
  59.         }
  60.         $this->$name $value;
  61.     }
  62.     /**
  63.      * @internal
  64.      */
  65.     public function __isset($name)
  66.     {
  67.         if ('map' === $name || 'container' === $name) {
  68.             @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since Symfony 3.3 as it will be removed/private in 4.0.'__CLASS__$name), E_USER_DEPRECATED);
  69.         }
  70.         return isset($this->$name);
  71.     }
  72.     /**
  73.      * @internal
  74.      */
  75.     public function __unset($name)
  76.     {
  77.         if ('map' === $name || 'container' === $name) {
  78.             @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since Symfony 3.3 as it will be removed/private in 4.0.'__CLASS__$name), E_USER_DEPRECATED);
  79.             $unset = \Closure::bind(function ($name) { unset($this->$name); }, $thisparent::class);
  80.             $unset($name);
  81.         }
  82.         unset($this->$name);
  83.     }
  84. }
  85. /**
  86.  * @internal to be removed in 4.0
  87.  */
  88. class _FirewallMap
  89. {
  90.     private $container;
  91.     private $map;
  92.     public function __construct(ContainerInterface $container$map)
  93.     {
  94.         $this->container $container;
  95.         $this->map $map;
  96.     }
  97.     public function getListeners(Request $request)
  98.     {
  99.         $context $this->getFirewallContext($request);
  100.         if (null === $context) {
  101.             return [[], nullnull];
  102.         }
  103.         return [$context->getListeners(), $context->getExceptionListener(), $context->getLogoutListener()];
  104.     }
  105.     /**
  106.      * @return FirewallConfig|null
  107.      */
  108.     public function getFirewallConfig(Request $request)
  109.     {
  110.         $context $this->getFirewallContext($request);
  111.         if (null === $context) {
  112.             return null;
  113.         }
  114.         return $context->getConfig();
  115.     }
  116.     /**
  117.      * @return FirewallContext|null
  118.      */
  119.     private function getFirewallContext(Request $request)
  120.     {
  121.         if ($request->attributes->has('_firewall_context')) {
  122.             $storedContextId $request->attributes->get('_firewall_context');
  123.             foreach ($this->map as $contextId => $requestMatcher) {
  124.                 if ($contextId === $storedContextId) {
  125.                     return $this->container->get($contextId);
  126.                 }
  127.             }
  128.             $request->attributes->remove('_firewall_context');
  129.         }
  130.         foreach ($this->map as $contextId => $requestMatcher) {
  131.             if (null === $requestMatcher || $requestMatcher->matches($request)) {
  132.                 $request->attributes->set('_firewall_context'$contextId);
  133.                 return $this->container->get($contextId);
  134.             }
  135.         }
  136.         return null;
  137.     }
  138. }