You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.7 KiB

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