[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/routing/Matcher/ -> TraceableUrlMatcher.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Routing\Matcher;
  13  
  14  use Symfony\Component\HttpFoundation\Request;
  15  use Symfony\Component\Routing\Exception\ExceptionInterface;
  16  use Symfony\Component\Routing\Route;
  17  use Symfony\Component\Routing\RouteCollection;
  18  
  19  /**
  20   * TraceableUrlMatcher helps debug path info matching by tracing the match.
  21   *
  22   * @author Fabien Potencier <fabien@symfony.com>
  23   */
  24  class TraceableUrlMatcher extends UrlMatcher
  25  {
  26      const ROUTE_DOES_NOT_MATCH = 0;
  27      const ROUTE_ALMOST_MATCHES = 1;
  28      const ROUTE_MATCHES = 2;
  29  
  30      protected $traces;
  31  
  32      public function getTraces($pathinfo)
  33      {
  34          $this->traces = [];
  35  
  36          try {
  37              $this->match($pathinfo);
  38          } catch (ExceptionInterface $e) {
  39          }
  40  
  41          return $this->traces;
  42      }
  43  
  44      public function getTracesForRequest(Request $request)
  45      {
  46          $this->request = $request;
  47          $traces = $this->getTraces($request->getPathInfo());
  48          $this->request = null;
  49  
  50          return $traces;
  51      }
  52  
  53      protected function matchCollection($pathinfo, RouteCollection $routes)
  54      {
  55          // HEAD and GET are equivalent as per RFC
  56          if ('HEAD' === $method = $this->context->getMethod()) {
  57              $method = 'GET';
  58          }
  59          $supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
  60  
  61          foreach ($routes as $name => $route) {
  62              $compiledRoute = $route->compile();
  63              $staticPrefix = $compiledRoute->getStaticPrefix();
  64              $requiredMethods = $route->getMethods();
  65  
  66              // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
  67              if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
  68                  // no-op
  69              } elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods)) || 'GET' !== $method) {
  70                  $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  71                  continue;
  72              } elseif ('/' === substr($staticPrefix, -1) && substr($staticPrefix, 0, -1) === $pathinfo) {
  73                  $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
  74  
  75                  return $this->allow = [];
  76              } else {
  77                  $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  78                  continue;
  79              }
  80              $regex = $compiledRoute->getRegex();
  81  
  82              if ($supportsTrailingSlash && $pos = strpos($regex, '/$')) {
  83                  $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
  84                  $hasTrailingSlash = true;
  85              } else {
  86                  $hasTrailingSlash = false;
  87              }
  88  
  89              if (!preg_match($regex, $pathinfo, $matches)) {
  90                  // does it match without any requirements?
  91                  $r = new Route($route->getPath(), $route->getDefaults(), [], $route->getOptions());
  92                  $cr = $r->compile();
  93                  if (!preg_match($cr->getRegex(), $pathinfo)) {
  94                      $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  95  
  96                      continue;
  97                  }
  98  
  99                  foreach ($route->getRequirements() as $n => $regex) {
 100                      $r = new Route($route->getPath(), $route->getDefaults(), [$n => $regex], $route->getOptions());
 101                      $cr = $r->compile();
 102  
 103                      if (\in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
 104                          $this->addTrace(sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
 105  
 106                          continue 2;
 107                      }
 108                  }
 109  
 110                  continue;
 111              }
 112  
 113              if ($hasTrailingSlash && '/' !== substr($pathinfo, -1)) {
 114                  if ((!$requiredMethods || \in_array('GET', $requiredMethods)) && 'GET' === $method) {
 115                      $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
 116  
 117                      return $this->allow = [];
 118                  }
 119                  $this->addTrace(sprintf('Method "%s" does not match any of the required methods (%s)', $this->context->getMethod(), implode(', ', $requiredMethods)), self::ROUTE_ALMOST_MATCHES, $name, $route);
 120                  continue;
 121              }
 122  
 123              $hostMatches = [];
 124              if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
 125                  $this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
 126                  continue;
 127              }
 128  
 129              $status = $this->handleRouteRequirements($pathinfo, $name, $route);
 130  
 131              if (self::REQUIREMENT_MISMATCH === $status[0]) {
 132                  if ($route->getCondition()) {
 133                      $this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $route->getCondition()), self::ROUTE_ALMOST_MATCHES, $name, $route);
 134                  } else {
 135                      $this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes (%s); the user will be redirected to first required scheme', $this->getContext()->getScheme(), implode(', ', $route->getSchemes())), self::ROUTE_ALMOST_MATCHES, $name, $route);
 136                  }
 137  
 138                  continue;
 139              }
 140  
 141              // check HTTP method requirement
 142              if ($requiredMethods) {
 143                  if (!\in_array($method, $requiredMethods)) {
 144                      if (self::REQUIREMENT_MATCH === $status[0]) {
 145                          $this->allow = array_merge($this->allow, $requiredMethods);
 146                      }
 147                      $this->addTrace(sprintf('Method "%s" does not match any of the required methods (%s)', $this->context->getMethod(), implode(', ', $requiredMethods)), self::ROUTE_ALMOST_MATCHES, $name, $route);
 148  
 149                      continue;
 150                  }
 151              }
 152  
 153              $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
 154  
 155              return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : []));
 156          }
 157  
 158          return [];
 159      }
 160  
 161      private function addTrace($log, $level = self::ROUTE_DOES_NOT_MATCH, $name = null, $route = null)
 162      {
 163          $this->traces[] = [
 164              'log' => $log,
 165              'name' => $name,
 166              'level' => $level,
 167              'path' => null !== $route ? $route->getPath() : null,
 168          ];
 169      }
 170  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1