RouteCollection.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. */
  65. public function add($name, Route $route)
  66. {
  67. unset($this->routes[$name]);
  68. $this->routes[$name] = $route;
  69. }
  70. /**
  71. * Returns all routes in this collection.
  72. *
  73. * @return Route[] An array of routes
  74. */
  75. public function all()
  76. {
  77. return $this->routes;
  78. }
  79. /**
  80. * Gets a route by name.
  81. *
  82. * @param string $name The route name
  83. *
  84. * @return Route|null A Route instance or null when not found
  85. */
  86. public function get($name)
  87. {
  88. return isset($this->routes[$name]) ? $this->routes[$name] : null;
  89. }
  90. /**
  91. * Removes a route or an array of routes by name from the collection.
  92. *
  93. * @param string|string[] $name The route name or an array of route names
  94. */
  95. public function remove($name)
  96. {
  97. foreach ((array) $name as $n) {
  98. unset($this->routes[$n]);
  99. }
  100. }
  101. /**
  102. * Adds a route collection at the end of the current set by appending all
  103. * routes of the added collection.
  104. */
  105. public function addCollection(self $collection)
  106. {
  107. // we need to remove all routes with the same names first because just replacing them
  108. // would not place the new route at the end of the merged array
  109. foreach ($collection->all() as $name => $route) {
  110. unset($this->routes[$name]);
  111. $this->routes[$name] = $route;
  112. }
  113. foreach ($collection->getResources() as $resource) {
  114. $this->addResource($resource);
  115. }
  116. }
  117. /**
  118. * Adds a prefix to the path of all child routes.
  119. *
  120. * @param string $prefix An optional prefix to add before each pattern of the route collection
  121. * @param array $defaults An array of default values
  122. * @param array $requirements An array of requirements
  123. */
  124. public function addPrefix($prefix, array $defaults = [], array $requirements = [])
  125. {
  126. if (null === $prefix) {
  127. @trigger_error(sprintf('Passing null as $prefix to %s is deprecated in Symfony 4.4 and will trigger a TypeError in 5.0.', __METHOD__), E_USER_DEPRECATED);
  128. }
  129. $prefix = trim(trim($prefix), '/');
  130. if ('' === $prefix) {
  131. return;
  132. }
  133. foreach ($this->routes as $route) {
  134. $route->setPath('/'.$prefix.$route->getPath());
  135. $route->addDefaults($defaults);
  136. $route->addRequirements($requirements);
  137. }
  138. }
  139. /**
  140. * Adds a prefix to the name of all the routes within in the collection.
  141. */
  142. public function addNamePrefix(string $prefix)
  143. {
  144. $prefixedRoutes = [];
  145. foreach ($this->routes as $name => $route) {
  146. $prefixedRoutes[$prefix.$name] = $route;
  147. if (null !== $name = $route->getDefault('_canonical_route')) {
  148. $route->setDefault('_canonical_route', $prefix.$name);
  149. }
  150. }
  151. $this->routes = $prefixedRoutes;
  152. }
  153. /**
  154. * Sets the host pattern on all routes.
  155. *
  156. * @param string $pattern The pattern
  157. * @param array $defaults An array of default values
  158. * @param array $requirements An array of requirements
  159. */
  160. public function setHost($pattern, array $defaults = [], array $requirements = [])
  161. {
  162. foreach ($this->routes as $route) {
  163. $route->setHost($pattern);
  164. $route->addDefaults($defaults);
  165. $route->addRequirements($requirements);
  166. }
  167. }
  168. /**
  169. * Sets a condition on all routes.
  170. *
  171. * Existing conditions will be overridden.
  172. *
  173. * @param string $condition The condition
  174. */
  175. public function setCondition($condition)
  176. {
  177. foreach ($this->routes as $route) {
  178. $route->setCondition($condition);
  179. }
  180. }
  181. /**
  182. * Adds defaults to all routes.
  183. *
  184. * An existing default value under the same name in a route will be overridden.
  185. *
  186. * @param array $defaults An array of default values
  187. */
  188. public function addDefaults(array $defaults)
  189. {
  190. if ($defaults) {
  191. foreach ($this->routes as $route) {
  192. $route->addDefaults($defaults);
  193. }
  194. }
  195. }
  196. /**
  197. * Adds requirements to all routes.
  198. *
  199. * An existing requirement under the same name in a route will be overridden.
  200. *
  201. * @param array $requirements An array of requirements
  202. */
  203. public function addRequirements(array $requirements)
  204. {
  205. if ($requirements) {
  206. foreach ($this->routes as $route) {
  207. $route->addRequirements($requirements);
  208. }
  209. }
  210. }
  211. /**
  212. * Adds options to all routes.
  213. *
  214. * An existing option value under the same name in a route will be overridden.
  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. }