Route.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Attribute;
  11. use Symfony\Component\Routing\Exception\LogicException;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. * @author Alexander M. Turek <me@derrabus.de>
  15. */
  16. #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
  17. class Route
  18. {
  19. /** @var string[] */
  20. public array $methods;
  21. /** @var string[] */
  22. public array $envs;
  23. /** @var string[] */
  24. public array $schemes;
  25. /** @var (string|DeprecatedAlias)[] */
  26. public array $aliases = [];
  27. /**
  28. * @param string|array<string,string>|null $path The route path (i.e. "/user/login")
  29. * @param string|null $name The route name (i.e. "app_user_login")
  30. * @param array<string|\Stringable> $requirements Requirements for the route attributes, @see https://symfony.com/doc/current/routing.html#parameters-validation
  31. * @param array<string, mixed> $options Options for the route (i.e. ['prefix' => '/api'])
  32. * @param array<string, mixed> $defaults Default values for the route attributes and query parameters
  33. * @param string|null $host The host for which this route should be active (i.e. "localhost")
  34. * @param string|string[] $methods The list of HTTP methods allowed by this route
  35. * @param string|string[] $schemes The list of schemes allowed by this route (i.e. "https")
  36. * @param string|null $condition An expression that must evaluate to true for the route to be matched, @see https://symfony.com/doc/current/routing.html#matching-expressions
  37. * @param int|null $priority The priority of the route if multiple ones are defined for the same path
  38. * @param string|null $locale The locale accepted by the route
  39. * @param string|null $format The format returned by the route (i.e. "json", "xml")
  40. * @param bool|null $utf8 Whether the route accepts UTF-8 in its parameters
  41. * @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes
  42. * @param string|string[]|null $env The env(s) in which the route is defined (i.e. "dev", "test", "prod", ["dev", "test"])
  43. * @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $alias The list of aliases for this route
  44. */
  45. public function __construct(
  46. public string|array|null $path = null,
  47. public ?string $name = null,
  48. public array $requirements = [],
  49. public array $options = [],
  50. public array $defaults = [],
  51. public ?string $host = null,
  52. array|string $methods = [],
  53. array|string $schemes = [],
  54. public ?string $condition = null,
  55. public ?int $priority = null,
  56. ?string $locale = null,
  57. ?string $format = null,
  58. ?bool $utf8 = null,
  59. ?bool $stateless = null,
  60. string|array|null $env = null,
  61. string|DeprecatedAlias|array $alias = [],
  62. ) {
  63. $this->path = $path;
  64. $this->methods = (array) $methods;
  65. $this->schemes = (array) $schemes;
  66. $this->envs = (array) $env;
  67. $this->aliases = \is_array($alias) ? $alias : [$alias];
  68. if (null !== $locale) {
  69. $this->defaults['_locale'] = $locale;
  70. }
  71. if (null !== $format) {
  72. $this->defaults['_format'] = $format;
  73. }
  74. if (null !== $utf8) {
  75. $this->options['utf8'] = $utf8;
  76. }
  77. if (null !== $stateless) {
  78. $this->defaults['_stateless'] = $stateless;
  79. }
  80. }
  81. #[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
  82. public function setPath(string $path): void
  83. {
  84. $this->path = $path;
  85. }
  86. #[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
  87. public function getPath(): ?string
  88. {
  89. return \is_array($this->path) ? null : $this->path;
  90. }
  91. #[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
  92. public function setLocalizedPaths(array $localizedPaths): void
  93. {
  94. $this->path = $localizedPaths;
  95. }
  96. #[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
  97. public function getLocalizedPaths(): array
  98. {
  99. return \is_array($this->path) ? $this->path : [];
  100. }
  101. #[\Deprecated('Use the "host" property instead', 'symfony/routing:7.4')]
  102. public function setHost(string $pattern): void
  103. {
  104. $this->host = $pattern;
  105. }
  106. #[\Deprecated('Use the "host" property instead', 'symfony/routing:7.4')]
  107. public function getHost(): ?string
  108. {
  109. return $this->host;
  110. }
  111. #[\Deprecated('Use the "name" property instead', 'symfony/routing:7.4')]
  112. public function setName(string $name): void
  113. {
  114. $this->name = $name;
  115. }
  116. #[\Deprecated('Use the "name" property instead', 'symfony/routing:7.4')]
  117. public function getName(): ?string
  118. {
  119. return $this->name;
  120. }
  121. #[\Deprecated('Use the "requirements" property instead', 'symfony/routing:7.4')]
  122. public function setRequirements(array $requirements): void
  123. {
  124. $this->requirements = $requirements;
  125. }
  126. #[\Deprecated('Use the "requirements" property instead', 'symfony/routing:7.4')]
  127. public function getRequirements(): array
  128. {
  129. return $this->requirements;
  130. }
  131. #[\Deprecated('Use the "options" property instead', 'symfony/routing:7.4')]
  132. public function setOptions(array $options): void
  133. {
  134. $this->options = $options;
  135. }
  136. #[\Deprecated('Use the "options" property instead', 'symfony/routing:7.4')]
  137. public function getOptions(): array
  138. {
  139. return $this->options;
  140. }
  141. #[\Deprecated('Use the "defaults" property instead', 'symfony/routing:7.4')]
  142. public function setDefaults(array $defaults): void
  143. {
  144. $this->defaults = $defaults;
  145. }
  146. #[\Deprecated('Use the "defaults" property instead', 'symfony/routing:7.4')]
  147. public function getDefaults(): array
  148. {
  149. return $this->defaults;
  150. }
  151. #[\Deprecated('Use the "schemes" property instead', 'symfony/routing:7.4')]
  152. public function setSchemes(array|string $schemes): void
  153. {
  154. $this->schemes = (array) $schemes;
  155. }
  156. #[\Deprecated('Use the "schemes" property instead', 'symfony/routing:7.4')]
  157. public function getSchemes(): array
  158. {
  159. return $this->schemes;
  160. }
  161. #[\Deprecated('Use the "methods" property instead', 'symfony/routing:7.4')]
  162. public function setMethods(array|string $methods): void
  163. {
  164. $this->methods = (array) $methods;
  165. }
  166. #[\Deprecated('Use the "methods" property instead', 'symfony/routing:7.4')]
  167. public function getMethods(): array
  168. {
  169. return $this->methods;
  170. }
  171. #[\Deprecated('Use the "condition" property instead', 'symfony/routing:7.4')]
  172. public function setCondition(?string $condition): void
  173. {
  174. $this->condition = $condition;
  175. }
  176. #[\Deprecated('Use the "condition" property instead', 'symfony/routing:7.4')]
  177. public function getCondition(): ?string
  178. {
  179. return $this->condition;
  180. }
  181. #[\Deprecated('Use the "priority" property instead', 'symfony/routing:7.4')]
  182. public function setPriority(int $priority): void
  183. {
  184. $this->priority = $priority;
  185. }
  186. #[\Deprecated('Use the "priority" property instead', 'symfony/routing:7.4')]
  187. public function getPriority(): ?int
  188. {
  189. return $this->priority;
  190. }
  191. #[\Deprecated('Use the "envs" property instead', 'symfony/routing:7.4')]
  192. public function setEnv(?string $env): void
  193. {
  194. $this->envs = (array) $env;
  195. }
  196. #[\Deprecated('Use the "envs" property instead', 'symfony/routing:7.4')]
  197. public function getEnv(): ?string
  198. {
  199. if (!$this->envs) {
  200. return null;
  201. }
  202. if (\count($this->envs) > 1) {
  203. throw new LogicException(\sprintf('The "env" property has %d environments. Use "getEnvs()" to get all of them.', \count($this->envs)));
  204. }
  205. return $this->envs[0];
  206. }
  207. /**
  208. * @return (string|DeprecatedAlias)[]
  209. */
  210. #[\Deprecated('Use the "aliases" property instead', 'symfony/routing:7.4')]
  211. public function getAliases(): array
  212. {
  213. return $this->aliases;
  214. }
  215. /**
  216. * @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $aliases
  217. */
  218. #[\Deprecated('Use the "aliases" property instead', 'symfony/routing:7.4')]
  219. public function setAliases(string|DeprecatedAlias|array $aliases): void
  220. {
  221. $this->aliases = \is_array($aliases) ? $aliases : [$aliases];
  222. }
  223. }
  224. if (!class_exists(\Symfony\Component\Routing\Annotation\Route::class, false)) {
  225. class_alias(Route::class, \Symfony\Component\Routing\Annotation\Route::class);
  226. }