function AttributeRouteDiscovery::getGlobals

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

Creates the default route settings for a class.

A class can use the route attribute on the class to set defaults for all attributed methods on the class.

Parameters

\ReflectionClass $class: The class to create global settings for.

Return value

array An array of route defaults.

1 call to AttributeRouteDiscovery::getGlobals()
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 146

Class

AttributeRouteDiscovery
Discovers routes using Symfony's Route attribute.

Namespace

Drupal\Core\Routing

Code

private function getGlobals(\ReflectionClass $class) : array {
  $globals = $this->resetGlobals();
  /** @var \Symfony\Component\Routing\Attribute\Route $attribute */
  $attribute = ($class->getAttributes(RouteAttribute::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? NULL)?->newInstance();
  if ($attribute) {
    if ($attribute->name !== NULL) {
      $globals['name'] = $attribute->name;
    }
    if ($attribute->path !== NULL) {
      $globals['path'] = $attribute->path;
      if (is_array($attribute->path)) {
        throw new UnsupportedRouteAttributePropertyException(sprintf('The "%s" route attribute does not support arrays in class "%s"', "path", $class->getName()));
      }
    }
    if ($attribute->requirements !== NULL) {
      $globals['requirements'] = $attribute->requirements;
    }
    if ($attribute->options !== NULL) {
      $globals['options'] = $attribute->options;
    }
    if ($attribute->defaults !== NULL) {
      $globals['defaults'] = $attribute->defaults;
      if (!empty($attribute->defaults['_locale'])) {
        throw new UnsupportedRouteAttributePropertyException(sprintf('The "%s" route attribute is not supported in class "%s""', "locale", $class->getName()));
      }
    }
    if ($attribute->schemes !== NULL) {
      $globals['schemes'] = $attribute->schemes;
    }
    if ($attribute->methods !== NULL) {
      $globals['methods'] = $attribute->methods;
    }
    if ($attribute->host !== NULL) {
      $globals['host'] = $attribute->host;
    }
    if ($attribute->condition !== NULL) {
      throw new UnsupportedRouteAttributePropertyException(sprintf('The "%s" route attribute is not supported in class "%s"', "condition", $class->getName()));
    }
    $globals['priority'] = $attribute->priority ?? 0;
    foreach ($globals['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" in "%s"?', $placeholder, $requirement, $class->getName()));
      }
    }
  }
  return $globals;
}

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