CompiledRoute.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  19. * @param string $staticPrefix The static prefix of the compiled route
  20. * @param string $regex The regular expression to use to match this route
  21. * @param array $tokens An array of tokens to use to generate URL for this route
  22. * @param array $pathVariables An array of path variables
  23. * @param string|null $hostRegex Host regex
  24. * @param array $hostTokens Host tokens
  25. * @param array $hostVariables An array of host variables
  26. * @param array $variables An array of variables (variables defined in the path and in the host patterns)
  27. */
  28. public function __construct(
  29. private string $staticPrefix,
  30. private string $regex,
  31. private array $tokens,
  32. private array $pathVariables,
  33. private ?string $hostRegex = null,
  34. private array $hostTokens = [],
  35. private array $hostVariables = [],
  36. private array $variables = [],
  37. ) {
  38. }
  39. public function __serialize(): array
  40. {
  41. return [
  42. 'vars' => $this->variables,
  43. 'path_prefix' => $this->staticPrefix,
  44. 'path_regex' => $this->regex,
  45. 'path_tokens' => $this->tokens,
  46. 'path_vars' => $this->pathVariables,
  47. 'host_regex' => $this->hostRegex,
  48. 'host_tokens' => $this->hostTokens,
  49. 'host_vars' => $this->hostVariables,
  50. ];
  51. }
  52. /**
  53. * @internal
  54. */
  55. final public function serialize(): string
  56. {
  57. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  58. }
  59. public function __unserialize(array $data): void
  60. {
  61. $this->variables = $data['vars'];
  62. $this->staticPrefix = $data['path_prefix'];
  63. $this->regex = $data['path_regex'];
  64. $this->tokens = $data['path_tokens'];
  65. $this->pathVariables = $data['path_vars'];
  66. $this->hostRegex = $data['host_regex'];
  67. $this->hostTokens = $data['host_tokens'];
  68. $this->hostVariables = $data['host_vars'];
  69. }
  70. /**
  71. * @internal
  72. */
  73. final public function unserialize(string $serialized): void
  74. {
  75. $this->__unserialize(unserialize($serialized, ['allowed_classes' => false]));
  76. }
  77. /**
  78. * Returns the static prefix.
  79. */
  80. public function getStaticPrefix(): string
  81. {
  82. return $this->staticPrefix;
  83. }
  84. /**
  85. * Returns the regex.
  86. */
  87. public function getRegex(): string
  88. {
  89. return $this->regex;
  90. }
  91. /**
  92. * Returns the host regex.
  93. */
  94. public function getHostRegex(): ?string
  95. {
  96. return $this->hostRegex;
  97. }
  98. /**
  99. * Returns the tokens.
  100. */
  101. public function getTokens(): array
  102. {
  103. return $this->tokens;
  104. }
  105. /**
  106. * Returns the host tokens.
  107. */
  108. public function getHostTokens(): array
  109. {
  110. return $this->hostTokens;
  111. }
  112. /**
  113. * Returns the variables.
  114. */
  115. public function getVariables(): array
  116. {
  117. return $this->variables;
  118. }
  119. /**
  120. * Returns the path variables.
  121. */
  122. public function getPathVariables(): array
  123. {
  124. return $this->pathVariables;
  125. }
  126. /**
  127. * Returns the host variables.
  128. */
  129. public function getHostVariables(): array
  130. {
  131. return $this->hostVariables;
  132. }
  133. }