Brak opisu

RouteCollection.php 7.7KB

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