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.

272 lines
7.4 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\Annotation;
  11. /**
  12. * Annotation class for @Route().
  13. *
  14. * @Annotation
  15. * @NamedArgumentConstructor
  16. * @Target({"CLASS", "METHOD"})
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Alexander M. Turek <me@derrabus.de>
  20. */
  21. #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
  22. class Route
  23. {
  24. private $path;
  25. private $localizedPaths = [];
  26. private $name;
  27. private $requirements = [];
  28. private $options = [];
  29. private $defaults = [];
  30. private $host;
  31. private $methods = [];
  32. private $schemes = [];
  33. private $condition;
  34. private $priority;
  35. private $env;
  36. /**
  37. * @param array|string $data data array managed by the Doctrine Annotations library or the path
  38. * @param array|string|null $path
  39. * @param string[] $requirements
  40. * @param string[]|string $methods
  41. * @param string[]|string $schemes
  42. *
  43. * @throws \BadMethodCallException
  44. */
  45. public function __construct(
  46. $data = [],
  47. $path = null,
  48. string $name = null,
  49. array $requirements = [],
  50. array $options = [],
  51. array $defaults = [],
  52. string $host = null,
  53. $methods = [],
  54. $schemes = [],
  55. string $condition = null,
  56. int $priority = null,
  57. string $locale = null,
  58. string $format = null,
  59. bool $utf8 = null,
  60. bool $stateless = null,
  61. string $env = null
  62. ) {
  63. if (\is_string($data)) {
  64. $data = ['path' => $data];
  65. } elseif (!\is_array($data)) {
  66. throw new \TypeError(sprintf('"%s": Argument $data is expected to be a string or array, got "%s".', __METHOD__, get_debug_type($data)));
  67. } elseif ([] !== $data) {
  68. $deprecation = false;
  69. foreach ($data as $key => $val) {
  70. if (\in_array($key, ['path', 'name', 'requirements', 'options', 'defaults', 'host', 'methods', 'schemes', 'condition', 'priority', 'locale', 'format', 'utf8', 'stateless', 'env', 'value'])) {
  71. $deprecation = true;
  72. }
  73. }
  74. if ($deprecation) {
  75. trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
  76. } else {
  77. $localizedPaths = $data;
  78. $data = ['path' => $localizedPaths];
  79. }
  80. }
  81. if (null !== $path && !\is_string($path) && !\is_array($path)) {
  82. throw new \TypeError(sprintf('"%s": Argument $path is expected to be a string, array or null, got "%s".', __METHOD__, get_debug_type($path)));
  83. }
  84. $data['path'] = $data['path'] ?? $path;
  85. $data['name'] = $data['name'] ?? $name;
  86. $data['requirements'] = $data['requirements'] ?? $requirements;
  87. $data['options'] = $data['options'] ?? $options;
  88. $data['defaults'] = $data['defaults'] ?? $defaults;
  89. $data['host'] = $data['host'] ?? $host;
  90. $data['methods'] = $data['methods'] ?? $methods;
  91. $data['schemes'] = $data['schemes'] ?? $schemes;
  92. $data['condition'] = $data['condition'] ?? $condition;
  93. $data['priority'] = $data['priority'] ?? $priority;
  94. $data['locale'] = $data['locale'] ?? $locale;
  95. $data['format'] = $data['format'] ?? $format;
  96. $data['utf8'] = $data['utf8'] ?? $utf8;
  97. $data['stateless'] = $data['stateless'] ?? $stateless;
  98. $data['env'] = $data['env'] ?? $env;
  99. $data = array_filter($data, static function ($value): bool {
  100. return null !== $value;
  101. });
  102. if (isset($data['localized_paths'])) {
  103. throw new \BadMethodCallException(sprintf('Unknown property "localized_paths" on annotation "%s".', static::class));
  104. }
  105. if (isset($data['value'])) {
  106. $data[\is_array($data['value']) ? 'localized_paths' : 'path'] = $data['value'];
  107. unset($data['value']);
  108. }
  109. if (isset($data['path']) && \is_array($data['path'])) {
  110. $data['localized_paths'] = $data['path'];
  111. unset($data['path']);
  112. }
  113. if (isset($data['locale'])) {
  114. $data['defaults']['_locale'] = $data['locale'];
  115. unset($data['locale']);
  116. }
  117. if (isset($data['format'])) {
  118. $data['defaults']['_format'] = $data['format'];
  119. unset($data['format']);
  120. }
  121. if (isset($data['utf8'])) {
  122. $data['options']['utf8'] = filter_var($data['utf8'], \FILTER_VALIDATE_BOOLEAN) ?: false;
  123. unset($data['utf8']);
  124. }
  125. if (isset($data['stateless'])) {
  126. $data['defaults']['_stateless'] = filter_var($data['stateless'], \FILTER_VALIDATE_BOOLEAN) ?: false;
  127. unset($data['stateless']);
  128. }
  129. foreach ($data as $key => $value) {
  130. $method = 'set'.str_replace('_', '', $key);
  131. if (!method_exists($this, $method)) {
  132. throw new \BadMethodCallException(sprintf('Unknown property "%s" on annotation "%s".', $key, static::class));
  133. }
  134. $this->$method($value);
  135. }
  136. }
  137. public function setPath($path)
  138. {
  139. $this->path = $path;
  140. }
  141. public function getPath()
  142. {
  143. return $this->path;
  144. }
  145. public function setLocalizedPaths(array $localizedPaths)
  146. {
  147. $this->localizedPaths = $localizedPaths;
  148. }
  149. public function getLocalizedPaths(): array
  150. {
  151. return $this->localizedPaths;
  152. }
  153. public function setHost($pattern)
  154. {
  155. $this->host = $pattern;
  156. }
  157. public function getHost()
  158. {
  159. return $this->host;
  160. }
  161. public function setName($name)
  162. {
  163. $this->name = $name;
  164. }
  165. public function getName()
  166. {
  167. return $this->name;
  168. }
  169. public function setRequirements($requirements)
  170. {
  171. $this->requirements = $requirements;
  172. }
  173. public function getRequirements()
  174. {
  175. return $this->requirements;
  176. }
  177. public function setOptions($options)
  178. {
  179. $this->options = $options;
  180. }
  181. public function getOptions()
  182. {
  183. return $this->options;
  184. }
  185. public function setDefaults($defaults)
  186. {
  187. $this->defaults = $defaults;
  188. }
  189. public function getDefaults()
  190. {
  191. return $this->defaults;
  192. }
  193. public function setSchemes($schemes)
  194. {
  195. $this->schemes = \is_array($schemes) ? $schemes : [$schemes];
  196. }
  197. public function getSchemes()
  198. {
  199. return $this->schemes;
  200. }
  201. public function setMethods($methods)
  202. {
  203. $this->methods = \is_array($methods) ? $methods : [$methods];
  204. }
  205. public function getMethods()
  206. {
  207. return $this->methods;
  208. }
  209. public function setCondition($condition)
  210. {
  211. $this->condition = $condition;
  212. }
  213. public function getCondition()
  214. {
  215. return $this->condition;
  216. }
  217. public function setPriority(int $priority): void
  218. {
  219. $this->priority = $priority;
  220. }
  221. public function getPriority(): ?int
  222. {
  223. return $this->priority;
  224. }
  225. public function setEnv(?string $env): void
  226. {
  227. $this->env = $env;
  228. }
  229. public function getEnv(): ?string
  230. {
  231. return $this->env;
  232. }
  233. }