You are here

class RouteCollection in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/routing/RouteCollection.php \Symfony\Component\Routing\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>

Hierarchy

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

Expanded class hierarchy of RouteCollection

97 files declare their use of RouteCollection
AcceptHeaderMatcher.php in core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php
Contains \Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher.
AccessManagerTest.php in core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
Contains \Drupal\Tests\Core\Access\AccessManagerTest.
AdminRouteSubscriber.php in core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php
Contains \Drupal\system\EventSubscriber\AdminRouteSubscriber.
AnnotationClassLoader.php in vendor/symfony/routing/Loader/AnnotationClassLoader.php
AnnotationDirectoryLoader.php in vendor/symfony/routing/Loader/AnnotationDirectoryLoader.php

... See full list

File

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

Namespace

Symfony\Component\Routing
View source
class RouteCollection implements \IteratorAggregate, \Countable {

  /**
   * @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.
   *
   * It implements \IteratorAggregate.
   *
   * @see all()
   *
   * @return \ArrayIterator An \ArrayIterator object for iterating over routes
   */
  public function getIterator() {
    return new \ArrayIterator($this->routes);
  }

  /**
   * 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
   */
  public function add($name, Route $route) {
    unset($this->routes[$name]);
    $this->routes[$name] = $route;
  }

  /**
   * Returns all routes in this collection.
   *
   * @return Route[] An array of routes
   */
  public function all() {
    return $this->routes;
  }

  /**
   * Gets a route by name.
   *
   * @param string $name The route name
   *
   * @return Route|null A Route instance or null when not found
   */
  public function get($name) {
    return isset($this->routes[$name]) ? $this->routes[$name] : null;
  }

  /**
   * 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.
   *
   * @param RouteCollection $collection A RouteCollection instance
   */
  public function addCollection(RouteCollection $collection) {

    // 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());
  }

  /**
   * 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
   */
  public function addPrefix($prefix, array $defaults = array(), array $requirements = array()) {
    $prefix = trim(trim($prefix), '/');
    if ('' === $prefix) {
      return;
    }
    foreach ($this->routes as $route) {
      $route
        ->setPath('/' . $prefix . $route
        ->getPath());
      $route
        ->addDefaults($defaults);
      $route
        ->addRequirements($requirements);
    }
  }

  /**
   * Sets the host pattern on all routes.
   *
   * @param string $pattern      The pattern
   * @param array  $defaults     An array of default values
   * @param array  $requirements An array of requirements
   */
  public function setHost($pattern, array $defaults = array(), array $requirements = array()) {
    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.
   *
   * @param string $condition The condition
   */
  public function setCondition($condition) {
    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.
   *
   * @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);
      }
    }
  }

  /**
   * 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);
  }

  /**
   * Adds a resource for this collection.
   *
   * @param ResourceInterface $resource A resource instance
   */
  public function addResource(ResourceInterface $resource) {
    $this->resources[] = $resource;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteCollection::$resources private property
RouteCollection::$routes private property
RouteCollection::add public function Adds a route. 1
RouteCollection::addCollection public function Adds a route collection at the end of the current set by appending all routes of the added collection. 1
RouteCollection::addDefaults public function Adds defaults to all routes. 1
RouteCollection::addOptions public function Adds options to all routes. 1
RouteCollection::addPrefix public function Adds a prefix to the path of all child routes. 1
RouteCollection::addRequirements public function Adds requirements to all routes. 1
RouteCollection::addResource public function Adds a resource for this collection. 1
RouteCollection::all public function Returns all routes in this collection. 2
RouteCollection::count public function Gets the number of Routes in this collection. 2
RouteCollection::get public function Gets a route by name. 2
RouteCollection::getIterator public function Gets the current RouteCollection as an Iterator that includes all routes. 2
RouteCollection::getResources public function Returns an array of resources loaded to build this collection. 1
RouteCollection::remove public function Removes a route or an array of routes by name from the collection. 1
RouteCollection::setCondition public function Sets a condition on all routes.
RouteCollection::setHost public function Sets the host pattern on all routes. 1
RouteCollection::setMethods public function Sets the HTTP methods (e.g. 'POST') all child routes are restricted to. 1
RouteCollection::setSchemes public function Sets the schemes (e.g. 'https') all child routes are restricted to. 1
RouteCollection::__clone public function 1