TraceableUrlMatcher.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Exception\ExceptionInterface;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16. * TraceableUrlMatcher helps debug path info matching by tracing the match.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class TraceableUrlMatcher extends UrlMatcher
  21. {
  22. public const ROUTE_DOES_NOT_MATCH = 0;
  23. public const ROUTE_ALMOST_MATCHES = 1;
  24. public const ROUTE_MATCHES = 2;
  25. protected $traces;
  26. /**
  27. * @return array
  28. */
  29. public function getTraces(string $pathinfo)
  30. {
  31. $this->traces = [];
  32. try {
  33. $this->match($pathinfo);
  34. } catch (ExceptionInterface) {
  35. }
  36. return $this->traces;
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function getTracesForRequest(Request $request)
  42. {
  43. $this->request = $request;
  44. $traces = $this->getTraces($request->getPathInfo());
  45. $this->request = null;
  46. return $traces;
  47. }
  48. protected function matchCollection(string $pathinfo, RouteCollection $routes): array
  49. {
  50. // HEAD and GET are equivalent as per RFC
  51. if ('HEAD' === $method = $this->context->getMethod()) {
  52. $method = 'GET';
  53. }
  54. $supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
  55. $trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
  56. foreach ($routes as $name => $route) {
  57. $compiledRoute = $route->compile();
  58. $staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
  59. $requiredMethods = $route->getMethods();
  60. // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
  61. if ('' !== $staticPrefix && !str_starts_with($trimmedPathinfo, $staticPrefix)) {
  62. $this->addTrace(\sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  63. continue;
  64. }
  65. $regex = $compiledRoute->getRegex();
  66. $pos = strrpos($regex, '$');
  67. $hasTrailingSlash = '/' === $regex[$pos - 1];
  68. $regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
  69. if (!preg_match($regex, $pathinfo, $matches)) {
  70. // does it match without any requirements?
  71. $r = new Route($route->getPath(), $route->getDefaults(), [], $route->getOptions());
  72. $cr = $r->compile();
  73. if (!preg_match($cr->getRegex(), $pathinfo)) {
  74. $this->addTrace(\sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  75. continue;
  76. }
  77. foreach ($route->getRequirements() as $n => $regex) {
  78. $r = new Route($route->getPath(), $route->getDefaults(), [$n => $regex], $route->getOptions());
  79. $cr = $r->compile();
  80. if (\in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
  81. $this->addTrace(\sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
  82. continue 2;
  83. }
  84. }
  85. continue;
  86. }
  87. $hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{[\w\x80-\xFF]+\}/?$#', $route->getPath());
  88. if ($hasTrailingVar && ($hasTrailingSlash || (null === $m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
  89. if ($hasTrailingSlash) {
  90. $matches = $m;
  91. } else {
  92. $hasTrailingVar = false;
  93. }
  94. }
  95. $hostMatches = [];
  96. if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
  97. $this->addTrace(\sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
  98. continue;
  99. }
  100. $attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
  101. $status = $this->handleRouteRequirements($pathinfo, $name, $route, $attributes);
  102. if (self::REQUIREMENT_MISMATCH === $status[0]) {
  103. $this->addTrace(\sprintf('Condition "%s" does not evaluate to "true"', $route->getCondition()), self::ROUTE_ALMOST_MATCHES, $name, $route);
  104. continue;
  105. }
  106. if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
  107. if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
  108. $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
  109. return $this->allow = $this->allowSchemes = [];
  110. }
  111. $this->addTrace(\sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  112. continue;
  113. }
  114. if ($route->getSchemes() && !$route->hasScheme($this->context->getScheme())) {
  115. $this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
  116. $this->addTrace(\sprintf('Scheme "%s" does not match any of the required schemes (%s)', $this->context->getScheme(), implode(', ', $route->getSchemes())), self::ROUTE_ALMOST_MATCHES, $name, $route);
  117. continue;
  118. }
  119. if ($requiredMethods && !\in_array($method, $requiredMethods)) {
  120. $this->allow = array_merge($this->allow, $requiredMethods);
  121. $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);
  122. continue;
  123. }
  124. $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
  125. return array_replace($attributes, $status[1] ?? []);
  126. }
  127. return [];
  128. }
  129. private function addTrace(string $log, int $level = self::ROUTE_DOES_NOT_MATCH, ?string $name = null, ?Route $route = null): void
  130. {
  131. $this->traces[] = [
  132. 'log' => $log,
  133. 'name' => $name,
  134. 'level' => $level,
  135. 'path' => $route?->getPath(),
  136. ];
  137. }
  138. }