This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Routing/RouteCollection.php

272 lines
6.8 KiB
PHP
Raw Normal View History

2010-02-17 13:53:31 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-02-17 13:53:31 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-02-17 13:53:31 +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
*/
namespace Symfony\Component\Routing;
use Symfony\Component\Config\Resource\ResourceInterface;
2010-02-17 13:53:31 +00:00
/**
* A RouteCollection represents a set of Route instances.
2010-02-17 13:53:31 +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.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
2011-06-14 14:35:32 +01:00
*
* @api
2010-02-17 13:53:31 +00:00
*/
class RouteCollection implements \IteratorAggregate, \Countable
2010-02-17 13:53:31 +00:00
{
/**
* @var Route[]
*/
private $routes = array();
/**
* @var array
*/
private $resources = array();
public function __clone()
{
foreach ($this->routes as $name => $route) {
$this->routes[$name] = clone $route;
}
}
/**
* Gets the current RouteCollection as an Iterator that includes all routes.
2012-12-11 10:49:22 +00:00
*
* It implements \IteratorAggregate.
*
* @see all()
*
* @return \ArrayIterator An \ArrayIterator object for iterating over routes
*/
2011-04-25 16:39:10 +01:00
public function getIterator()
{
return new \ArrayIterator($this->routes);
2010-02-17 13:53:31 +00:00
}
/**
* Gets the number of Routes in this collection.
*
* @return int The number of routes
*/
public function count()
{
return count($this->routes);
}
/**
* Adds a route.
*
* @param string $name The route name
* @param Route $route A Route instance
*
2011-06-14 14:35:32 +01:00
* @api
*/
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
public function add($name, Route $route)
{
unset($this->routes[$name]);
$this->routes[$name] = $route;
}
2010-02-17 13:53:31 +00:00
/**
* Returns all routes in this collection.
*
* @return Route[] An array of routes
*/
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
public function all()
2010-02-17 13:53:31 +00:00
{
return $this->routes;
2010-02-17 13:53:31 +00:00
}
/**
* Gets a route by name.
*
* @param string $name The route name
*
* @return Route|null A Route instance or null when not found
*/
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
public function get($name)
{
return isset($this->routes[$name]) ? $this->routes[$name] : null;
}
2010-02-17 13:53:31 +00:00
/**
2014-12-21 17:00:50 +00:00
* Removes a route or an array of routes by name from the collection.
*
* @param string|array $name The route name or an array of route names
*/
public function remove($name)
{
foreach ((array) $name as $n) {
unset($this->routes[$n]);
}
}
/**
* Adds a route collection at the end of the current set by appending all
* routes of the added collection.
*
2014-11-30 13:33:44 +00:00
* @param RouteCollection $collection A RouteCollection instance
2011-06-14 14:35:32 +01:00
*
* @api
*/
public function addCollection(RouteCollection $collection)
2010-02-17 13:53:31 +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
}
/**
* Adds a prefix to the path of all child routes.
*
* @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
*/
public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
2010-02-17 13:53:31 +00:00
{
$prefix = trim(trim($prefix), '/');
if ('' === $prefix) {
return;
}
foreach ($this->routes as $route) {
$route->setPath('/'.$prefix.$route->getPath());
$route->addDefaults($defaults);
$route->addRequirements($requirements);
}
2010-02-17 13:53:31 +00:00
}
2012-11-05 19:53:57 +00:00
/**
* Sets the host pattern on all routes.
2012-11-05 19:53:57 +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
*/
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) {
$route->setHost($pattern);
$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
}
}
/**
* 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);
}
}
/**
* Returns an array of resources loaded to build this collection.
*
* @return ResourceInterface[] An array of resources
*/
public function getResources()
{
return array_unique($this->resources);
}
2010-02-17 13:53:31 +00: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
}