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

  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 ContainerInterface $container;
  24.     private iterable $map;
  25.     public function __construct(ContainerInterface $containeriterable $map)
  26.     {
  27.         $this->container $container;
  28.         $this->map $map;
  29.     }
  30.     public function getListeners(Request $request): array
  31.     {
  32.         $context $this->getFirewallContext($request);
  33.         if (null === $context) {
  34.             return [[], nullnull];
  35.         }
  36.         return [$context->getListeners(), $context->getExceptionListener(), $context->getLogoutListener()];
  37.     }
  38.     public function getFirewallConfig(Request $request): ?FirewallConfig
  39.     {
  40.         $context $this->getFirewallContext($request);
  41.         if (null === $context) {
  42.             return null;
  43.         }
  44.         return $context->getConfig();
  45.     }
  46.     private function getFirewallContext(Request $request): ?FirewallContext
  47.     {
  48.         if ($request->attributes->has('_firewall_context')) {
  49.             $storedContextId $request->attributes->get('_firewall_context');
  50.             foreach ($this->map as $contextId => $requestMatcher) {
  51.                 if ($contextId === $storedContextId) {
  52.                     return $this->container->get($contextId);
  53.                 }
  54.             }
  55.             $request->attributes->remove('_firewall_context');
  56.         }
  57.         foreach ($this->map as $contextId => $requestMatcher) {
  58.             if (null === $requestMatcher || $requestMatcher->matches($request)) {
  59.                 $request->attributes->set('_firewall_context'$contextId);
  60.                 /** @var FirewallContext $context */
  61.                 $context $this->container->get($contextId);
  62.                 if ($context->getConfig()?->isStateless() && !$request->attributes->has('_stateless')) {
  63.                     $request->attributes->set('_stateless'true);
  64.                 }
  65.                 return $context;
  66.             }
  67.         }
  68.         return null;
  69.     }
  70. }