Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. RouteCollection.php

class RouteCollection

A RouteCollection represents a set of Route instances.

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&gt;

@implements \IteratorAggregate<string, Route>

Hierarchy

  • class \Symfony\Component\Routing\RouteCollection implements \Symfony\Component\Routing\IteratorAggregate, \Symfony\Component\Routing\Countable

Expanded class hierarchy of RouteCollection

89 files declare their use of RouteCollection
AccessAwareRouter.php in core/lib/Drupal/Core/Routing/AccessAwareRouter.php
AddTrait.php in vendor/symfony/routing/Loader/Configurator/Traits/AddTrait.php
AdminRouteSubscriber.php in core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php
AttributeClassLoader.php in vendor/symfony/routing/Loader/AttributeClassLoader.php
AttributeDirectoryLoader.php in vendor/symfony/routing/Loader/AttributeDirectoryLoader.php

... See full list

File

vendor/symfony/routing/RouteCollection.php, line 30

Namespace

Symfony\Component\Routing
View source
class RouteCollection implements \IteratorAggregate, \Countable {
    
    /**
     * @var array<string, Route>
     */
    private array $routes = [];
    
    /**
     * @var array<string, Alias>
     */
    private array $aliases = [];
    
    /**
     * @var array<string, ResourceInterface>
     */
    private array $resources = [];
    
    /**
     * @var array<string, int>
     */
    private array $priorities = [];
    public function __clone() {
        foreach ($this->routes as $name => $route) {
            $this->routes[$name] = clone $route;
        }
        foreach ($this->aliases as $name => $alias) {
            $this->aliases[$name] = clone $alias;
        }
    }
    
    /**
     * Gets the current RouteCollection as an Iterator that includes all routes.
     *
     * It implements \IteratorAggregate.
     *
     * @see all()
     *
     * @return \ArrayIterator<string, Route>
     */
    public function getIterator() : \ArrayIterator {
        return new \ArrayIterator($this->all());
    }
    
    /**
     * Gets the number of Routes in this collection.
     */
    public function count() : int {
        return \count($this->routes);
    }
    public function add(string $name, Route $route, int $priority = 0) : void {
        unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
        $this->routes[$name] = $route;
        if ($priority) {
            $this->priorities[$name] = $priority;
        }
    }
    
    /**
     * Returns all routes in this collection.
     *
     * @return array<string, Route>
     */
    public function all() : array {
        if ($this->priorities) {
            $priorities = $this->priorities;
            $keysOrder = array_flip(array_keys($this->routes));
            uksort($this->routes, static fn($n1, $n2) => ($priorities[$n2] ?? 0) <=> ($priorities[$n1] ?? 0) ?: $keysOrder[$n1] <=> $keysOrder[$n2]);
        }
        return $this->routes;
    }
    
    /**
     * Gets a route by name.
     */
    public function get(string $name) : ?Route {
        $visited = [];
        while (null !== ($alias = $this->aliases[$name] ?? null)) {
            if (false !== ($searchKey = array_search($name, $visited))) {
                $visited[] = $name;
                throw new RouteCircularReferenceException($name, \array_slice($visited, $searchKey));
            }
            if ($alias->isDeprecated()) {
                $deprecation = $alias->getDeprecation($name);
                trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
            }
            $visited[] = $name;
            $name = $alias->getId();
        }
        return $this->routes[$name] ?? null;
    }
    
    /**
     * Removes a route or an array of routes by name from the collection.
     *
     * @param string|string[] $name The route name or an array of route names
     */
    public function remove(string|array $name) : void {
        $routes = [];
        foreach ((array) $name as $n) {
            if (isset($this->routes[$n])) {
                $routes[] = $n;
            }
            unset($this->routes[$n], $this->priorities[$n], $this->aliases[$n]);
        }
        if (!$routes) {
            return;
        }
        foreach ($this->aliases as $k => $alias) {
            if (\in_array($alias->getId(), $routes, true)) {
                unset($this->aliases[$k]);
            }
        }
    }
    
