CompiledRoute.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Drupal\Component\Utility\Unicode;
  4. use Symfony\Component\Routing\CompiledRoute as SymfonyCompiledRoute;
  5. /**
  6. * A compiled route contains derived information from a route object.
  7. */
  8. class CompiledRoute extends SymfonyCompiledRoute {
  9. /**
  10. * The fitness of this route.
  11. *
  12. * @var int
  13. */
  14. protected $fit;
  15. /**
  16. * The pattern outline of this route.
  17. *
  18. * @var string
  19. */
  20. protected $patternOutline;
  21. /**
  22. * The number of parts in the path of this route.
  23. *
  24. * @var int
  25. */
  26. protected $numParts;
  27. /**
  28. * Constructs a new compiled route object.
  29. *
  30. * This is a ridiculously long set of constructor parameters, but as this
  31. * object is little more than a collection of values it's not a serious
  32. * problem. The parent Symfony class does the same, as well, making it
  33. * difficult to override differently.
  34. *
  35. * @param int $fit
  36. * The fitness of the route.
  37. * @param string $pattern_outline
  38. * The pattern outline for this route.
  39. * @param int $num_parts
  40. * The number of parts in the path.
  41. * @param string $staticPrefix
  42. * The static prefix of the compiled route
  43. * @param string $regex
  44. * The regular expression to use to match this route
  45. * @param array $tokens
  46. * An array of tokens to use to generate URL for this route
  47. * @param array $pathVariables
  48. * An array of path variables
  49. * @param string|null $hostRegex
  50. * Host regex
  51. * @param array $hostTokens
  52. * Host tokens
  53. * @param array $hostVariables
  54. * An array of host variables
  55. * @param array $variables
  56. * An array of variables (variables defined in the path and in the host patterns)
  57. */
  58. public function __construct($fit, $pattern_outline, $num_parts, $staticPrefix, $regex, array $tokens, array $pathVariables, $hostRegex = NULL, array $hostTokens = [], array $hostVariables = [], array $variables = []) {
  59. parent::__construct($staticPrefix, $regex, $tokens, $pathVariables, $hostRegex, $hostTokens, $hostVariables, $variables);
  60. $this->fit = $fit;
  61. // Support case-insensitive route matching by ensuring the pattern outline
  62. // is lowercase.
  63. // @see \Drupal\Core\Routing\RouteProvider::getRoutesByPath()
  64. $this->patternOutline = Unicode::strtolower($pattern_outline);
  65. $this->numParts = $num_parts;
  66. }
  67. /**
  68. * Returns the fit of this route.
  69. *
  70. * See RouteCompiler for a definition of how the fit is calculated.
  71. *
  72. * @return int
  73. * The fit of the route.
  74. */
  75. public function getFit() {
  76. return $this->fit;
  77. }
  78. /**
  79. * Returns the number of parts in this route's path.
  80. *
  81. * The string "foo/bar/baz" has 3 parts, regardless of how many of them are
  82. * placeholders.
  83. *
  84. * @return int
  85. * The number of parts in the path.
  86. */
  87. public function getNumParts() {
  88. return $this->numParts;
  89. }
  90. /**
  91. * Returns the pattern outline of this route.
  92. *
  93. * The pattern outline of a route is the path pattern of the route, but
  94. * normalized such that all placeholders are replaced with %.
  95. *
  96. * @return string
  97. * The normalized path pattern.
  98. */
  99. public function getPatternOutline() {
  100. return $this->patternOutline;
  101. }
  102. /**
  103. * Returns the options.
  104. *
  105. * @return array
  106. * The options.
  107. */
  108. public function getOptions() {
  109. return $this->route->getOptions();
  110. }
  111. /**
  112. * Returns the defaults.
  113. *
  114. * @return array
  115. * The defaults.
  116. */
  117. public function getDefaults() {
  118. return $this->route->getDefaults();
  119. }
  120. /**
  121. * Returns the requirements.
  122. *
  123. * @return array
  124. * The requirements.
  125. */
  126. public function getRequirements() {
  127. return $this->route->getRequirements();
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function serialize() {
  133. // Calling the parent method is safer than trying to optimize out the extra
  134. // function calls.
  135. $data = unserialize(parent::serialize());
  136. $data['fit'] = $this->fit;
  137. $data['patternOutline'] = $this->patternOutline;
  138. $data['numParts'] = $this->numParts;
  139. return serialize($data);
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function unserialize($serialized) {
  145. parent::unserialize($serialized);
  146. $data = unserialize($serialized);
  147. $this->fit = $data['fit'];
  148. $this->patternOutline = $data['patternOutline'];
  149. $this->numParts = $data['numParts'];
  150. }
  151. }