2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Routing;
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:53:31 +00:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* A RouteCollection represents a set of Route instances.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2012-11-26 17:28:37 +00:00
|
|
|
* When adding a route at the end of the collection, an existing route
|
|
|
|
* with the same name is removed first. So there can only be one route
|
|
|
|
* with a given name.
|
2011-09-30 08:20:51 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2012-04-01 11:32:28 +01:00
|
|
|
* @author Tobias Schultze <http://tobion.de>
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
2012-06-11 01:37:14 +01:00
|
|
|
class RouteCollection implements \IteratorAggregate, \Countable
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2012-11-12 15:14:50 +00:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* @var Route[]
|
2012-11-12 15:14:50 +00:00
|
|
|
*/
|
|
|
|
private $routes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $resources = array();
|
|
|
|
|
2011-10-24 10:39:31 +01:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
foreach ($this->routes as $name => $route) {
|
|
|
|
$this->routes[$name] = clone $route;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-30 08:20:51 +01:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Gets the current RouteCollection as an Iterator that includes all routes.
|
2012-12-11 10:49:22 +00:00
|
|
|
*
|
2012-11-26 17:28:37 +00:00
|
|
|
* It implements \IteratorAggregate.
|
|
|
|
*
|
|
|
|
* @see all()
|
2011-09-30 08:20:51 +01:00
|
|
|
*
|
2012-11-26 17:28:37 +00:00
|
|
|
* @return \ArrayIterator An \ArrayIterator object for iterating over routes
|
2011-09-30 08:20:51 +01:00
|
|
|
*/
|
2011-04-25 16:39:10 +01:00
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new \ArrayIterator($this->routes);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-06-11 01:37:14 +01:00
|
|
|
/**
|
|
|
|
* Gets the number of Routes in this collection.
|
|
|
|
*
|
2012-11-26 17:28:37 +00:00
|
|
|
* @return int The number of routes
|
2012-06-11 01:37:14 +01:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2012-11-26 17:28:37 +00:00
|
|
|
return count($this->routes);
|
2012-06-11 01:37:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds a route.
|
|
|
|
*
|
|
|
|
* @param string $name The route name
|
|
|
|
* @param Route $route A Route instance
|
|
|
|
*
|
2011-06-14 14:35:32 +01:00
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-11-23 08:42:19 +00:00
|
|
|
public function add($name, Route $route)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2012-11-26 17:28:37 +00:00
|
|
|
unset($this->routes[$name]);
|
2011-09-30 08:20:51 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->routes[$name] = $route;
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Returns all routes in this collection.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-11-12 15:14:50 +00:00
|
|
|
* @return Route[] An array of routes
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-11-23 08:42:19 +00:00
|
|
|
public function all()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2012-11-26 17:28:37 +00:00
|
|
|
return $this->routes;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Gets a route by name.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-04-01 11:32:28 +01:00
|
|
|
* @param string $name The route name
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-04-01 11:32:28 +01:00
|
|
|
* @return Route|null A Route instance or null when not found
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-11-23 08:42:19 +00:00
|
|
|
public function get($name)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2012-11-26 17:28:37 +00:00
|
|
|
return isset($this->routes[$name]) ? $this->routes[$name] : null;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2011-09-30 08:20:51 +01:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Removes a route or an array of routes by name from the collection.
|
2012-11-26 17:28:37 +00:00
|
|
|
*
|
2012-04-01 11:32:28 +01:00
|
|
|
* @param string|array $name The route name or an array of route names
|
2011-09-30 08:20:51 +01:00
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
2012-04-01 11:32:28 +01:00
|
|
|
foreach ((array) $name as $n) {
|
2012-11-26 17:28:37 +00:00
|
|
|
unset($this->routes[$n]);
|
2011-09-30 08:20:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Adds a route collection at the end of the current set by appending all
|
|
|
|
* routes of the added collection.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param RouteCollection $collection A RouteCollection instance
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2012-11-15 15:00:09 +00:00
|
|
|
public function addCollection(RouteCollection $collection)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2012-11-26 17:28:37 +00:00
|
|
|
// we need to remove all routes with the same names first because just replacing them
|
|
|
|
// would not place the new route at the end of the merged array
|
|
|
|
foreach ($collection->all() as $name => $route) {
|
|
|
|
unset($this->routes[$name]);
|
|
|
|
$this->routes[$name] = $route;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->resources = array_merge($this->resources, $collection->getResources());
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-11-15 15:00:09 +00:00
|
|
|
* Adds a prefix to the path of all child routes.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-10-23 10:56:23 +01:00
|
|
|
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
|
|
|
* @param array $defaults An array of default values
|
|
|
|
* @param array $requirements An array of requirements
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2012-11-15 15:00:09 +00:00
|
|
|
public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2012-10-06 00:21:17 +01:00
|
|
|
$prefix = trim(trim($prefix), '/');
|
2011-04-26 13:26:29 +01:00
|
|
|
|
2012-11-15 15:00:09 +00:00
|
|
|
if ('' === $prefix) {
|
2012-04-01 11:32:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->routes as $route) {
|
2013-04-02 10:39:57 +01:00
|
|
|
$route->setPath('/'.$prefix.$route->getPath());
|
2012-11-26 17:28:37 +00:00
|
|
|
$route->addDefaults($defaults);
|
|
|
|
$route->addRequirements($requirements);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 19:53:57 +00:00
|
|
|
/**
|
2013-01-21 16:57:28 +00:00
|
|
|
* Sets the host pattern on all routes.
|
2012-11-05 19:53:57 +00:00
|
|
|
*
|
2012-11-15 15:00:09 +00:00
|
|
|
* @param string $pattern The pattern
|
|
|
|
* @param array $defaults An array of default values
|
|
|
|
* @param array $requirements An array of requirements
|
2012-11-05 19:53:57 +00:00
|
|
|
*/
|
2013-01-21 16:57:28 +00:00
|
|
|
public function setHost($pattern, array $defaults = array(), array $requirements = array())
|
2012-11-05 19:53:57 +00:00
|
|
|
{
|
2012-11-19 09:05:27 +00:00
|
|
|
foreach ($this->routes as $route) {
|
2013-01-21 16:57:28 +00:00
|
|
|
$route->setHost($pattern);
|
2012-11-15 15:00:09 +00:00
|
|
|
$route->addDefaults($defaults);
|
|
|
|
$route->addRequirements($requirements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds defaults to all routes.
|
|
|
|
*
|
|
|
|
* An existing default value under the same name in a route will be overridden.
|
|
|
|
*
|
|
|
|
* @param array $defaults An array of default values
|
|
|
|
*/
|
|
|
|
public function addDefaults(array $defaults)
|
|
|
|
{
|
|
|
|
if ($defaults) {
|
|
|
|
foreach ($this->routes as $route) {
|
|
|
|
$route->addDefaults($defaults);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds requirements to all routes.
|
|
|
|
*
|
|
|
|
* An existing requirement under the same name in a route will be overridden.
|
|
|
|
*
|
|
|
|
* @param array $requirements An array of requirements
|
|
|
|
*/
|
|
|
|
public function addRequirements(array $requirements)
|
|
|
|
{
|
|
|
|
if ($requirements) {
|
|
|
|
foreach ($this->routes as $route) {
|
|
|
|
$route->addRequirements($requirements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds options to all routes.
|
|
|
|
*
|
|
|
|
* An existing option value under the same name in a route will be overridden.
|
|
|
|
*
|
|
|
|
* @param array $options An array of options
|
|
|
|
*/
|
|
|
|
public function addOptions(array $options)
|
|
|
|
{
|
|
|
|
if ($options) {
|
|
|
|
foreach ($this->routes as $route) {
|
|
|
|
$route->addOptions($options);
|
|
|
|
}
|
2012-11-05 19:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 11:26:47 +00:00
|
|
|
/**
|
|
|
|
* Sets the schemes (e.g. 'https') all child routes are restricted to.
|
|
|
|
*
|
|
|
|
* @param string|array $schemes The scheme or an array of schemes
|
|
|
|
*/
|
|
|
|
public function setSchemes($schemes)
|
|
|
|
{
|
|
|
|
foreach ($this->routes as $route) {
|
|
|
|
$route->setSchemes($schemes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
|
|
|
|
*
|
|
|
|
* @param string|array $methods The method or an array of methods
|
|
|
|
*/
|
|
|
|
public function setMethods($methods)
|
|
|
|
{
|
|
|
|
foreach ($this->routes as $route) {
|
|
|
|
$route->setMethods($methods);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns an array of resources loaded to build this collection.
|
|
|
|
*
|
|
|
|
* @return ResourceInterface[] An array of resources
|
|
|
|
*/
|
|
|
|
public function getResources()
|
|
|
|
{
|
2012-11-26 17:28:37 +00:00
|
|
|
return array_unique($this->resources);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds a resource for this collection.
|
|
|
|
*
|
|
|
|
* @param ResourceInterface $resource A resource instance
|
|
|
|
*/
|
|
|
|
public function addResource(ResourceInterface $resource)
|
|
|
|
{
|
|
|
|
$this->resources[] = $resource;
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|