RouteCollection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. use Symfony\Component\Config\Resource\ResourceInterface;
  12. use Symfony\Component\Routing\Exception\InvalidArgumentException;
  13. use Symfony\Component\Routing\Exception\RouteCircularReferenceException;
  14. /**
  15. * A RouteCollection represents a set of Route instances.
  16. *
  17. * When adding a route at the end of the collection, an existing route
  18. * with the same name is removed first. So there can only be one route
  19. * with a given name.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Tobias Schultze <http://tobion.de>
  23. *
  24. * @implements \IteratorAggregate<string, Route>
  25. */
  26. class RouteCollection implements \IteratorAggregate, \Countable
  27. {
  28. /**
  29. * @var array<string, Route>
  30. */
  31. private array $routes = [];
  32. /**
  33. * @var array<string, Alias>
  34. */
  35. private array $aliases = [];
  36. /**
  37. * @var array<string, ResourceInterface>
  38. */
  39. private array $resources = [];
  40. /**
  41. * @var array<string, int>
  42. */
  43. private array $priorities = [];
  44. public function __clone()
  45. {
  46. foreach ($this->routes as $name => $route) {
  47. $this->routes[$name] = clone $route;
  48. }
  49. foreach ($this->aliases as $name => $alias) {
  50. $this->aliases[$name] = clone $alias;
  51. }
  52. }
  53. /**
  54. * Gets the current RouteCollection as an Iterator that includes all routes.
  55. *
  56. * It implements \IteratorAggregate.
  57. *
  58. * @see all()
  59. *
  60. * @return \ArrayIterator<string, Route>
  61. */
  62. public function getIterator(): \ArrayIterator
  63. {
  64. return new \ArrayIterator($this->all());
  65. }
  66. /**
  67. * Gets the number of Routes in this collection.
  68. */
  69. public function count(): int
  70. {
  71. return \count($this->routes);
  72. }
  73. public function add(string $name, Route $route, int $priority = 0): void
  74. {
  75. unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
  76. $this->routes[$name] = $route;
  77. if ($priority) {
  78. $this->priorities[$name] = $priority;
  79. }
  80. }
  81. /**
  82. * Returns all routes in this collection.
  83. *
  84. * @return array<string, Route>
  85. */
  86. public function all(): array
  87. {
  88. if ($this->priorities) {
  89. $priorities = $this->priorities;
  90. $keysOrder = array_flip(array_keys($this->routes));
  91. uksort($this->routes, static fn ($n1, $n2) => (($priorities[$n2] ?? 0) <=> ($priorities[$n1] ?? 0)) ?: ($keysOrder[$n1] <=> $keysOrder[$n2]));
  92. }
  93. return $this->routes;
  94. }
  95. /**
  96. * Gets a route by name.
  97. */
  98. public function get(string $name): ?Route
  99. {
  100. $visited = [];
  101. while (null !== $alias = $this->aliases[$name] ?? null) {
  102. if (false !== $searchKey = array_search($name, $visited)) {
  103. $visited[] = $name;
  104. throw new RouteCircularReferenceException($name, \array_slice($visited, $searchKey));
  105. }
  106. if ($alias->isDeprecated()) {
  107. $deprecation = $alias->getDeprecation($name);
  108. trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
  109. }
  110. $visited[] = $name;
  111. $name = $alias->getId();
  112. }
  113. return $this->routes[$name] ?? null;
  114. }
  115. /**
  116. * Removes a route or an array of routes by name from the collection.
  117. *
  118. * @param string|string[] $name The route name or an array of route names
  119. */
  120. public function remove(string|array $name): void
  121. {
  122. $routes = [];
  123. foreach ((array) $name as $n) {
  124. if (isset($this->routes[$n])) {
  125. $routes[] = $n;
  126. }
  127. unset($this->routes[$n], $this->priorities[$n], $this->aliases[$n]);
  128. }
  129. if (!$routes) {
  130. return;
  131. }
  132. foreach ($this->aliases as $k => $alias) {
  133. if (\in_array($alias->getId(), $routes, true)) {
  134. unset($this->aliases[$k]);
  135. }
  136. }
  137. }
  138. /**
  139. * Adds a route collection at the end of the current set by appending all
  140. * routes of the added collection.
  141. */
  142. public function addCollection(self $collection): void
  143. {
  144. // we need to remove all routes with the same names first because just replacing them
  145. // would not place the new route at the end of the merged array
  146. foreach ($collection->all() as $name => $route) {
  147. unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
  148. $this->routes[$name] = $route;
  149. if (isset($collection->priorities[$name])) {
  150. $this->priorities[$name] = $collection->priorities[$name];
  151. }
  152. }
  153. foreach ($collection->getAliases() as $name => $alias) {
  154. unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
  155. $this->aliases[$name] = $alias;
  156. }
  157. foreach ($collection->getResources() as $resource) {
  158. $this->addResource($resource);
  159. }
  160. }
  161. /**
  162. * Adds a prefix to the path of all child routes.
  163. */
  164. public function addPrefix(string $prefix, array $defaults = [], array $requirements = []): void
  165. {
  166. $prefix = trim(trim($prefix), '/');
  167. if ('' === $prefix) {
  168. return;
  169. }
  170. foreach ($this->routes as $route) {
  171. $route->setPath('/'.$prefix.$route->getPath());
  172. $route->addDefaults($defaults);
  173. $route->addRequirements($requirements);
  174. }
  175. }
  176. /**
  177. * Adds a prefix to the name of all the routes within in the collection.
  178. */
  179. public function addNamePrefix(string $prefix): void
  180. {
  181. $prefixedRoutes = [];
  182. $prefixedPriorities = [];
  183. $prefixedAliases = [];
  184. foreach ($this->routes as $name => $route) {
  185. $prefixedRoutes[$prefix.$name] = $route;
  186. if (null !== $canonicalName = $route->getDefault('_canonical_route')) {
  187. $route->setDefault('_canonical_route', $prefix.$canonicalName);
  188. }
  189. if (isset($this->priorities[$name])) {
  190. $prefixedPriorities[$prefix.$name] = $this->priorities[$name];
  191. }
  192. }
  193. foreach ($this->aliases as $name => $alias) {
  194. $prefixedAliases[$prefix.$name] = $alias->withId($prefix.$alias->getId());
  195. }
  196. $this->routes = $prefixedRoutes;
  197. $this->priorities = $prefixedPriorities;
  198. $this->aliases = $prefixedAliases;
  199. }
  200. /**
  201. * Sets the host pattern on all routes.
  202. */
  203. public function setHost(?string $pattern, array $defaults = [], array $requirements = []): void
  204. {
  205. foreach ($this->routes as $route) {
  206. $route->setHost($pattern);
  207. $route->addDefaults($defaults);
  208. $route->addRequirements($requirements);
  209. }
  210. }
  211. /**
  212. * Sets a condition on all routes.
  213. *
  214. * Existing conditions will be overridden.
  215. */
  216. public function setCondition(?string $condition): void
  217. {
  218. foreach ($this->routes as $route) {
  219. $route->setCondition($condition);
  220. }
  221. }
  222. /**
  223. * Adds defaults to all routes.
  224. *
  225. * An existing default value under the same name in a route will be overridden.
  226. */
  227. public function addDefaults(array $defaults): void
  228. {
  229. if ($defaults) {
  230. foreach ($this->routes as $route) {
  231. $route->addDefaults($defaults);
  232. }
  233. }
  234. }
  235. /**
  236. * Adds requirements to all routes.
  237. *
  238. * An existing requirement under the same name in a route will be overridden.
  239. */
  240. public function addRequirements(array $requirements): void
  241. {
  242. if ($requirements) {
  243. foreach ($this->routes as $route) {
  244. $route->addRequirements($requirements);
  245. }
  246. }
  247. }
  248. /**
  249. * Adds options to all routes.
  250. *
  251. * An existing option value under the same name in a route will be overridden.
  252. */
  253. public function addOptions(array $options): void
  254. {
  255. if ($options) {
  256. foreach ($this->routes as $route) {
  257. $route->addOptions($options);
  258. }
  259. }
  260. }
  261. /**
  262. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  263. *
  264. * @param string|string[] $schemes The scheme or an array of schemes
  265. */
  266. public function setSchemes(string|array $schemes): void
  267. {
  268. foreach ($this->routes as $route) {
  269. $route->setSchemes($schemes);
  270. }
  271. }
  272. /**
  273. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  274. *
  275. * @param string|string[] $methods The method or an array of methods
  276. */
  277. public function setMethods(string|array $methods): void
  278. {
  279. foreach ($this->routes as $route) {
  280. $route->setMethods($methods);
  281. }
  282. }
  283. /**
  284. * Returns an array of resources loaded to build this collection.
  285. *
  286. * @return ResourceInterface[]
  287. */
  288. public function getResources(): array
  289. {
  290. return array_values($this->resources);
  291. }
  292. /**
  293. * Adds a resource for this collection. If the resource already exists
  294. * it is not added.
  295. */
  296. public function addResource(ResourceInterface $resource): void
  297. {
  298. $key = (string) $resource;
  299. if (!isset($this->resources[$key])) {
  300. $this->resources[$key] = $resource;
  301. }
  302. }
  303. /**
  304. * Sets an alias for an existing route.
  305. *
  306. * @param string $name The alias to create
  307. * @param string $alias The route to alias
  308. *
  309. * @throws InvalidArgumentException if the alias is for itself
  310. */
  311. public function addAlias(string $name, string $alias): Alias
  312. {
  313. if ($name === $alias) {
  314. throw new InvalidArgumentException(\sprintf('Route alias "%s" can not reference itself.', $name));
  315. }
  316. unset($this->routes[$name], $this->priorities[$name]);
  317. return $this->aliases[$name] = new Alias($alias);
  318. }
  319. /**
  320. * @return array<string, Alias>
  321. */
  322. public function getAliases(): array
  323. {
  324. return $this->aliases;
  325. }
  326. public function getAlias(string $name): ?Alias
  327. {
  328. return $this->aliases[$name] ?? null;
  329. }
  330. public function getPriority(string $name): ?int
  331. {
  332. return $this->priorities[$name] ?? null;
  333. }
  334. }