public function AnnotationClassLoader::load

Loads from annotations from a class.

Parameters

string $class A class name:

string $type The resource type:

Return value

RouteCollection A RouteCollection instance

Throws

\InvalidArgumentException When route can't be parsed

File

drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php, line 93

Class

AnnotationClassLoader
AnnotationClassLoader loads routing information from a PHP class and its methods.

Namespace

Symfony\Component\Routing\Loader

Code

public function load($class, $type = null) {
  if (!class_exists($class)) {
    throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  }
  $globals = array(
    'pattern' => '',
    'requirements' => array(),
    'options' => array(),
    'defaults' => array(),
  );
  $class = new \ReflectionClass($class);
  if ($class
    ->isAbstract()) {
    throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class));
  }
  if ($annot = $this->reader
    ->getClassAnnotation($class, $this->routeAnnotationClass)) {
    if (null !== $annot
      ->getPattern()) {
      $globals['pattern'] = $annot
        ->getPattern();
    }
    if (null !== $annot
      ->getRequirements()) {
      $globals['requirements'] = $annot
        ->getRequirements();
    }
    if (null !== $annot
      ->getOptions()) {
      $globals['options'] = $annot
        ->getOptions();
    }
    if (null !== $annot
      ->getDefaults()) {
      $globals['defaults'] = $annot
        ->getDefaults();
    }
  }
  $collection = new RouteCollection();
  $collection
    ->addResource(new FileResource($class
    ->getFileName()));
  foreach ($class
    ->getMethods() as $method) {
    $this->defaultRouteIndex = 0;
    foreach ($this->reader
      ->getMethodAnnotations($method) as $annot) {
      if ($annot instanceof $this->routeAnnotationClass) {
        $this
          ->addRoute($collection, $annot, $globals, $class, $method);
      }
    }
  }
  return $collection;
}