RouteCollection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. /**
  74. * @return void
  75. */
  76. public function add(string $name, Route $route, int $priority = 0)
  77. {
  78. unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
  79. $this->routes[$name] = $route;
  80. if ($priority) {
  81. $this->priorities[$name] = $priority;
  82. }
  83. }
  84. /**
  85. * Returns all routes in this collection.
  86. *
  87. * @return array<string, Route>
  88. */
  89. public function all(): array
  90. {
  91. if ($this->priorities) {
  92. $priorities = $this->priorities;
  93. $keysOrder = array_flip(array_keys($this->routes));
  94. uksort($this->routes, static fn ($n1, $n2) => (($priorities[$n2] ?? 0) <=> ($priorities[$n1] ?? 0)) ?: ($keysOrder[$n1] <=> $keysOrder[$n2]));
  95. }
  96. return $this->routes;
  97. }
  98. /**
  99. * Gets a route by name.
  100. */
  101. public function get(string $name): ?Route
  102. {
  103. $visited = [];
  104. while (null !== $alias = $this->aliases[$name] ?? null) {
  105. if (false !== $searchKey = array_search($name, $visited)) {
  106. $visited[] = $name;
  107. throw new RouteCircularReferenceException($name, \array_slice($visited, $searchKey));
  108. }
  109. if ($alias->isDeprecated()) {
  110. $deprecation = $alias->getDeprecation($name);
  111. trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
  112. }
  113. $visited[] = $name;
  114. $name = $alias->getId();
  115. }
  116. return $this->routes[$name] ?? null;
  117. }
  118. /**
  119. * Removes a route or an array of routes by name from the collection.
  120. *
  121. * @param string|string[] $name The route name or an array of route names
  122. *
  123. * @return void
  124. */
  125. public function remove(string|array $name)
  126. {
  127. $routes = [];
  128. foreach ((array) $name as $n) {
  129. if (isset($this->routes[$n])) {
  130. $routes[] = $n;
  131. }
  132. unset($this->routes[$n], $this->priorities[$n], $this->aliases[$n]);
  133. }
  134. if (!$routes) {
  135. return;
  136. }
  137. foreach ($this->aliases as $k => $alias) {
  138. if (\in_array($alias->getId(), $routes, true)) {
  139. unset($this->aliases[$k]);
  140. }
  141. }
  142. }
  143. /**
  144. * Adds a route collection at the end of the current set by appending all
  145. * routes of the added collection.
  146. *
  147. * @return void
  148. */
  149. public function addCollection(self $collection)
  150. {
  151. // we need to remove all routes with the same names first because just replacing them
  152. // would not place the new route at the end of the merged array
  153. foreach ($collection->all() as $name => $route) {
  154. unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
  155. $this->routes[$name] = $route;
  156. if (isset($collection->priorities[$name])) {
  157. $this->priorities[$name] = $collection->priorities[$name];
  158. }
  159. }
  160. foreach ($collection->getAliases() as $name => $alias) {
  161. unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
  162. $this->aliases[$name] = $alias;
  163. }
  164. foreach ($collection->getResources() as $resource) {
  165. $this->addResource($resource);
  166. }
  167. }
  168. /**
  169. * Adds a prefix to the path of all child routes.
  170. *
  171. * @return void
  172. */
  173. public function addPrefix(string $prefix, array $defaults = [], array $requirements = [])
  174. {
  175. $prefix = trim(trim($prefix), '/');
  176. if ('' === $prefix) {
  177. return;
  178. }
  179. foreach ($this->routes as $route) {
  180. $route->setPath('/'.$prefix.$route->getPath());
  181. $route->addDefaults($defaults);
  182. $route->addRequirements($requirements);
  183. }
  184. }
  185. /**
  186. * Adds a prefix to the name of all the routes within in the collection.
  187. *
  188. * @return void
  189. */
  190. public function addNamePrefix(string $prefix)
  191. {
  192. $prefixedRoutes = [];
  193. $prefixedPriorities = [];
  194. $prefixedAliases = [];
  195. foreach ($this->routes as $name => $route) {
  196. $prefixedRoutes[$prefix.$name] = $route;
  197. if (null !== $canonicalName = $route->getDefault('_canonical_route')) {
  198. $route->setDefault('_canonical_route', $prefix.$canonicalName);
  199. }
  200. if (isset($this->priorities[$name])) {
  201. $prefixedPriorities[$prefix.$name] = $this->priorities[$name];
  202. }
  203. }
  204. foreach ($this->aliases as $name => $alias) {
  205. $prefixedAliases[$prefix.$name] = $alias->withId($prefix.$alias->getId());
  206. }
  207. $this->routes = $prefixedRoutes;
  208. $this->priorities = $prefixedPriorities;
  209. $this->aliases = $prefixedAliases;
  210. }
  211. /**
  212. * Sets the host pattern on all routes.
  213. *
  214. * @return void
  215. */
  216. public function setHost(?string $pattern, array $defaults = [], array $requirements = [])
  217. {
  218. foreach ($this->routes as $route) {
  219. $route->setHost($pattern);
  220. $route->addDefaults($defaults);
  221. $route->addRequirements($requirements);
  222. }
  223. }
  224. /**
  225. * Sets a condition on all routes.
  226. *
  227. * Existing conditions will be overridden.
  228. *
  229. * @return void
  230. */
  231. public function setCondition(?string $condition)
  232. {
  233. foreach ($this->routes as $route) {
  234. $route->setCondition($condition);
  235. }
  236. }
  237. /**
  238. * Adds defaults to all routes.
  239. *
  240. * An existing default value under the same name in a route will be overridden.
  241. *
  242. * @return void
  243. */
  244. public function addDefaults(array $defaults)
  245. {
  246. if ($defaults) {
  247. foreach ($this->routes as $route) {
  248. $route->addDefaults($defaults);
  249. }
  250. }
  251. }
  252. /**
  253. * Adds requirements to all routes.
  254. *
  255. * An existing requirement under the same name in a route will be overridden.
  256. *
  257. * @return void
  258. */
  259. public function addRequirements(array $requirements)
  260. {
  261. if ($requirements) {
  262. foreach ($this->routes as $route) {
  263. $route->addRequirements($requirements);
  264. }
  265. }
  266. }
  267. /**
  268. * Adds options to all routes.
  269. *
  270. * An existing option value under the same name in a route will be overridden.
  271. *
  272. * @return void
  273. */
  274. public function addOptions(array $options)
  275. {
  276. if ($options) {
  277. foreach ($this->routes as $route) {
  278. $route->addOptions($options);
  279. }
  280. }
  281. }
  282. /**
  283. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  284. *
  285. * @param string|string[] $schemes The scheme or an array of schemes
  286. *
  287. * @return void
  288. */
  289. public function setSchemes(string|array $schemes)
  290. {
  291. foreach ($this->routes as $route) {
  292. $route->setSchemes($schemes);
  293. }
  294. }
  295. /**
  296. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  297. *
  298. * @param string|string[] $methods The method or an array of methods
  299. *
  300. * @return void
  301. */
  302. public function setMethods(string|array $methods)
  303. {
  304. foreach ($this->routes as $route) {
  305. $route->setMethods($methods);
  306. }
  307. }
  308. /**
  309. * Returns an array of resources loaded to build this collection.
  310. *
  311. * @return ResourceInterface[]
  312. */
  313. public function getResources(): array
  314. {
  315. return array_values($this->resources);
  316. }
  317. /**
  318. * Adds a resource for this collection. If the resource already exists
  319. * it is not added.
  320. *
  321. * @return void
  322. */
  323. public function addResource(ResourceInterface $resource)
  324. {
  325. $key = (string) $resource;
  326. if (!isset($this->resources[$key])) {
  327. $this->resources[$key] = $resource;
  328. }
  329. }
  330. /**
  331. * Sets an alias for an existing route.
  332. *
  333. * @param string $name The alias to create
  334. * @param string $alias The route to alias
  335. *
  336. * @throws InvalidArgumentException if the alias is for itself
  337. */
  338. public function addAlias(string $name, string $alias): Alias
  339. {
  340. if ($name === $alias) {
  341. throw new InvalidArgumentException(\sprintf('Route alias "%s" can not reference itself.', $name));
  342. }
  343. unset($this->routes[$name], $this->priorities[$name]);
  344. return $this->aliases[$name] = new Alias($alias);
  345. }
  346. /**
  347. * @return array<string, Alias>
  348. */
  349. public function getAliases(): array
  350. {
  351. return $this->aliases;
  352. }
  353. public function getAlias(string $name): ?Alias
  354. {
  355. return $this->aliases[$name] ?? null;
  356. }
  357. public function getPriority(string $name): ?int
  358. {
  359. return $this->priorities[$name] ?? null;
  360. }
  361. }