Route.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /**
  12. * @author Fabien Potencier <fabien@symfony.com>
  13. * @author Alexander M. Turek <me@derrabus.de>
  14. */
  15. #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
  16. class Route
  17. {
  18. private ?string $path = null;
  19. private array $localizedPaths = [];
  20. private array $methods;
  21. private array $schemes;
  22. /**
  23. * @param string|array<string,string>|null $path The route path (i.e. "/user/login")
  24. * @param string|null $name The route name (i.e. "app_user_login")
  25. * @param array<string|\Stringable> $requirements Requirements for the route attributes, @see https://symfony.com/doc/current/routing.html#parameters-validation
  26. * @param array<string, mixed> $options Options for the route (i.e. ['prefix' => '/api'])
  27. * @param array<string, mixed> $defaults Default values for the route attributes and query parameters
  28. * @param string|null $host The host for which this route should be active (i.e. "localhost")
  29. * @param string|string[] $methods The list of HTTP methods allowed by this route
  30. * @param string|string[] $schemes The list of schemes allowed by this route (i.e. "https")
  31. * @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
  32. * @param int|null $priority The priority of the route if multiple ones are defined for the same path
  33. * @param string|null $locale The locale accepted by the route
  34. * @param string|null $format The format returned by the route (i.e. "json", "xml")
  35. * @param bool|null $utf8 Whether the route accepts UTF-8 in its parameters
  36. * @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes
  37. * @param string|null $env The env in which the route is defined (i.e. "dev", "test", "prod")
  38. */
  39. public function __construct(
  40. string|array|null $path = null,
  41. private ?string $name = null,
  42. private array $requirements = [],
  43. private array $options = [],
  44. private array $defaults = [],
  45. private ?string $host = null,
  46. array|string $methods = [],
  47. array|string $schemes = [],
  48. private ?string $condition = null,
  49. private ?int $priority = null,
  50. ?string $locale = null,
  51. ?string $format = null,
  52. ?bool $utf8 = null,
  53. ?bool $stateless = null,
  54. private ?string $env = null,
  55. ) {
  56. if (\is_array($path)) {
  57. $this->localizedPaths = $path;
  58. } else {
  59. $this->path = $path;
  60. }
  61. $this->setMethods($methods);
  62. $this->setSchemes($schemes);
  63. if (null !== $locale) {
  64. $this->defaults['_locale'] = $locale;
  65. }
  66. if (null !== $format) {
  67. $this->defaults['_format'] = $format;
  68. }
  69. if (null !== $utf8) {
  70. $this->options['utf8'] = $utf8;
  71. }
  72. if (null !== $stateless) {
  73. $this->defaults['_stateless'] = $stateless;
  74. }
  75. }
  76. public function setPath(string $path): void
  77. {
  78. $this->path = $path;
  79. }
  80. public function getPath(): ?string
  81. {
  82. return $this->path;
  83. }
  84. public function setLocalizedPaths(array $localizedPaths): void
  85. {
  86. $this->localizedPaths = $localizedPaths;
  87. }
  88. public function getLocalizedPaths(): array
  89. {
  90. return $this->localizedPaths;
  91. }
  92. public function setHost(string $pattern): void
  93. {
  94. $this->host = $pattern;
  95. }
  96. public function getHost(): ?string
  97. {
  98. return $this->host;
  99. }
  100. public function setName(string $name): void
  101. {
  102. $this->name = $name;
  103. }
  104. public function getName(): ?string
  105. {
  106. return $this->name;
  107. }
  108. public function setRequirements(array $requirements): void
  109. {
  110. $this->requirements = $requirements;
  111. }
  112. public function getRequirements(): array
  113. {
  114. return $this->requirements;
  115. }
  116. public function setOptions(array $options): void
  117. {
  118. $this->options = $options;
  119. }
  120. public function getOptions(): array
  121. {
  122. return $this->options;
  123. }
  124. public function setDefaults(array $defaults): void
  125. {
  126. $this->defaults = $defaults;
  127. }
  128. public function getDefaults(): array
  129. {
  130. return $this->defaults;
  131. }
  132. public function setSchemes(array|string $schemes): void
  133. {
  134. $this->schemes = (array) $schemes;
  135. }
  136. public function getSchemes(): array
  137. {
  138. return $this->schemes;
  139. }
  140. public function setMethods(array|string $methods): void
  141. {
  142. $this->methods = (array) $methods;
  143. }
  144. public function getMethods(): array
  145. {
  146. return $this->methods;
  147. }
  148. public function setCondition(?string $condition): void
  149. {
  150. $this->condition = $condition;
  151. }
  152. public function getCondition(): ?string
  153. {
  154. return $this->condition;
  155. }
  156. public function setPriority(int $priority): void
  157. {
  158. $this->priority = $priority;
  159. }
  160. public function getPriority(): ?int
  161. {
  162. return $this->priority;
  163. }
  164. public function setEnv(?string $env): void
  165. {
  166. $this->env = $env;
  167. }
  168. public function getEnv(): ?string
  169. {
  170. return $this->env;
  171. }
  172. }
  173. if (!class_exists(\Symfony\Component\Routing\Annotation\Route::class, false)) {
  174. class_alias(Route::class, \Symfony\Component\Routing\Annotation\Route::class);
  175. }