4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing;
14 use Symfony\Component\Config\Resource\ResourceInterface;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Routing\RouteCollection;
18 class ChainRouteCollection extends RouteCollection
21 * @var RouteCollection[]
23 private $routeCollections = array();
26 * @var RouteCollection
28 private $routeCollection;
30 public function __clone()
32 foreach ($this->routeCollections as $routeCollection) {
33 $this->routeCollections[] = clone $routeCollection;
38 * Gets the current RouteCollection as an Iterator that includes all routes.
40 * It implements \IteratorAggregate.
44 * @return \ArrayIterator An \ArrayIterator object for iterating over routes
46 public function getIterator()
48 return new \ArrayIterator($this->all());
52 * Gets the number of Routes in this collection.
54 * @return int The number of routes
56 public function count()
59 foreach ($this->routeCollections as $routeCollection) {
60 $count += $routeCollection->count();
69 * @param string $name The route name
70 * @param Route $route A Route instance
72 public function add($name, Route $route)
74 $this->createInternalCollection();
75 $this->routeCollection->add($name, $route);
79 * Returns all routes in this collection.
81 * @return Route[] An array of routes
85 $routeCollectionAll = new RouteCollection();
86 foreach ($this->routeCollections as $routeCollection) {
87 $routeCollectionAll->addCollection($routeCollection);
90 return $routeCollectionAll->all();
94 * Gets a route by name.
96 * @param string $name The route name
98 * @return Route|null A Route instance or null when not found
100 public function get($name)
102 foreach ($this->routeCollections as $routeCollection) {
103 $route = $routeCollection->get($name);
104 if (null !== $route) {
113 * Removes a route or an array of routes by name from the collection.
115 * @param string|array $name The route name or an array of route names
117 public function remove($name)
119 foreach ($this->routeCollections as $routeCollection) {
120 $route = $routeCollection->get($name);
121 if (null !== $route) {
122 $routeCollection->remove($name);
128 * Adds a route collection at the end of the current set by appending all
129 * routes of the added collection.
131 * @param RouteCollection $collection A RouteCollection instance
133 public function addCollection(RouteCollection $collection)
135 $this->routeCollections[] = $collection;
139 * Adds a prefix to the path of all child routes.
141 * @param string $prefix An optional prefix to add before each pattern of the route collection
142 * @param array $defaults An array of default values
143 * @param array $requirements An array of requirements
145 public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
147 $this->createInternalCollection();
148 foreach ($this->routeCollections as $routeCollection) {
149 $routeCollection->addPrefix($prefix, $defaults, $requirements);
154 * Sets the host pattern on all routes.
156 * @param string $pattern The pattern
157 * @param array $defaults An array of default values
158 * @param array $requirements An array of requirements
160 public function setHost($pattern, array $defaults = array(), array $requirements = array())
162 $this->createInternalCollection();
163 foreach ($this->routeCollections as $routeCollection) {
164 $routeCollection->setHost($pattern, $defaults, $requirements);
169 * Adds defaults to all routes.
171 * An existing default value under the same name in a route will be overridden.
173 * @param array $defaults An array of default values
175 public function addDefaults(array $defaults)
177 $this->createInternalCollection();
178 foreach ($this->routeCollections as $routeCollection) {
179 $routeCollection->addDefaults($defaults);
184 * Adds requirements to all routes.
186 * An existing requirement under the same name in a route will be overridden.
188 * @param array $requirements An array of requirements
190 public function addRequirements(array $requirements)
192 $this->createInternalCollection();
193 foreach ($this->routeCollections as $routeCollection) {
194 $routeCollection->addRequirements($requirements);
199 * Adds options to all routes.
201 * An existing option value under the same name in a route will be overridden.
203 * @param array $options An array of options
205 public function addOptions(array $options)
207 $this->createInternalCollection();
208 foreach ($this->routeCollections as $routeCollection) {
209 $routeCollection->addOptions($options);
214 * Sets the schemes (e.g. 'https') all child routes are restricted to.
216 * @param string|array $schemes The scheme or an array of schemes
218 public function setSchemes($schemes)
220 $this->createInternalCollection();
221 foreach ($this->routeCollections as $routeCollection) {
222 $routeCollection->setSchemes($schemes);
227 * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
229 * @param string|array $methods The method or an array of methods
231 public function setMethods($methods)
233 $this->createInternalCollection();
234 foreach ($this->routeCollections as $routeCollection) {
235 $routeCollection->setMethods($methods);
240 * Returns an array of resources loaded to build this collection.
242 * @return ResourceInterface[] An array of resources
244 public function getResources()
246 $resources = array();
247 foreach ($this->routeCollections as $routeCollection) {
248 $resources = array_merge($resources, $routeCollection->getResources());
251 return array_unique($resources);
255 * Adds a resource for this collection.
257 * @param ResourceInterface $resource A resource instance
259 public function addResource(ResourceInterface $resource)
261 $this->createInternalCollection();
262 $this->routeCollection->addResource($resource);
265 private function createInternalCollection()
267 if (!$this->routeCollection instanceof RouteCollection) {
268 $this->routeCollection = new RouteCollection();
269 $this->routeCollections[] = $this->routeCollection;