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();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
2012-11-15 15:00:09 +00:00
|
|
|
* @deprecated since version 2.2, will be removed in 2.3
|
2012-11-12 15:14:50 +00:00
|
|
|
*/
|
|
|
|
private $prefix = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RouteCollection|null
|
2012-11-26 17:28:37 +00:00
|
|
|
* @deprecated since version 2.2, will be removed in 2.3
|
2012-11-12 15:14:50 +00:00
|
|
|
*/
|
2011-09-30 08:20:51 +01:00
|
|
|
private $parent;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Gets the parent RouteCollection.
|
|
|
|
*
|
2012-04-01 11:32:28 +01:00
|
|
|
* @return RouteCollection|null The parent RouteCollection or null when it's the root
|
2012-11-26 17:28:37 +00:00
|
|
|
*
|
|
|
|
* @deprecated since version 2.2, will be removed in 2.3
|
2011-09-30 08:20:51 +01:00
|
|
|
*/
|
|
|
|
public function getParent()
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Gets the root RouteCollection.
|
2011-09-30 08:20:51 +01:00
|
|
|
*
|
2012-04-01 11:32:28 +01:00
|
|
|
* @return RouteCollection The root RouteCollection
|
2012-11-26 17:28:37 +00:00
|
|
|
*
|
|
|
|
* @deprecated since version 2.2, will be removed in 2.3
|
2011-09-30 08:20:51 +01:00
|
|
|
*/
|
2012-04-01 11:32:28 +01:00
|
|
|
public function getRoot()
|
2011-09-30 08:20:51 +01:00
|
|
|
{
|
2012-04-01 11:32:28 +01:00
|
|
|
$parent = $this;
|
|
|
|
while ($parent->getParent()) {
|
|
|
|
$parent = $parent->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parent;
|
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
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Removes a route or an array of routes by name from the collection
|
|
|
|
*
|
|
|
|
* For BC it's also removed from the root, which will not be the case in 2.3
|
|
|
|
* as the RouteCollection won't be a tree structure.
|
2011-09-30 08:20:51 +01: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-11-26 17:28:37 +00:00
|
|
|
// just for BC
|
2012-04-01 11:32:28 +01:00
|
|
|
$root = $this->getRoot();
|
2011-09-30 08:20:51 +01:00
|
|
|
|
2012-04-01 11:32:28 +01:00
|
|
|
foreach ((array) $name as $n) {
|
2012-11-26 17:28:37 +00:00
|
|
|
unset($root->routes[$n]);
|
|
|
|
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
|
|
|
*
|
2012-11-05 19:53:57 +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
|
|
|
// This is to keep BC for getParent() and getRoot(). It does not prevent
|
|
|
|
// infinite loops by recursive referencing. But we don't need that logic
|
|
|
|
// anymore as the tree logic has been deprecated and we are just widening
|
|
|
|
// the accepted range.
|
|
|
|
$collection->parent = $this;
|
2012-04-01 11:32:28 +01:00
|
|
|
|
2012-11-15 15:00:09 +00:00
|
|
|
// this is to keep BC
|
|
|
|
$numargs = func_num_args();
|
|
|
|
if ($numargs > 1) {
|
|
|
|
$collection->addPrefix($this->prefix . func_get_arg(1));
|
|
|
|
if ($numargs > 2) {
|
|
|
|
$collection->addDefaults(func_get_arg(2));
|
|
|
|
if ($numargs > 3) {
|
|
|
|
$collection->addRequirements(func_get_arg(3));
|
|
|
|
if ($numargs > 4) {
|
|
|
|
$collection->addOptions(func_get_arg(4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the sub-collection must have the prefix of the parent (current instance) prepended because it does not
|
|
|
|
// necessarily already have it applied (depending on the order RouteCollections are added to each other)
|
|
|
|
// this will be removed when the BC layer for getPrefix() is removed
|
|
|
|
$collection->addPrefix($this->prefix);
|
2012-11-12 12:06:49 +00:00
|
|
|
}
|
2012-04-14 18:13:54 +01: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;
|
|
|
|
}
|
|
|
|
|
2012-10-06 00:21:17 +01:00
|
|
|
// a prefix must start with a single slash and must not end with a slash
|
2012-11-15 15:00:09 +00:00
|
|
|
$this->prefix = '/' . $prefix . $this->prefix;
|
|
|
|
|
|
|
|
// this is to keep BC
|
|
|
|
$options = func_num_args() > 3 ? func_get_arg(3) : array();
|
2011-04-26 13:26:29 +01:00
|
|
|
|
2012-04-01 11:32:28 +01:00
|
|
|
foreach ($this->routes as $route) {
|
2012-11-15 15:00:09 +00:00
|
|
|
$route->setPattern('/' . $prefix . $route->getPattern());
|
2012-11-26 17:28:37 +00:00
|
|
|
$route->addDefaults($defaults);
|
|
|
|
$route->addRequirements($requirements);
|
|
|
|
$route->addOptions($options);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-04-01 11:32:28 +01:00
|
|
|
/**
|
|
|
|
* Returns the prefix that may contain placeholders.
|
|
|
|
*
|
|
|
|
* @return string The prefix
|
2012-11-15 15:00:09 +00:00
|
|
|
*
|
|
|
|
* @deprecated since version 2.2, will be removed in 2.3
|
2012-04-01 11:32:28 +01:00
|
|
|
*/
|
2011-04-25 16:39:10 +01:00
|
|
|
public function getPrefix()
|
|
|
|
{
|
|
|
|
return $this->prefix;
|
|
|
|
}
|
|
|
|
|
2012-11-05 19:53:57 +00:00
|
|
|
/**
|
2012-11-26 17:28:37 +00:00
|
|
|
* Sets the hostname 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
|
|
|
*/
|
2012-11-15 15:00:09 +00:00
|
|
|
public function setHostnamePattern($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) {
|
2012-11-05 19:53:57 +00:00
|
|
|
$route->setHostnamePattern($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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|