RouteCollection.php 10 KB

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