    /**
     * Adds a route collection at the end of the current set by appending all
     * routes of the added collection.
     */
    public function addCollection(self $collection) : void {
        // 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->priorities[$name], $this->aliases[$name]);
            $this->routes[$name] = $route;
            if (isset($collection->priorities[$name])) {
                $this->priorities[$name] = $collection->priorities[$name];
            }
        }
        foreach ($collection->getAliases() as $name => $alias) {
            unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
            $this->aliases[$name] = $alias;
        }
        foreach ($collection->getResources() as $resource) {
            $this->addResource($resource);
        }
    }
    
    /**
     * Adds a prefix to the path of all child routes.
     */
    public function addPrefix(string $prefix, array $defaults = [], array $requirements = []) : void {
        $prefix = trim(trim($prefix), '/');
        if ('' === $prefix) {
            return;
        }
        foreach ($this->routes as $route) {
            $route->setPath('/' . $prefix . $route->getPath());
            $route->addDefaults($defaults);
            $route->addRequirements($requirements);
        }
    }
    
    /**
     * Adds a prefix to the name of all the routes within in the collection.
     */
    public function addNamePrefix(string $prefix) : void {
        $prefixedRoutes = [];
        $prefixedPriorities = [];
        $prefixedAliases = [];
        foreach ($this->routes as $name => $route) {
            $prefixedRoutes[$prefix . $name] = $route;
            if (null !== ($canonicalName = $route->getDefault('_canonical_route'))) {
                $route->setDefault('_canonical_route', $prefix . $canonicalName);
            }
            if (isset($this->priorities[$name])) {
                $prefixedPriorities[$prefix . $name] = $this->priorities[$name];
            }
        }
        foreach ($this->aliases as $name => $alias) {
            $prefixedAliases[$prefix . $name] = $alias->withId($prefix . $alias->getId());
        }
        $this->routes = $prefixedRoutes;
        $this->priorities = $prefixedPriorities;
        $this->aliases = $prefixedAliases;
    }
    
    /**
     * Sets the host pattern on all routes.
     */
    public function setHost(?string $pattern, array $defaults = [], array $requirements = []) : void {
        foreach ($this->routes as $route) {
            $route->setHost($pattern);
            $route->addDefaults($defaults);
            $route->addRequirements($requirements);
        }
    }
    
    /**
     * Sets a condition on all routes.
     *
     * Existing conditions will be overridden.
     */
    public function setCondition(?string $condition) : void {
        foreach ($this->routes as $route) {
            $route->setCondition($condition);
        }
    }
    
    /**
     * Adds defaults to all routes.
     *
     * An existing default value under the same name in a route will be overridden.
     */
    public function addDefaults(array $defaults) : void {
        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.
     */
    public function addRequirements(array $requirements) : void {
        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.
     */
    public function addOptions(array $options) : void {
        if ($options) {
            foreach ($this->routes as $route) {
                $route->addOptions($options);
            }
        }
    }
    
    /**
     * Sets the schemes (e.g. 'https') all child routes are restricted to.
     *
     * @param string|string[] $schemes The scheme or an array of schemes
     */
    public function setSchemes(string|array $schemes) : void {
        foreach ($this->routes as $route) {
            $route->setSchemes($schemes);
        }
    }
    
    /**
     * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
     *
     * @param string|string[] $methods The method or an array of methods
     */
    public function setMethods(string|array $methods) : void {
        foreach ($this->routes as $route) {
            $route->setMethods($methods);
        }
    }
    
    /**
     * Returns an array of resources loaded to build this collection.
     *
     * @return ResourceInterface[]
     */
    public function getResources() : array {
        return array_values($this->resources);
    }
    
    /**
     * Adds a resource for this collection. If the resource already exists
     * it is not added.
     */
    public function addResource(ResourceInterface $resource) : void {
        $key = (string) $resource;
        if (!isset($this->resources[$key])) {
            $this->resources[$key] = $resource;
        }
    }
    
    /**
     * Sets an alias for an existing route.
     *
     * @param string $name  The alias to create
     * @param string $alias The route to alias
     *
     * @throws InvalidArgumentException if the alias is for itself
     */
    public function addAlias(string $name, string $alias) : Alias {
        if ($name === $alias) {
            throw new InvalidArgumentException(\sprintf('Route alias "%s" can not reference itself.', $name));
        }
        unset($this->routes[$name], $this->priorities[$name]);
        return $this->aliases[$name] = new Alias($alias);
    }
    
    /**
     * @return array<string, Alias>
     */
    public function getAliases() : array {
        return $this->aliases;
    }
    public function getAlias(string $name) : ?Alias {
        return $this->aliases[$name] ?? null;
    }
    public function getPriority(string $name) : ?int {
        return $this->priorities[$name] ?? null;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
RouteCollection::$aliases private property
RouteCollection::$priorities private property
RouteCollection::$resources private property
RouteCollection::$routes private property
RouteCollection::add public function
RouteCollection::addAlias public function Sets an alias for an existing route.
RouteCollection::addCollection public function Adds a route collection at the end of the current set by appending all
routes of the added collection.
RouteCollection::addDefaults public function Adds defaults to all routes.
RouteCollection::addNamePrefix public function Adds a prefix to the name of all the routes within in the collection.
RouteCollection::addOptions public function Adds options to all routes.
RouteCollection::addPrefix public function Adds a prefix to the path of all child routes.
RouteCollection::addRequirements public function Adds requirements to all routes.
RouteCollection::addResource public function Adds a resource for this collection. If the resource already exists
it is not added.
RouteCollection::all public function Returns all routes in this collection. 1
RouteCollection::count public function Gets the number of Routes in this collection. 1
RouteCollection::get public function Gets a route by name. 1
RouteCollection::getAlias public function
RouteCollection::getAliases public function
RouteCollection::getIterator public function Gets the current RouteCollection as an Iterator that includes all routes. 1
RouteCollection::getPriority public function
RouteCollection::getResources public function Returns an array of resources loaded to build this collection.
RouteCollection::remove public function Removes a route or an array of routes by name from the collection.
RouteCollection::setCondition public function Sets a condition on all routes.
RouteCollection::setHost public function Sets the host pattern on all routes.
RouteCollection::setMethods public function Sets the HTTP methods (e.g. &#039;POST&#039;) all child routes are restricted to.
RouteCollection::setSchemes public function Sets the schemes (e.g. &#039;https&#039;) all child routes are restricted to.
RouteCollection::__clone public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal