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>
28 class RouteCollection
implements \IteratorAggregate
, \Countable
33 private $routes = array();
38 private $resources = array();
40 public function __clone()
42 foreach ($this->routes
as $name => $route) {
43 $this->routes
[$name] = clone $route;
48 * Gets the current RouteCollection as an Iterator that includes all routes.
50 * It implements \IteratorAggregate.
54 * @return \ArrayIterator An \ArrayIterator object for iterating over routes
56 public function getIterator()
58 return new \
ArrayIterator($this->routes
);
62 * Gets the number of Routes in this collection.
64 * @return int The number of routes
66 public function count()
68 return count($this->routes
);
74 * @param string $name The route name
75 * @param Route $route A Route instance
79 public function add($name, Route
$route)
81 unset($this->routes
[$name]);
83 $this->routes
[$name] = $route;
87 * Returns all routes in this collection.
89 * @return Route[] An array of routes
97 * Gets a route by name.
99 * @param string $name The route name
101 * @return Route|null A Route instance or null when not found
103 public function get($name)
105 return isset($this->routes
[$name]) ? $this->routes
[$name] : null;
109 * Removes a route or an array of routes by name from the collection
111 * @param string|array $name The route name or an array of route names
113 public function remove($name)
115 foreach ((array) $name as $n) {
116 unset($this->routes
[$n]);
121 * Adds a route collection at the end of the current set by appending all
122 * routes of the added collection.
124 * @param RouteCollection $collection A RouteCollection instance
128 public function addCollection(RouteCollection
$collection)
130 // we need to remove all routes with the same names first because just replacing them
131 // would not place the new route at the end of the merged array
132 foreach ($collection->all() as $name => $route) {
133 unset($this->routes
[$name]);
134 $this->routes
[$name] = $route;
137 $this->resources
= array_merge($this->resources
, $collection->getResources());
141 * Adds a prefix to the path of all child routes.
143 * @param string $prefix An optional prefix to add before each pattern of the route collection
144 * @param array $defaults An array of default values
145 * @param array $requirements An array of requirements
149 public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
151 $prefix = trim(trim($prefix), '/');
153 if ('' === $prefix) {
157 foreach ($this->routes
as $route) {
158 $route->setPath('/'.$prefix.$route->getPath());
159 $route->addDefaults($defaults);
160 $route->addRequirements($requirements);
165 * Sets the host pattern on all routes.
167 * @param string $pattern The pattern
168 * @param array $defaults An array of default values
169 * @param array $requirements An array of requirements
171 public function setHost($pattern, array $defaults = array(), array $requirements = array())
173 foreach ($this->routes
as $route) {
174 $route->setHost($pattern);
175 $route->addDefaults($defaults);
176 $route->addRequirements($requirements);
181 * Adds defaults to all routes.
183 * An existing default value under the same name in a route will be overridden.
185 * @param array $defaults An array of default values
187 public function addDefaults(array $defaults)
190 foreach ($this->routes
as $route) {
191 $route->addDefaults($defaults);
197 * Adds requirements to all routes.
199 * An existing requirement under the same name in a route will be overridden.
201 * @param array $requirements An array of requirements
203 public function addRequirements(array $requirements)
206 foreach ($this->routes
as $route) {
207 $route->addRequirements($requirements);
213 * Adds options to all routes.
215 * An existing option value under the same name in a route will be overridden.
217 * @param array $options An array of options
219 public function addOptions(array $options)
222 foreach ($this->routes
as $route) {
223 $route->addOptions($options);
229 * Sets the schemes (e.g. 'https') all child routes are restricted to.
231 * @param string|array $schemes The scheme or an array of schemes
233 public function setSchemes($schemes)
235 foreach ($this->routes
as $route) {
236 $route->setSchemes($schemes);
241 * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
243 * @param string|array $methods The method or an array of methods
245 public function setMethods($methods)
247 foreach ($this->routes
as $route) {
248 $route->setMethods($methods);
253 * Returns an array of resources loaded to build this collection.
255 * @return ResourceInterface[] An array of resources
257 public function getResources()
259 return array_unique($this->resources
);
263 * Adds a resource for this collection.
265 * @param ResourceInterface $resource A resource instance
267 public function addResource(ResourceInterface
$resource)
269 $this->resources
[] = $resource;