Drupal investigation

TraceableUrlMatcher.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. const ROUTE_DOES_NOT_MATCH = 0;
  23. const ROUTE_ALMOST_MATCHES = 1;
  24. const ROUTE_MATCHES = 2;
  25. protected $traces;
  26. public function getTraces($pathinfo)
  27. {
  28. $this->traces = array();
  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($pathinfo, RouteCollection $routes)
  43. {
  44. foreach ($routes as $name => $route) {
  45. $compiledRoute = $route->compile();
  46. if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
  47. // does it match without any requirements?
  48. $r = new Route($route->getPath(), $route->getDefaults(), array(), $route->getOptions());
  49. $cr = $r->compile();
  50. if (!preg_match($cr->getRegex(), $pathinfo)) {
  51. $this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
  52. continue;
  53. }
  54. foreach ($route->getRequirements() as $n => $regex) {
  55. $r = new Route($route->getPath(), $route->getDefaults(), array($n => $regex), $route->getOptions());
  56. $cr = $r->compile();
  57. if (in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
  58. $this->addTrace(sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
  59. continue 2;
  60. }
  61. }
  62. continue;
  63. }
  64. // check host requirement
  65. $hostMatches = array();
  66. if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
  67. $this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
  68. continue;
  69. }
  70. // check HTTP method requirement
  71. if ($requiredMethods = $route->getMethods()) {
  72. // HEAD and GET are equivalent as per RFC
  73. if ('HEAD' === $method = $this->context->getMethod()) {
  74. $method = 'GET';
  75. }
  76. if (!in_array($method, $requiredMethods)) {
  77. $this->allow = array_merge($this->allow, $requiredMethods);
  78. $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);
  79. continue;
  80. }
  81. }
  82. // check condition
  83. if ($condition = $route->getCondition()) {
  84. if (!$this->getExpressionLanguage()->evaluate($condition, array('context' => $this->context, 'request' => $this->request))) {
  85. $this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $condition), self::ROUTE_ALMOST_MATCHES, $name, $route);
  86. continue;
  87. }
  88. }
  89. // check HTTP scheme requirement
  90. if ($requiredSchemes = $route->getSchemes()) {
  91. $scheme = $this->context->getScheme();
  92. if (!$route->hasScheme($scheme)) {
  93. $this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes (%s); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route);
  94. return true;
  95. }
  96. }
  97. $this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
  98. return true;
  99. }
  100. }
  101. private function addTrace($log, $level = self::ROUTE_DOES_NOT_MATCH, $name = null, $route = null)
  102. {
  103. $this->traces[] = array(
  104. 'log' => $log,
  105. 'name' => $name,
  106. 'level' => $level,
  107. 'path' => null !== $route ? $route->getPath() : null,
  108. );
  109. }
  110. }