RouteCollection.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = array();
  28. /**
  29. * @var array
  30. */
  31. private $resources = array();
  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|array $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. * @param RouteCollection $collection A RouteCollection instance
  107. */
  108. public function addCollection(RouteCollection $collection)
  109. {
  110. // we need to remove all routes with the same names first because just replacing them
  111. // would not place the new route at the end of the merged array
  112. foreach ($collection->all() as $name => $route) {
  113. unset($this->routes[$name]);
  114. $this->routes[$name] = $route;
  115. }
  116. $this->resources = array_merge($this->resources, $collection->getResources());
  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(), array $requirements = array())
  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. * Sets the host pattern on all routes.
  139. *
  140. * @param string $pattern The pattern
  141. * @param array $defaults An array of default values
  142. * @param array $requirements An array of requirements
  143. */
  144. public function setHost($pattern, array $defaults = array(), array $requirements = array())
  145. {
  146. foreach ($this->routes as $route) {
  147. $route->setHost($pattern);
  148. $route->addDefaults($defaults);
  149. $route->addRequirements($requirements);
  150. }
  151. }
  152. /**
  153. * Sets a condition on all routes.
  154. *
  155. * Existing conditions will be overridden.
  156. *
  157. * @param string $condition The condition
  158. */
  159. public function setCondition($condition)
  160. {
  161. foreach ($this->routes as $route) {
  162. $route->setCondition($condition);
  163. }
  164. }
  165. /**
  166. * Adds defaults to all routes.
  167. *
  168. * An existing default value under the same name in a route will be overridden.
  169. *
  170. * @param array $defaults An array of default values
  171. */
  172. public function addDefaults(array $defaults)
  173. {
  174. if ($defaults) {
  175. foreach ($this->routes as $route) {
  176. $route->addDefaults($defaults);
  177. }
  178. }
  179. }
  180. /**
  181. * Adds requirements to all routes.
  182. *
  183. * An existing requirement under the same name in a route will be overridden.
  184. *
  185. * @param array $requirements An array of requirements
  186. */
  187. public function addRequirements(array $requirements)
  188. {
  189. if ($requirements) {
  190. foreach ($this->routes as $route) {
  191. $route->addRequirements($requirements);
  192. }
  193. }
  194. }
  195. /**
  196. * Adds options to all routes.
  197. *
  198. * An existing option value under the same name in a route will be overridden.
  199. *
  200. * @param array $options An array of options
  201. */
  202. public function addOptions(array $options)
  203. {
  204. if ($options) {
  205. foreach ($this->routes as $route) {
  206. $route->addOptions($options);
  207. }
  208. }
  209. }
  210. /**
  211. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  212. *
  213. * @param string|array $schemes The scheme or an array of schemes
  214. */
  215. public function setSchemes($schemes)
  216. {
  217. foreach ($this->routes as $route) {
  218. $route->setSchemes($schemes);
  219. }
  220. }
  221. /**
  222. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  223. *
  224. * @param string|array $methods The method or an array of methods
  225. */
  226. public function setMethods($methods)
  227. {
  228. foreach ($this->routes as $route) {
  229. $route->setMethods($methods);
  230. }
  231. }
  232. /**
  233. * Returns an array of resources loaded to build this collection.
  234. *
  235. * @return ResourceInterface[] An array of resources
  236. */
  237. public function getResources()
  238. {
  239. return array_unique($this->resources);
  240. }
  241. /**
  242. * Adds a resource for this collection.
  243. *
  244. * @param ResourceInterface $resource A resource instance
  245. */
  246. public function addResource(ResourceInterface $resource)
  247. {
  248. $this->resources[] = $resource;
  249. }
  250. }