This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

193 lines
5.9 KiB
PHP
Raw Normal View History

2010-07-21 16:43:12 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2010-07-21 16:43:12 +01:00
namespace Symfony\Component\Routing\Loader;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
use Symfony\Component\Routing\Loader\LoaderResolver;
use Symfony\Component\Routing\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
2010-07-21 16:43:12 +01:00
/**
* AnnotationClassLoader loads routing information from a PHP class and its methods.
*
* You need to define an implementation for the getRouteDefaults() method. Most of the
* time, this method should define some PHP callable to be called for the route
* (a controller in MVC speak).
*
* The @Route annotation can be set on the class (for global parameters),
* and on each method.
*
* The @Route annotation main value is the route pattern. The annotation also
* recognizes three parameters: requirements, options, and name. The name parameter
* is mandatory. Here is an example of how you should be able to use it:
*
* /**
* * @Route("/Blog")
* * /
* class Blog
* {
* /**
* * @Route("/", name="blog_index")
* * /
* public function index()
* {
* }
*
* /**
2011-01-12 06:10:57 +00:00
* * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
2010-07-21 16:43:12 +01:00
* * /
* public function show()
* {
* }
* }
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2010-07-21 16:43:12 +01:00
*/
abstract class AnnotationClassLoader implements LoaderInterface
{
protected $reader;
protected $annotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
2010-07-21 16:43:12 +01:00
/**
* Constructor.
*
* @param AnnotationReader $reader
2010-07-21 16:43:12 +01:00
*/
public function __construct(AnnotationReader $reader)
{
$this->reader = $reader;
}
/**
* Sets the annotation class to read route properties from.
*
* @param string $annotationClass A fully-qualified class name
*/
public function setAnnotationClass($annotationClass)
{
$this->annotationClass = $annotationClass;
}
2010-07-21 16:43:12 +01:00
/**
* Loads from annotations from a class.
*
* @param string $class A class name
* @param string $type The resource type
2010-07-21 16:43:12 +01:00
*
* @return RouteCollection A RouteCollection instance
*
* @throws \InvalidArgumentException When route can't be parsed
*/
public function load($class, $type = null)
2010-07-21 16:43:12 +01:00
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$globals = array(
'pattern' => '',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
2010-07-21 16:43:12 +01:00
);
$class = new \ReflectionClass($class);
if ($annot = $this->reader->getClassAnnotation($class, $this->annotationClass)) {
2010-07-21 16:43:12 +01:00
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();
}
2010-07-21 16:43:12 +01:00
}
$collection = new RouteCollection();
$collection->addResource(new FileResource($class->getFileName()));
2010-07-21 16:43:12 +01:00
foreach ($class->getMethods() as $method) {
if ($annot = $this->reader->getMethodAnnotation($method, $this->annotationClass)) {
2010-07-21 16:43:12 +01:00
if (null === $annot->getName()) {
$annot->setName($this->getDefaultRouteName($class, $method));
}
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
2010-07-21 16:43:12 +01:00
$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);
2010-07-21 16:43:12 +01:00
}
}
return $collection;
}
/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
* @param string $type The resource type
2010-07-21 16:43:12 +01:00
*
* @return boolean True if this class supports the given resource, false otherwise
2010-07-21 16:43:12 +01:00
*/
public function supports($resource, $type = null)
2010-07-21 16:43:12 +01:00
{
return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
2010-07-21 16:43:12 +01:00
}
/**
* Sets the loader resolver.
*
* @param LoaderResolver $resolver A LoaderResolver instance
*/
public function setResolver(LoaderResolver $resolver)
{
}
/**
* Gets the loader resolver.
*
* @return LoaderResolver A LoaderResolver instance
*/
public function getResolver()
{
}
/**
* Gets the default route name for a class method.
*
* @param \ReflectionClass $class
* @param \ReflectionMethod $method
*
* @return string
*/
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
{
return strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
}
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method);
2010-07-21 16:43:12 +01:00
}