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
|
|
|
/**
|
|
|
|
* A RouteCollection represents a set of Route instances.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class RouteCollection
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $routes;
|
|
|
|
protected $resources;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->routes = array();
|
|
|
|
$this->resources = array();
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds a route.
|
|
|
|
*
|
|
|
|
* @param string $name The route name
|
|
|
|
* @param Route $route A Route instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When route name contains non valid characters
|
|
|
|
*/
|
2010-11-23 08:42:19 +00:00
|
|
|
public function add($name, Route $route)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-12-12 07:53:18 +00:00
|
|
|
if (!preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Name "%s" contains non valid characters for a route name.', $name));
|
|
|
|
}
|
2010-02-17 13:53:31 +00: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
|
|
|
/**
|
|
|
|
* Returns the array of routes.
|
|
|
|
*
|
|
|
|
* @return array An array of routes
|
|
|
|
*/
|
2010-11-23 08:42:19 +00:00
|
|
|
public function all()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->routes;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets a route by name.
|
|
|
|
*
|
|
|
|
* @param string $name The route name
|
|
|
|
*
|
|
|
|
* @return Route $route A Route instance
|
|
|
|
*/
|
2010-11-23 08:42:19 +00:00
|
|
|
public function get($name)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
return isset($this->routes[$name]) ? $this->routes[$name] : null;
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds a route collection to the current set of routes (at the end of the current set).
|
|
|
|
*
|
|
|
|
* @param RouteCollection $collection A RouteCollection instance
|
|
|
|
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
|
|
|
*/
|
|
|
|
public function addCollection(RouteCollection $collection, $prefix = '')
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$collection->addPrefix($prefix);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($collection->getResources() as $resource) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->addResource($resource);
|
|
|
|
}
|
|
|
|
|
2010-11-23 08:42:19 +00:00
|
|
|
$this->routes = array_merge($this->routes, $collection->all());
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds a prefix to all routes in the current set.
|
|
|
|
*
|
|
|
|
* @param string $prefix An optional prefix to add before each pattern of the route collection
|
|
|
|
*/
|
|
|
|
public function addPrefix($prefix)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$prefix) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-23 08:42:19 +00:00
|
|
|
foreach ($this->all() as $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$route->setPattern($prefix.$route->getPattern());
|
|
|
|
}
|
2010-02-17 13:53:31 +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()
|
|
|
|
{
|
2010-06-28 08:15:15 +01: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
|
|
|
}
|