RouteCollection.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /**
  13. * A RouteCollection represents a set of Route instances.
  14. *
  15. * When adding a route at the end of the collection, an existing route
  16. * with the same name is removed first. So there can only be one route
  17. * with a given name.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Tobias Schultze <http://tobion.de>
  21. */
  22. class RouteCollection implements \IteratorAggregate, \Countable
  23. {
  24. /**
  25. * @var Route[]
  26. */
  27. private $routes = [];
  28. /**
  29. * @var array
  30. */
  31. private $resources = [];
  32. public function __clone()
  33. {
  34. foreach ($this->routes as $name => $route) {
  35. $this->routes[$name] = clone $route;
  36. }
  37. }
  38. /**
  39. * Gets the current RouteCollection as an Iterator that includes all routes.
  40. *
  41. * It implements \IteratorAggregate.
  42. *
  43. * @see all()
  44. *
  45. * @return \ArrayIterator|Route[] An \ArrayIterator object for iterating over routes
  46. */
  47. public function getIterator()
  48. {
  49. return new \ArrayIterator($this->routes);
  50. }
  51. /**
  52. * Gets the number of Routes in this collection.
  53. *
  54. * @return int The number of routes
  55. */
  56. public function count()
  57. {
  58. return \count($this->routes);
  59. }
  60. public function add(string $name, Route $route)
  61. {
  62. unset($this->routes[$name]);
  63. $this->routes[$name] = $route;
  64. }
  65. /**
  66. * Returns all routes in this collection.
  67. *
  68. * @return Route[] An array of routes
  69. */
  70. public function all()
  71. {
  72. return $this->routes;
  73. }
  74. /**
  75. * Gets a route by name.
  76. *
  77. * @return Route|null A Route instance or null when not found
  78. */
  79. public function get(string $name)
  80. {
  81. return isset($this->routes[$name]) ? $this->routes[$name] : null;
  82. }
  83. /**
  84. * Removes a route or an array of routes by name from the collection.
  85. *
  86. * @param string|string[] $name The route name or an array of route names
  87. */
  88. public function remove($name)
  89. {
  90. foreach ((array) $name as $n) {
  91. unset($this->routes[$n]);
  92. }
  93. }
  94. /**
  95. * Adds a route collection at the end of the current set by appending all
  96. * routes of the added collection.
  97. */
  98. public function addCollection(self $collection)
  99. {
  100. // we need to remove all routes with the same names first because just replacing them
  101. // would not place the new route at the end of the merged array
  102. foreach ($collection->all() as $name => $route) {
  103. unset($this->routes[$name]);
  104. $this->routes[$name] = $route;
  105. }
  106. foreach ($collection->getResources() as $resource) {
  107. $this->addResource($resource);
  108. }
  109. }
  110. /**
  111. * Adds a prefix to the path of all child routes.
  112. */
  113. public function addPrefix(string $prefix, array $defaults = [], array $requirements = [])
  114. {
  115. $prefix = trim(trim($prefix), '/');
  116. if ('' === $prefix) {
  117. return;
  118. }
  119. foreach ($this->routes as $route) {
  120. $route->setPath('/'.$prefix.$route->getPath());
  121. $route->addDefaults($defaults);
  122. $route->addRequirements($requirements);
  123. }
  124. }
  125. /**
  126. * Adds a prefix to the name of all the routes within in the collection.
  127. */
  128. public function addNamePrefix(string $prefix)
  129. {
  130. $prefixedRoutes = [];
  131. foreach ($this->routes as $name => $route) {
  132. $prefixedRoutes[$prefix.$name] = $route;
  133. if (null !== $name = $route->getDefault('_canonical_route')) {
  134. $route->setDefault('_canonical_route', $prefix.$name);
  135. }
  136. }
  137. $this->routes = $prefixedRoutes;
  138. }
  139. /**
  140. * Sets the host pattern on all routes.
  141. */
  142. public function setHost(?string $pattern, array $defaults = [], array $requirements = [])
  143. {
  144. foreach ($this->routes as $route) {
  145. $route->setHost($pattern);
  146. $route->addDefaults($defaults);
  147. $route->addRequirements($requirements);
  148. }
  149. }
  150. /**
  151. * Sets a condition on all routes.
  152. *
  153. * Existing conditions will be overridden.
  154. */
  155. public function setCondition(?string $condition)
  156. {
  157. foreach ($this->routes as $route) {
  158. $route->setCondition($condition);
  159. }
  160. }
  161. /**
  162. * Adds defaults to all routes.
  163. *
  164. * An existing default value under the same name in a route will be overridden.
  165. */
  166. public function addDefaults(array $defaults)
  167. {
  168. if ($defaults) {
  169. foreach ($this->routes as $route) {
  170. $route->addDefaults($defaults);
  171. }
  172. }
  173. }
  174. /**
  175. * Adds requirements to all routes.
  176. *
  177. * An existing requirement under the same name in a route will be overridden.
  178. */
  179. public function addRequirements(array $requirements)
  180. {
  181. if ($requirements) {
  182. foreach ($this->routes as $route) {
  183. $route->addRequirements($requirements);
  184. }
  185. }
  186. }
  187. /**
  188. * Adds options to all routes.
  189. *
  190. * An existing option value under the same name in a route will be overridden.
  191. */
  192. public function addOptions(array $options)
  193. {
  194. if ($options) {
  195. foreach ($this->routes as $route) {
  196. $route->addOptions($options);
  197. }
  198. }
  199. }
  200. /**
  201. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  202. *
  203. * @param string|string[] $schemes The scheme or an array of schemes
  204. */
  205. public function setSchemes($schemes)
  206. {
  207. foreach ($this->routes as $route) {
  208. $route->setSchemes($schemes);
  209. }
  210. }
  211. /**
  212. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  213. *
  214. * @param string|string[] $methods The method or an array of methods
  215. */
  216. public function setMethods($methods)
  217. {
  218. foreach ($this->routes as $route) {
  219. $route->setMethods($methods);
  220. }
  221. }
  222. /**
  223. * Returns an array of resources loaded to build this collection.
  224. *
  225. * @return ResourceInterface[] An array of resources
  226. */
  227. public function getResources()
  228. {
  229. return array_values($this->resources);
  230. }
  231. /**
  232. * Adds a resource for this collection. If the resource already exists
  233. * it is not added.
  234. */
  235. public function addResource(ResourceInterface $resource)
  236. {
  237. $key = (string) $resource;
  238. if (!isset($this->resources[$key])) {
  239. $this->resources[$key] = $resource;
  240. }
  241. }
  242. }