function AttributeRouteDiscovery::addRoute

Same name and namespace in other branches
  1. main core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php \Drupal\Core\Routing\AttributeRouteDiscovery::addRoute()

Adds a route to the provided route collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection to add the route to.

\Symfony\Component\Routing\Annotation\Route $attribute: The attribute object that describes the route.

array $globals: The defaults for the class.

\ReflectionClass $class: The class.

\ReflectionMethod $method: The attributed method.

1 call to AttributeRouteDiscovery::addRoute()
AttributeRouteDiscovery::createRouteCollection in core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php
Creates a route collection from a class's attributed methods.

File

core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php, line 220

Class

AttributeRouteDiscovery
Discovers routes using Symfony's Route attribute.

Namespace

Drupal\Core\Routing

Code

private function addRoute(RouteCollection $collection, RouteAttribute $attribute, array $globals, \ReflectionClass $class, \ReflectionMethod $method) : void {
  if ($attribute->name === NULL) {
    throw new UnsupportedRouteAttributePropertyException(sprintf('The Route attribute on "%s::%s()" is missing a required "name" property.', $class->getName(), $method->getName()));
  }
  $name = $globals['name'] . $attribute->name;
  if (is_array($attribute->path)) {
    throw new UnsupportedRouteAttributePropertyException(sprintf('The "%s" route attribute does not support arrays on route "%s" in "%s::%s()"', "path", $name, $class->getName(), $method->getName()));
  }
  if (!empty($attribute->defaults['_locale'])) {
    throw new UnsupportedRouteAttributePropertyException(sprintf('The "%s" route attribute is not supported on route "%s" in "%s::%s()"', "locale", $name, $class->getName(), $method->getName()));
  }
  if ($attribute->condition !== NULL) {
    throw new UnsupportedRouteAttributePropertyException(sprintf('The "%s" route attribute is not supported on route "%s" in "%s::%s()"', "condition", $name, $class->getName(), $method->getName()));
  }
  $requirements = $attribute->requirements;
  foreach ($requirements as $placeholder => $requirement) {
    if (\is_int($placeholder)) {
      throw new \InvalidArgumentException(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s::%s()"?', $placeholder, $requirement, $name, $class->getName(), $method->getName()));
    }
  }
  $defaults = array_replace($globals['defaults'], $attribute->defaults);
  $requirements = array_replace($globals['requirements'], $requirements);
  $options = array_replace($globals['options'], $attribute->options);
  $schemes = array_unique(array_merge($globals['schemes'], $attribute->schemes));
  $methods = array_unique(array_merge($globals['methods'], $attribute->methods));
  $host = $attribute->host ?? $globals['host'];
  $priority = $attribute->priority ?? $globals['priority'];
  $path = $attribute->path;
  $prefix = $globals['path'];
  $route = $this->createRoute($prefix . $path, $defaults, $requirements, $options, $host, $schemes, $methods, NULL);
  $this->configureRoute($route, $class, $method);
  $collection->add($name, $route, $priority);
  foreach ($attribute->aliases as $aliasAttribute) {
    if ($aliasAttribute instanceof DeprecatedAlias) {
      $alias = $collection->addAlias($aliasAttribute->aliasName, $name);
      $alias->setDeprecated($aliasAttribute->package, $aliasAttribute->version, $aliasAttribute->message);
      continue;
    }
    $collection->addAlias($aliasAttribute, $name);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.