You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.3 KiB

3 years ago
  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;
  11. /**
  12. * CompiledRoutes are returned by the RouteCompiler class.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class CompiledRoute implements \Serializable
  17. {
  18. private $variables;
  19. private $tokens;
  20. private $staticPrefix;
  21. private $regex;
  22. private $pathVariables;
  23. private $hostVariables;
  24. private $hostRegex;
  25. private $hostTokens;
  26. /**
  27. * @param string $staticPrefix The static prefix of the compiled route
  28. * @param string $regex The regular expression to use to match this route
  29. * @param array $tokens An array of tokens to use to generate URL for this route
  30. * @param array $pathVariables An array of path variables
  31. * @param string|null $hostRegex Host regex
  32. * @param array $hostTokens Host tokens
  33. * @param array $hostVariables An array of host variables
  34. * @param array $variables An array of variables (variables defined in the path and in the host patterns)
  35. */
  36. public function __construct(string $staticPrefix, string $regex, array $tokens, array $pathVariables, string $hostRegex = null, array $hostTokens = [], array $hostVariables = [], array $variables = [])
  37. {
  38. $this->staticPrefix = $staticPrefix;
  39. $this->regex = $regex;
  40. $this->tokens = $tokens;
  41. $this->pathVariables = $pathVariables;
  42. $this->hostRegex = $hostRegex;
  43. $this->hostTokens = $hostTokens;
  44. $this->hostVariables = $hostVariables;
  45. $this->variables = $variables;
  46. }
  47. public function __serialize(): array
  48. {
  49. return [
  50. 'vars' => $this->variables,
  51. 'path_prefix' => $this->staticPrefix,
  52. 'path_regex' => $this->regex,
  53. 'path_tokens' => $this->tokens,
  54. 'path_vars' => $this->pathVariables,
  55. 'host_regex' => $this->hostRegex,
  56. 'host_tokens' => $this->hostTokens,
  57. 'host_vars' => $this->hostVariables,
  58. ];
  59. }
  60. /**
  61. * @internal
  62. */
  63. final public function serialize(): string
  64. {
  65. return serialize($this->__serialize());
  66. }
  67. public function __unserialize(array $data): void
  68. {
  69. $this->variables = $data['vars'];
  70. $this->staticPrefix = $data['path_prefix'];
  71. $this->regex = $data['path_regex'];
  72. $this->tokens = $data['path_tokens'];
  73. $this->pathVariables = $data['path_vars'];
  74. $this->hostRegex = $data['host_regex'];
  75. $this->hostTokens = $data['host_tokens'];
  76. $this->hostVariables = $data['host_vars'];
  77. }
  78. /**
  79. * @internal
  80. */
  81. final public function unserialize($serialized)
  82. {
  83. $this->__unserialize(unserialize($serialized, ['allowed_classes' => false]));
  84. }
  85. /**
  86. * Returns the static prefix.
  87. *
  88. * @return string The static prefix
  89. */
  90. public function getStaticPrefix()
  91. {
  92. return $this->staticPrefix;
  93. }
  94. /**
  95. * Returns the regex.
  96. *
  97. * @return string The regex
  98. */
  99. public function getRegex()
  100. {
  101. return $this->regex;
  102. }
  103. /**
  104. * Returns the host regex.
  105. *
  106. * @return string|null The host regex or null
  107. */
  108. public function getHostRegex()
  109. {
  110. return $this->hostRegex;
  111. }
  112. /**
  113. * Returns the tokens.
  114. *
  115. * @return array The tokens
  116. */
  117. public function getTokens()
  118. {
  119. return $this->tokens;
  120. }
  121. /**
  122. * Returns the host tokens.
  123. *
  124. * @return array The tokens
  125. */
  126. public function getHostTokens()
  127. {
  128. return $this->hostTokens;
  129. }
  130. /**
  131. * Returns the variables.
  132. *
  133. * @return array The variables
  134. */
  135. public function getVariables()
  136. {
  137. return $this->variables;
  138. }
  139. /**
  140. * Returns the path variables.
  141. *
  142. * @return array The variables
  143. */
  144. public function getPathVariables()
  145. {
  146. return $this->pathVariables;
  147. }
  148. /**
  149. * Returns the host variables.
  150. *
  151. * @return array The variables
  152. */
  153. public function getHostVariables()
  154. {
  155. return $this->hostVariables;
  156. }
  157. }