RouteCompiler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_compiled = parent::compile($route);
  30. // The Drupal-specific compiled information.
  31. $stripped_path = static::getPathWithoutDefaults($route);
  32. $fit = static::getFit($stripped_path);
  33. $pattern_outline = static::getPatternOutline($stripped_path);
  34. // We count the number of parts including any optional trailing parts. This
  35. // allows the RouteProvider to filter candidate routes more efficiently.
  36. $num_parts = count(explode('/', trim($route->getPath(), '/')));
  37. return new CompiledRoute(
  38. $fit,
  39. $pattern_outline,
  40. $num_parts,
  41. // The following parameters are what Symfony uses in
  42. // \Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection().
  43. // Set the static prefix to an empty string since it is redundant to
  44. // the matching in \Drupal\Core\Routing\RouteProvider::getRoutesByPath()
  45. // and by skipping it we more easily make the routing case-insensitive.
  46. '',
  47. $symfony_compiled->getRegex(),
  48. $symfony_compiled->getTokens(),
  49. $symfony_compiled->getPathVariables(),
  50. $symfony_compiled->getHostRegex(),
  51. $symfony_compiled->getHostTokens(),
  52. $symfony_compiled->getHostVariables(),
  53. $symfony_compiled->getVariables()
  54. );
  55. }
  56. /**
  57. * Returns the pattern outline.
  58. *
  59. * The pattern outline is the path pattern but normalized so that all
  60. * placeholders are the string '%'.
  61. *
  62. * @param string $path
  63. * The path for which we want the normalized outline.
  64. *
  65. * @return string
  66. * The path pattern outline.
  67. */
  68. public static function getPatternOutline($path) {
  69. return preg_replace('#\{\w+\}#', '%', $path);
  70. }
  71. /**
  72. * Determines the fitness of the provided path.
  73. *
  74. * @param string $path
  75. * The path whose fitness we want.
  76. *
  77. * @return int
  78. * The fitness of the path, as an integer.
  79. */
  80. public static function getFit($path) {
  81. $parts = explode('/', trim($path, '/'));
  82. $number_parts = count($parts);
  83. // We store the highest index of parts here to save some work in the fit
  84. // calculation loop.
  85. $slashes = $number_parts - 1;
  86. // The fit value is a binary number which has 1 at every fixed path
  87. // position and 0 where there is a wildcard. We keep track of all such
  88. // patterns that exist so that we can minimize the number of path
  89. // patterns we need to check in the RouteProvider.
  90. $fit = 0;
  91. foreach ($parts as $k => $part) {
  92. if (strpos($part, '{') === FALSE) {
  93. $fit |= 1 << ($slashes - $k);
  94. }
  95. }
  96. return $fit;
  97. }
  98. /**
  99. * Returns the path of the route, without placeholders with a default value.
  100. *
  101. * When computing the path outline and fit, we want to skip default-value
  102. * placeholders. If we didn't, the path would never match. Note that this
  103. * only works for placeholders at the end of the path. Infix placeholders
  104. * with default values don't make sense anyway, so that should not be a
  105. * problem.
  106. *
  107. * @param \Symfony\Component\Routing\Route $route
  108. * The route to have the placeholders removed from.
  109. *
  110. * @return string
  111. * The path string, stripped of placeholders that have default values.
  112. */
  113. public static function getPathWithoutDefaults(Route $route) {
  114. $path = $route->getPath();
  115. $defaults = $route->getDefaults();
  116. // Remove placeholders with default values from the outline, so that they
  117. // will still match.
  118. $remove = array_map(function ($a) {
  119. return '/{' . $a . '}';
  120. }, array_keys($defaults));
  121. $path = str_replace($remove, '', $path);
  122. return $path;
  123. }
  124. }