Chris@0: routeCollections as $routeCollection) { Chris@0: $this->routeCollections[] = clone $routeCollection; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current RouteCollection as an Iterator that includes all routes. Chris@0: * Chris@0: * It implements \IteratorAggregate. Chris@0: * Chris@0: * @see all() Chris@0: * Chris@0: * @return \ArrayIterator An \ArrayIterator object for iterating over routes Chris@0: */ Chris@0: public function getIterator() Chris@0: { Chris@0: return new \ArrayIterator($this->all()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the number of Routes in this collection. Chris@0: * Chris@0: * @return int The number of routes Chris@0: */ Chris@0: public function count() Chris@0: { Chris@0: $count = 0; Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $count += $routeCollection->count(); Chris@0: } Chris@0: Chris@0: return $count; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a route. Chris@0: * Chris@0: * @param string $name The route name Chris@0: * @param Route $route A Route instance Chris@0: */ Chris@0: public function add($name, Route $route) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: $this->routeCollection->add($name, $route); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns all routes in this collection. Chris@0: * Chris@0: * @return Route[] An array of routes Chris@0: */ Chris@0: public function all() Chris@0: { Chris@0: $routeCollectionAll = new RouteCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollectionAll->addCollection($routeCollection); Chris@0: } Chris@0: Chris@0: return $routeCollectionAll->all(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a route by name. Chris@0: * Chris@0: * @param string $name The route name Chris@0: * Chris@0: * @return Route|null A Route instance or null when not found Chris@0: */ Chris@0: public function get($name) Chris@0: { Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $route = $routeCollection->get($name); Chris@0: if (null !== $route) { Chris@0: return $route; Chris@0: } Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes a route or an array of routes by name from the collection. Chris@0: * Chris@0: * @param string|array $name The route name or an array of route names Chris@0: */ Chris@0: public function remove($name) Chris@0: { Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $route = $routeCollection->get($name); Chris@0: if (null !== $route) { Chris@0: $routeCollection->remove($name); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a route collection at the end of the current set by appending all Chris@0: * routes of the added collection. Chris@0: * Chris@0: * @param RouteCollection $collection A RouteCollection instance Chris@0: */ Chris@0: public function addCollection(RouteCollection $collection) Chris@0: { Chris@0: $this->routeCollections[] = $collection; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a prefix to the path of all child routes. Chris@0: * Chris@0: * @param string $prefix An optional prefix to add before each pattern of the route collection Chris@0: * @param array $defaults An array of default values Chris@0: * @param array $requirements An array of requirements Chris@0: */ Chris@0: public function addPrefix($prefix, array $defaults = array(), array $requirements = array()) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->addPrefix($prefix, $defaults, $requirements); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the host pattern on all routes. Chris@0: * Chris@0: * @param string $pattern The pattern Chris@0: * @param array $defaults An array of default values Chris@0: * @param array $requirements An array of requirements Chris@0: */ Chris@0: public function setHost($pattern, array $defaults = array(), array $requirements = array()) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->setHost($pattern, $defaults, $requirements); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds defaults to all routes. Chris@0: * Chris@0: * An existing default value under the same name in a route will be overridden. Chris@0: * Chris@0: * @param array $defaults An array of default values Chris@0: */ Chris@0: public function addDefaults(array $defaults) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->addDefaults($defaults); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds requirements to all routes. Chris@0: * Chris@0: * An existing requirement under the same name in a route will be overridden. Chris@0: * Chris@0: * @param array $requirements An array of requirements Chris@0: */ Chris@0: public function addRequirements(array $requirements) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->addRequirements($requirements); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds options to all routes. Chris@0: * Chris@0: * An existing option value under the same name in a route will be overridden. Chris@0: * Chris@0: * @param array $options An array of options Chris@0: */ Chris@0: public function addOptions(array $options) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->addOptions($options); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the schemes (e.g. 'https') all child routes are restricted to. Chris@0: * Chris@0: * @param string|array $schemes The scheme or an array of schemes Chris@0: */ Chris@0: public function setSchemes($schemes) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->setSchemes($schemes); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to. Chris@0: * Chris@0: * @param string|array $methods The method or an array of methods Chris@0: */ Chris@0: public function setMethods($methods) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $routeCollection->setMethods($methods); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of resources loaded to build this collection. Chris@0: * Chris@0: * @return ResourceInterface[] An array of resources Chris@0: */ Chris@0: public function getResources() Chris@0: { Chris@0: $resources = array(); Chris@0: foreach ($this->routeCollections as $routeCollection) { Chris@0: $resources = array_merge($resources, $routeCollection->getResources()); Chris@0: } Chris@0: Chris@0: return array_unique($resources); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a resource for this collection. Chris@0: * Chris@0: * @param ResourceInterface $resource A resource instance Chris@0: */ Chris@0: public function addResource(ResourceInterface $resource) Chris@0: { Chris@0: $this->createInternalCollection(); Chris@0: $this->routeCollection->addResource($resource); Chris@0: } Chris@0: Chris@0: private function createInternalCollection() Chris@0: { Chris@0: if (!$this->routeCollection instanceof RouteCollection) { Chris@0: $this->routeCollection = new RouteCollection(); Chris@0: $this->routeCollections[] = $this->routeCollection; Chris@0: } Chris@0: } Chris@0: }