RouteCompiler.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Component\Routing\RouteCompilerInterface;
  4. use Symfony\Component\Routing\Route;
  5. use Symfony\Component\Routing\RouteCompiler as SymfonyRouteCompiler;
  6. /**
  7. * Compiler to generate derived information from a Route necessary for matching.
  8. */
  9. class RouteCompiler extends SymfonyRouteCompiler implements RouteCompilerInterface {
  10. /**
  11. * Utility constant to use for regular expressions against the path.
  12. */
  13. const REGEX_DELIMITER = '#';
  14. /**
  15. * Compiles the current route instance.
  16. *
  17. * Because so much of the parent class is private, we need to call the parent
  18. * class's compile() method and then dissect its return value to build our
  19. * new compiled object. If upstream gets refactored so we can subclass more
  20. * easily then this may not be necessary.
  21. *
  22. * @param \Symfony\Component\Routing\Route $route
  23. * A Route instance.
  24. *
  25. * @return \Drupal\Core\Routing\CompiledRoute
  26. * A CompiledRoute instance.
  27. */
  28. public static function compile(Route $route) {
  29. // Symfony 4 requires that all UTF-8 route patterns have the "utf8" option
  30. // set and Drupal does not support non UTF-8 routes.
  31. $route->setOption('utf8', TRUE);
  32. $symfony_compiled = parent::compile($route);
  33. // The Drupal-specific compiled information.
  34. $stripped_path = static::getPathWithoutDefaults($route);
  35. $fit = static::getFit($stripped_path);
  36. $pattern_outline = static::getPatternOutline($stripped_path);
  37. // We count the number of parts including any optional trailing parts. This
  38. // allows the RouteProvider to filter candidate routes more efficiently.
  39. $num_parts = count(explode('/', trim($route->getPath(), '/')));
  40. return new CompiledRoute(
  41. $fit,
  42. $pattern_outline,
  43. $num_parts,
  44. // The following parameters are what Symfony uses in
  45. // \Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection().
  46. // Set the static prefix to an empty string since it is redundant to
  47. // the matching in \Drupal\Core\Routing\RouteProvider::getRoutesByPath()
  48. // and by skipping it we more easily make the routing case-insensitive.
  49. '',
  50. $symfony_compiled->getRegex(),
  51. $symfony_compiled->getTokens(),
  52. $symfony_compiled->getPathVariables(),
  53. $symfony_compiled->getHostRegex(),
  54. $symfony_compiled->getHostTokens(),
  55. $symfony_compiled->getHostVariables(),
  56. $symfony_compiled->getVariables()
  57. );
  58. }
  59. /**
  60. * Returns the pattern outline.
  61. *
  62. * The pattern outline is the path pattern but normalized so that all
  63. * placeholders are the string '%'.
  64. *
  65. * @param string $path
  66. * The path for which we want the normalized outline.
  67. *
  68. * @return string
  69. * The path pattern outline.
  70. */
  71. public static function getPatternOutline($path) {
  72. return preg_replace('#\{\w+\}#', '%', $path);
  73. }
  74. /**
  75. * Determines the fitness of the provided path.
  76. *
  77. * @param string $path
  78. * The path whose fitness we want.
  79. *
  80. * @return int
  81. * The fitness of the path, as an integer.
  82. */
  83. public static function getFit($path) {
  84. $parts = explode('/', trim($path, '/'));
  85. $number_parts = count($parts);
  86. // We store the highest index of parts here to save some work in the fit
  87. // calculation loop.
  88. $slashes = $number_parts - 1;
  89. // The fit value is a binary number which has 1 at every fixed path
  90. // position and 0 where there is a wildcard. We keep track of all such
  91. // patterns that exist so that we can minimize the number of path
  92. // patterns we need to check in the RouteProvider.
  93. $fit = 0;
  94. foreach ($parts as $k => $part) {
  95. if (strpos($part, '{') === FALSE) {
  96. $fit |= 1 << ($slashes - $k);
  97. }
  98. }
  99. return $fit;
  100. }
  101. /**
  102. * Returns the path of the route, without placeholders with a default value.
  103. *
  104. * When computing the path outline and fit, we want to skip default-value
  105. * placeholders. If we didn't, the path would never match. Note that this
  106. * only works for placeholders at the end of the path. Infix placeholders
  107. * with default values don't make sense anyway, so that should not be a
  108. * problem.
  109. *
  110. * @param \Symfony\Component\Routing\Route $route
  111. * The route to have the placeholders removed from.
  112. *
  113. * @return string
  114. * The path string, stripped of placeholders that have default values.
  115. */
  116. public static function getPathWithoutDefaults(Route $route) {
  117. $path = $route->getPath();
  118. $defaults = $route->getDefaults();
  119. // Remove placeholders with default values from the outline, so that they
  120. // will still match.
  121. $remove = array_map(function ($a) {
  122. return '/{' . $a . '}';
  123. }, array_keys($defaults));
  124. $path = str_replace($remove, '', $path);
  125. return $path;
  126. }
  127. }