vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php line 182

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\Routing\Matcher\Dumper;
  11. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  12. use Symfony\Component\Routing\Exception\NoConfigurationException;
  13. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  14. use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
  15. use Symfony\Component\Routing\RequestContext;
  16. /**
  17.  * @author Nicolas Grekas <p@tchwork.com>
  18.  *
  19.  * @internal
  20.  *
  21.  * @property RequestContext $context
  22.  */
  23. trait CompiledUrlMatcherTrait
  24. {
  25.     private $matchHost false;
  26.     private $staticRoutes = [];
  27.     private $regexpList = [];
  28.     private $dynamicRoutes = [];
  29.     private $checkCondition;
  30.     public function match($pathinfo): array
  31.     {
  32.         $allow $allowSchemes = [];
  33.         if ($ret $this->doMatch($pathinfo$allow$allowSchemes)) {
  34.             return $ret;
  35.         }
  36.         if ($allow) {
  37.             throw new MethodNotAllowedException(array_keys($allow));
  38.         }
  39.         if (!$this instanceof RedirectableUrlMatcherInterface) {
  40.             throw new ResourceNotFoundException(sprintf('No routes found for "%s".'$pathinfo));
  41.         }
  42.         if (!\in_array($this->context->getMethod(), ['HEAD''GET'], true)) {
  43.             // no-op
  44.         } elseif ($allowSchemes) {
  45.             redirect_scheme:
  46.             $scheme $this->context->getScheme();
  47.             $this->context->setScheme(key($allowSchemes));
  48.             try {
  49.                 if ($ret $this->doMatch($pathinfo)) {
  50.                     return $this->redirect($pathinfo$ret['_route'], $this->context->getScheme()) + $ret;
  51.                 }
  52.             } finally {
  53.                 $this->context->setScheme($scheme);
  54.             }
  55.         } elseif ('/' !== $trimmedPathinfo rtrim($pathinfo'/') ?: '/') {
  56.             $pathinfo $trimmedPathinfo === $pathinfo $pathinfo.'/' $trimmedPathinfo;
  57.             if ($ret $this->doMatch($pathinfo$allow$allowSchemes)) {
  58.                 return $this->redirect($pathinfo$ret['_route']) + $ret;
  59.             }
  60.             if ($allowSchemes) {
  61.                 goto redirect_scheme;
  62.             }
  63.         }
  64.         throw new ResourceNotFoundException(sprintf('No routes found for "%s".'$pathinfo));
  65.     }
  66.     private function doMatch(string $pathinfo, array &$allow = [], array &$allowSchemes = []): array
  67.     {
  68.         $allow $allowSchemes = [];
  69.         $pathinfo rawurldecode($pathinfo) ?: '/';
  70.         $trimmedPathinfo rtrim($pathinfo'/') ?: '/';
  71.         $context $this->context;
  72.         $requestMethod $canonicalMethod $context->getMethod();
  73.         if ($this->matchHost) {
  74.             $host strtolower($context->getHost());
  75.         }
  76.         if ('HEAD' === $requestMethod) {
  77.             $canonicalMethod 'GET';
  78.         }
  79.         $supportsRedirections 'GET' === $canonicalMethod && $this instanceof RedirectableUrlMatcherInterface;
  80.         foreach ($this->staticRoutes[$trimmedPathinfo] ?? [] as [$ret$requiredHost$requiredMethods$requiredSchemes$hasTrailingSlash, , $condition]) {
  81.             if ($condition && !($this->checkCondition)($condition$context$condition $request ?? $request $this->request ?: $this->createRequest($pathinfo) : null)) {
  82.                 continue;
  83.             }
  84.             if ($requiredHost) {
  85.                 if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost$host$hostMatches)) {
  86.                     continue;
  87.                 }
  88.                 if ('#' === $requiredHost[0] && $hostMatches) {
  89.                     $hostMatches['_route'] = $ret['_route'];
  90.                     $ret $this->mergeDefaults($hostMatches$ret);
  91.                 }
  92.             }
  93.             if ('/' !== $pathinfo && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
  94.                 if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
  95.                     return $allow $allowSchemes = [];
  96.                 }
  97.                 continue;
  98.             }
  99.             $hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
  100.             if ($hasRequiredScheme && $requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
  101.                 $allow += $requiredMethods;
  102.                 continue;
  103.             }
  104.             if (!$hasRequiredScheme) {
  105.                 $allowSchemes += $requiredSchemes;
  106.                 continue;
  107.             }
  108.             return $ret;
  109.         }
  110.         $matchedPathinfo $this->matchHost $host.'.'.$pathinfo $pathinfo;
  111.         foreach ($this->regexpList as $offset => $regex) {
  112.             while (preg_match($regex$matchedPathinfo$matches)) {
  113.                 foreach ($this->dynamicRoutes[$m = (int) $matches['MARK']] as [$ret$vars$requiredMethods$requiredSchemes$hasTrailingSlash$hasTrailingVar$condition]) {
  114.                     if (null !== $condition) {
  115.                         if (=== $condition) { // marks the last route in the regexp
  116.                             continue 3;
  117.                         }
  118.                         if (!($this->checkCondition)($condition$context$condition $request ?? $request $this->request ?: $this->createRequest($pathinfo) : null)) {
  119.                             continue;
  120.                         }
  121.                     }
  122.                     $hasTrailingVar $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
  123.                     if ($hasTrailingVar && ($hasTrailingSlash || (null === $n $matches[\count($vars)] ?? null) || '/' !== ($n[-1] ?? '/')) && preg_match($regex$this->matchHost $host.'.'.$trimmedPathinfo $trimmedPathinfo$n) && $m === (int) $n['MARK']) {
  124.                         if ($hasTrailingSlash) {
  125.                             $matches $n;
  126.                         } else {
  127.                             $hasTrailingVar false;
  128.                         }
  129.                     }
  130.                     if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
  131.                         if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
  132.                             return $allow $allowSchemes = [];
  133.                         }
  134.                         continue;
  135.                     }
  136.                     foreach ($vars as $i => $v) {
  137.                         if (isset($matches[$i])) {
  138.                             $ret[$v] = $matches[$i];
  139.                         }
  140.                     }
  141.                     if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
  142.                         $allowSchemes += $requiredSchemes;
  143.                         continue;
  144.                     }
  145.                     if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
  146.                         $allow += $requiredMethods;
  147.                         continue;
  148.                     }
  149.                     return $ret;
  150.                 }
  151.                 $regex substr_replace($regex'F'$m $offset+ \strlen($m));
  152.                 $offset += \strlen($m);
  153.             }
  154.         }
  155.         if ('/' === $pathinfo && !$allow && !$allowSchemes) {
  156.             throw new NoConfigurationException();
  157.         }
  158.         return [];
  159.     }
  160. }