RouteCollection.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /**
  61. * Adds a route.
  62. *
  63. * @param string $name The route name
  64. * @param Route $route A Route instance
  65. */
  66. public function add($name, Route $route)
  67. {
  68. unset($this->routes[$name]);
  69. $this->routes[$name] = $route;
  70. }
  71. /**
  72. * Returns all routes in this collection.
  73. *
  74. * @return Route[] An array of routes
  75. */
  76. public function all()
  77. {
  78. return $this->routes;
  79. }
  80. /**
  81. * Gets a route by name.
  82. *
  83. * @param string $name The route name
  84. *
  85. * @return Route|null A Route instance or null when not found
  86. */
  87. public function get($name)
  88. {
  89. return isset($this->routes[$name]) ? $this->routes[$name] : null;
  90. }
  91. /**
  92. * Removes a route or an array of routes by name from the collection.
  93. *
  94. * @param string|string[] $name The route name or an array of route names
  95. */
  96. public function remove($name)
  97. {
  98. foreach ((array) $name as $n) {
  99. unset($this->routes[$n]);
  100. }
  101. }
  102. /**
  103. * Adds a route collection at the end of the current set by appending all
  104. * routes of the added collection.
  105. */
  106. public function addCollection(self $collection)
  107. {
  108. // we need to remove all routes with the same names first because just replacing them
  109. // would not place the new route at the end of the merged array
  110. foreach ($collection->all() as $name => $route) {
  111. unset($this->routes[$name]);
  112. $this->routes[$name] = $route;
  113. }
  114. foreach ($collection->getResources() as $resource) {
  115. $this->addResource($resource);
  116. }
  117. }
  118. /**
  119. * Adds a prefix to the path of all child routes.
  120. *
  121. * @param string $prefix An optional prefix to add before each pattern of the route collection
  122. * @param array $defaults An array of default values
  123. * @param array $requirements An array of requirements
  124. */
  125. public function addPrefix($prefix, array $defaults = [], array $requirements = [])
  126. {
  127. $prefix = trim(trim($prefix), '/');
  128. if ('' === $prefix) {
  129. return;
  130. }
  131. foreach ($this->routes as $route) {
  132. $route->setPath('/'.$prefix.$route->getPath());
  133. $route->addDefaults($defaults);
  134. $route->addRequirements($requirements);
  135. }
  136. }
  137. /**
  138. * Adds a prefix to the name of all the routes within in the collection.
  139. */
  140. public function addNamePrefix(string $prefix)
  141. {
  142. $prefixedRoutes = [];
  143. foreach ($this->routes as $name => $route) {
  144. $prefixedRoutes[$prefix.$name] = $route;
  145. if (null !== $name = $route->getDefault('_canonical_route')) {
  146. $route->setDefault('_canonical_route', $prefix.$name);
  147. }
  148. }
  149. $this->routes = $prefixedRoutes;
  150. }
  151. /**
  152. * Sets the host pattern on all routes.
  153. *
  154. * @param string $pattern The pattern
  155. * @param array $defaults An array of default values
  156. * @param array $requirements An array of requirements
  157. */
  158. public function setHost($pattern, array $defaults = [], array $requirements = [])
  159. {
  160. foreach ($this->routes as $route) {
  161. $route->setHost($pattern);
  162. $route->addDefaults($defaults);
  163. $route->addRequirements($requirements);
  164. }
  165. }
  166. /**
  167. * Sets a condition on all routes.
  168. *
  169. * Existing conditions will be overridden.
  170. *
  171. * @param string $condition The condition
  172. */
  173. public function setCondition($condition)
  174. {
  175. foreach ($this->routes as $route) {
  176. $route->setCondition($condition);
  177. }
  178. }
  179. /**
  180. * Adds defaults to all routes.
  181. *
  182. * An existing default value under the same name in a route will be overridden.
  183. *
  184. * @param array $defaults An array of default values
  185. */
  186. public function addDefaults(array $defaults)
  187. {
  188. if ($defaults) {
  189. foreach ($this->routes as $route) {
  190. $route->addDefaults($defaults);
  191. }
  192. }
  193. }
  194. /**
  195. * Adds requirements to all routes.
  196. *
  197. * An existing requirement under the same name in a route will be overridden.
  198. *
  199. * @param array $requirements An array of requirements
  200. */
  201. public function addRequirements(array $requirements)
  202. {
  203. if ($requirements) {
  204. foreach ($this->routes as $route) {
  205. $route->addRequirements($requirements);
  206. }
  207. }
  208. }
  209. /**
  210. * Adds options to all routes.
  211. *
  212. * An existing option value under the same name in a route will be overridden.
  213. *
  214. * @param array $options An array of options
  215. */
  216. public function addOptions(array $options)
  217. {
  218. if ($options) {
  219. foreach ($this->routes as $route) {
  220. $route->addOptions($options);
  221. }
  222. }
  223. }
  224. /**
  225. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  226. *
  227. * @param string|string[] $schemes The scheme or an array of schemes
  228. */
  229. public function setSchemes($schemes)
  230. {
  231. foreach ($this->routes as $route) {
  232. $route->setSchemes($schemes);
  233. }
  234. }
  235. /**
  236. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  237. *
  238. * @param string|string[] $methods The method or an array of methods
  239. */
  240. public function setMethods($methods)
  241. {
  242. foreach ($this->routes as $route) {
  243. $route->setMethods($methods);
  244. }
  245. }
  246. /**
  247. * Returns an array of resources loaded to build this collection.
  248. *
  249. * @return ResourceInterface[] An array of resources
  250. */
  251. public function getResources()
  252. {
  253. return array_values($this->resources);
  254. }
  255. /**
  256. * Adds a resource for this collection. If the resource already exists
  257. * it is not added.
  258. */
  259. public function addResource(ResourceInterface $resource)
  260. {
  261. $key = (string) $resource;
  262. if (!isset($this->resources[$key])) {
  263. $this->resources[$key] = $resource;
  264. }
  265. }
  266. }