菜谱项目

RouteCollection.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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|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(RouteCollection $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. $this->resources = array_merge($this->resources, $collection->getResources());
  115. }
  116. /**
  117. * Adds a prefix to the path of all child routes.
  118. *
  119. * @param string $prefix An optional prefix to add before each pattern of the route collection
  120. * @param array $defaults An array of default values
  121. * @param array $requirements An array of requirements
  122. */
  123. public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
  124. {
  125. $prefix = trim(trim($prefix), '/');
  126. if ('' === $prefix) {
  127. return;
  128. }
  129. foreach ($this->routes as $route) {
  130. $route->setPath('/'.$prefix.$route->getPath());
  131. $route->addDefaults($defaults);
  132. $route->addRequirements($requirements);
  133. }
  134. }
  135. /**
  136. * Sets the host pattern on all routes.
  137. *
  138. * @param string $pattern The pattern
  139. * @param array $defaults An array of default values
  140. * @param array $requirements An array of requirements
  141. */
  142. public function setHost($pattern, array $defaults = array(), array $requirements = array())
  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. * @param string $condition The condition
  156. */
  157. public function setCondition($condition)
  158. {
  159. foreach ($this->routes as $route) {
  160. $route->setCondition($condition);
  161. }
  162. }
  163. /**
  164. * Adds defaults to all routes.
  165. *
  166. * An existing default value under the same name in a route will be overridden.
  167. *
  168. * @param array $defaults An array of default values
  169. */
  170. public function addDefaults(array $defaults)
  171. {
  172. if ($defaults) {
  173. foreach ($this->routes as $route) {
  174. $route->addDefaults($defaults);
  175. }
  176. }
  177. }
  178. /**
  179. * Adds requirements to all routes.
  180. *
  181. * An existing requirement under the same name in a route will be overridden.
  182. *
  183. * @param array $requirements An array of requirements
  184. */
  185. public function addRequirements(array $requirements)
  186. {
  187. if ($requirements) {
  188. foreach ($this->routes as $route) {
  189. $route->addRequirements($requirements);
  190. }
  191. }
  192. }
  193. /**
  194. * Adds options to all routes.
  195. *
  196. * An existing option value under the same name in a route will be overridden.
  197. *
  198. * @param array $options An array of options
  199. */
  200. public function addOptions(array $options)
  201. {
  202. if ($options) {
  203. foreach ($this->routes as $route) {
  204. $route->addOptions($options);
  205. }
  206. }
  207. }
  208. /**
  209. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  210. *
  211. * @param string|string[] $schemes The scheme or an array of schemes
  212. */
  213. public function setSchemes($schemes)
  214. {
  215. foreach ($this->routes as $route) {
  216. $route->setSchemes($schemes);
  217. }
  218. }
  219. /**
  220. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  221. *
  222. * @param string|string[] $methods The method or an array of methods
  223. */
  224. public function setMethods($methods)
  225. {
  226. foreach ($this->routes as $route) {
  227. $route->setMethods($methods);
  228. }
  229. }
  230. /**
  231. * Returns an array of resources loaded to build this collection.
  232. *
  233. * @return ResourceInterface[] An array of resources
  234. */
  235. public function getResources()
  236. {
  237. return array_unique($this->resources);
  238. }
  239. /**
  240. * Adds a resource for this collection.
  241. */
  242. public function addResource(ResourceInterface $resource)
  243. {
  244. $this->resources[] = $resource;
  245. }
  246. }