4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing;
14 use Symfony\Component\Config\Resource\ResourceInterface;
17 * A RouteCollection represents a set of Route instances.
19 * When adding a route at the end of the collection, an existing route
20 * with the same name is removed first. So there can only be one route
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Tobias Schultze <http://tobion.de>
26 class RouteCollection implements \IteratorAggregate, \Countable
31 private $routes = array();
36 private $resources = array();
38 public function __clone()
40 foreach ($this->routes as $name => $route) {
41 $this->routes[$name] = clone $route;
46 * Gets the current RouteCollection as an Iterator that includes all routes.
48 * It implements \IteratorAggregate.
52 * @return \ArrayIterator|Route[] An \ArrayIterator object for iterating over routes
54 public function getIterator()
56 return new \ArrayIterator($this->routes);
60 * Gets the number of Routes in this collection.
62 * @return int The number of routes
64 public function count()
66 return \count($this->routes);
72 * @param string $name The route name
73 * @param Route $route A Route instance
75 public function add($name, Route $route)
77 unset($this->routes[$name]);
79 $this->routes[$name] = $route;
83 * Returns all routes in this collection.
85 * @return Route[] An array of routes
93 * Gets a route by name.
95 * @param string $name The route name
97 * @return Route|null A Route instance or null when not found
99 public function get($name)
101 return isset($this->routes[$name]) ? $this->routes[$name] : null;
105 * Removes a route or an array of routes by name from the collection.
107 * @param string|string[] $name The route name or an array of route names
109 public function remove($name)
111 foreach ((array) $name as $n) {
112 unset($this->routes[$n]);
117 * Adds a route collection at the end of the current set by appending all
118 * routes of the added collection.
120 public function addCollection(self $collection)
122 // we need to remove all routes with the same names first because just replacing them
123 // would not place the new route at the end of the merged array
124 foreach ($collection->all() as $name => $route) {
125 unset($this->routes[$name]);
126 $this->routes[$name] = $route;
129 foreach ($collection->getResources() as $resource) {
130 $this->addResource($resource);
135 * Adds a prefix to the path of all child routes.
137 * @param string $prefix An optional prefix to add before each pattern of the route collection
138 * @param array $defaults An array of default values
139 * @param array $requirements An array of requirements
141 public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
143 $prefix = trim(trim($prefix), '/');
145 if ('' === $prefix) {
149 foreach ($this->routes as $route) {
150 $route->setPath('/'.$prefix.$route->getPath());
151 $route->addDefaults($defaults);
152 $route->addRequirements($requirements);
157 * Sets the host pattern on all routes.
159 * @param string $pattern The pattern
160 * @param array $defaults An array of default values
161 * @param array $requirements An array of requirements
163 public function setHost($pattern, array $defaults = array(), array $requirements = array())
165 foreach ($this->routes as $route) {
166 $route->setHost($pattern);
167 $route->addDefaults($defaults);
168 $route->addRequirements($requirements);
173 * Sets a condition on all routes.
175 * Existing conditions will be overridden.
177 * @param string $condition The condition
179 public function setCondition($condition)
181 foreach ($this->routes as $route) {
182 $route->setCondition($condition);
187 * Adds defaults to all routes.
189 * An existing default value under the same name in a route will be overridden.
191 * @param array $defaults An array of default values
193 public function addDefaults(array $defaults)
196 foreach ($this->routes as $route) {
197 $route->addDefaults($defaults);
203 * Adds requirements to all routes.
205 * An existing requirement under the same name in a route will be overridden.
207 * @param array $requirements An array of requirements
209 public function addRequirements(array $requirements)
212 foreach ($this->routes as $route) {
213 $route->addRequirements($requirements);
219 * Adds options to all routes.
221 * An existing option value under the same name in a route will be overridden.
223 * @param array $options An array of options
225 public function addOptions(array $options)
228 foreach ($this->routes as $route) {
229 $route->addOptions($options);
235 * Sets the schemes (e.g. 'https') all child routes are restricted to.
237 * @param string|string[] $schemes The scheme or an array of schemes
239 public function setSchemes($schemes)
241 foreach ($this->routes as $route) {
242 $route->setSchemes($schemes);
247 * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
249 * @param string|string[] $methods The method or an array of methods
251 public function setMethods($methods)
253 foreach ($this->routes as $route) {
254 $route->setMethods($methods);
259 * Returns an array of resources loaded to build this collection.
261 * @return ResourceInterface[] An array of resources
263 public function getResources()
265 return array_values($this->resources);
269 * Adds a resource for this collection. If the resource already exists
272 public function addResource(ResourceInterface $resource)
274 $key = (string) $resource;
276 if (!isset($this->resources[$key])) {
277 $this->resources[$key] = $resource;