Route.php 6.5 KB

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