Symfony\Component\Routing\Loader\AnnotationClassLoader::load PHP Method

load() public method

Loads from annotations from a class.
public load ( string $class ) : RouteCollection
$class string A class name
return Symfony\Component\Routing\RouteCollection A RouteCollection instance
    public function load($class)
    {
        if (!class_exists($class)) {
            throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
        }

        $class = new \ReflectionClass($class);
        $annotClass = 'Symfony\\Component\\Routing\\Annotation\\Route';

        $globals = array(
            'pattern'      => '',
            'requirements' => array(),
            'options'      => array(),
            'defaults'     => array(),
        );

        if ($annot = $this->reader->getClassAnnotation($class, $annotClass)) {
            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();
            }
        }

        $this->reader->setDefaultAnnotationNamespace('Symfony\\Component\\Routing\\Annotation\\');
        $collection = new RouteCollection();
        foreach ($class->getMethods() as $method) {
            if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
                if (null === $annot->getName()) {
                    $annot->setName($this->getDefaultRouteName($class, $method));
                }

                $defaults = array_merge($globals['defaults'], $annot->getDefaults());
                $requirements = array_merge($globals['requirements'], $annot->getRequirements());
                $options = array_merge($globals['options'], $annot->getOptions());

                $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);

                $this->configureRoute($route, $class, $method);

                $collection->add($annot->getName(), $route);
            }
        }

        return $collection;
    }

Same methods

AnnotationClassLoader::load ( string $class, string $type = null ) : RouteCollection

Usage Example

Example #1
0
 /**
  * {@inheritdoc}
  */
 public function load($class, $type = null)
 {
     $collection = parent::load($class, $type);
     $class = new \ReflectionClass($class);
     $globals = $this->getGlobals($class);
     foreach ($class->getMethods() as $method) {
         // Manual route definition
         if (null !== $this->reader->getMethodAnnotation($method, $this->routeAnnotationClass) || !$method->isPublic() || !preg_match('/^(.*)Action$/', $method->name, $matches)) {
             continue;
         }
         $actionName =& $matches[1];
         $route = $this->createRoute($globals['path'], $globals['defaults'], $globals['requirements'], $globals['options'], $globals['host'], $globals['schemes'], [strtoupper($actionName)], $globals['condition']);
         $this->configureRoute($route, $class, $method, null);
         $collection->add($this->getDefaultRouteName($class, $method), $route);
     }
     return $collection;
 }