123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Routing;
- use Symfony\Component\Config\Resource\ResourceInterface;
- /**
- * A RouteCollection represents a set of Route instances.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class RouteCollection
- {
- protected $routes;
- protected $resources;
- /**
- * Constructor.
- */
- public function __construct()
- {
- $this->routes = array();
- $this->resources = array();
- }
- /**
- * 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 add($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));
- }
- $this->routes[$name] = $route;
- }
- /**
- * Returns the array of routes.
- *
- * @return array An array of routes
- */
- public function all()
- {
- return $this->routes;
- }
- /**
- * Gets a route by name.
- *
- * @param string $name The route name
- *
- * @return Route $route A Route instance
- */
- public function get($name)
- {
- return isset($this->routes[$name]) ? $this->routes[$name] : null;
- }
- /**
- * 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 = '')
- {
- $collection->addPrefix($prefix);
- foreach ($collection->getResources() as $resource) {
- $this->addResource($resource);
- }
- $this->routes = array_merge($this->routes, $collection->all());
- }
- /**
- * 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)
- {
- if (!$prefix) {
- return;
- }
- foreach ($this->all() as $route) {
- $route->setPattern($prefix.$route->getPattern());
- }
- }
- /**
- * Returns an array of resources loaded to build this collection.
- *
- * @return ResourceInterface[] An array of resources
- */
- public function getResources()
- {
- return array_unique($this->resources);
- }
- /**
- * Adds a resource for this collection.
- *
- * @param ResourceInterface $resource A resource instance
- */
- public function addResource(ResourceInterface $resource)
- {
- $this->resources[] = $resource;
- }
- }
|