CompiledRoute.php 4.2 KB

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