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

127 lines
3.1 KiB
PHP
Raw Normal View History

2010-02-17 13:53:31 +00:00
<?php
namespace Symfony\Component\Routing;
2010-02-17 13:53:31 +00:00
use Symfony\Component\Routing\Resource\ResourceInterface;
2010-06-28 08:15:15 +01:00
2010-02-17 13:53:31 +00:00
/*
* This file is part of the Symfony framework.
2010-02-17 13:53:31 +00:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* A RouteCollection represents a set of Route instances.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2010-02-17 13:53:31 +00:00
*/
class RouteCollection
{
protected $routes;
protected $resources;
2010-02-17 13:53:31 +00:00
/**
* Constructor.
*/
public function __construct()
2010-02-17 13:53:31 +00:00
{
$this->routes = array();
$this->resources = array();
2010-02-17 13:53:31 +00: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
*/
public function addRoute($name, Route $route)
{
if (!preg_match('/^[a-z0-9A-Z_]+$/', $name)) {
throw new \InvalidArgumentException(sprintf('Name "%s" contains non valid characters for a route name.', $name));
}
2010-02-17 13:53:31 +00:00
$this->routes[$name] = $route;
}
2010-02-17 13:53:31 +00:00
/**
* Returns the array of routes.
*
* @return array An array of routes
*/
public function getRoutes()
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 $route A Route instance
*/
public function getRoute($name)
{
return isset($this->routes[$name]) ? $this->routes[$name] : null;
}
2010-02-17 13:53:31 +00: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
{
$collection->addPrefix($prefix);
foreach ($collection->getResources() as $resource) {
$this->addResource($resource);
}
$this->routes = array_merge($this->routes, $collection->getRoutes());
2010-02-17 13:53:31 +00: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
{
if (!$prefix) {
return;
}
foreach ($this->getRoutes() as $route) {
$route->setPattern($prefix.$route->getPattern());
}
2010-02-17 13:53:31 +00: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-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
}