Loads from annotations from a class.
string $class A class name:
string|null $type The resource type:
RouteCollection A RouteCollection instance
\InvalidArgumentException When route can't be parsed
public function load($class, $type = null) {
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$globals = array(
'path' => '',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
'schemes' => array(),
'methods' => array(),
'host' => '',
);
$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)) {
// for BC reasons
if (null !== $annot
->getPath()) {
$globals['path'] = $annot
->getPath();
}
elseif (null !== $annot
->getPattern()) {
$globals['path'] = $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();
}
if (null !== $annot
->getSchemes()) {
$globals['schemes'] = $annot
->getSchemes();
}
if (null !== $annot
->getMethods()) {
$globals['methods'] = $annot
->getMethods();
}
if (null !== $annot
->getHost()) {
$globals['host'] = $annot
->getHost();
}
}
$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;
}