2010-07-21 16:43:12 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2011-05-19 21:46:34 +01:00
|
|
|
use Doctrine\Common\Annotations\Reader;
|
2010-07-21 16:43:12 +01:00
|
|
|
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2010-12-21 13:18:51 +00:00
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
|
|
|
use Symfony\Component\Config\Loader\LoaderResolver;
|
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()
|
|
|
|
* {
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-07-21 16:43:12 +01:00
|
|
|
*/
|
|
|
|
abstract class AnnotationClassLoader implements LoaderInterface
|
|
|
|
{
|
|
|
|
protected $reader;
|
2011-03-19 16:40:24 +00:00
|
|
|
protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
|
2011-07-12 14:30:32 +01:00
|
|
|
protected $defaultRouteIndex;
|
2010-07-21 16:43:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2010-12-06 06:48:44 +00:00
|
|
|
*
|
2011-05-19 21:46:34 +01:00
|
|
|
* @param Reader $reader
|
2010-07-21 16:43:12 +01:00
|
|
|
*/
|
2011-05-19 21:46:34 +01:00
|
|
|
public function __construct(Reader $reader)
|
2010-07-21 16:43:12 +01:00
|
|
|
{
|
|
|
|
$this->reader = $reader;
|
|
|
|
}
|
|
|
|
|
2010-12-21 00:50:16 +00:00
|
|
|
/**
|
|
|
|
* Sets the annotation class to read route properties from.
|
|
|
|
*
|
2011-03-19 16:40:24 +00:00
|
|
|
* @param string $class A fully-qualified class name
|
2010-12-21 00:50:16 +00:00
|
|
|
*/
|
2011-03-19 16:40:24 +00:00
|
|
|
public function setRouteAnnotationClass($class)
|
2010-12-21 00:50:16 +00:00
|
|
|
{
|
2011-03-19 16:40:24 +00:00
|
|
|
$this->routeAnnotationClass = $class;
|
|
|
|
}
|
|
|
|
|
2010-07-21 16:43:12 +01:00
|
|
|
/**
|
|
|
|
* Loads from annotations from a class.
|
|
|
|
*
|
2010-12-06 06:48:44 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2010-12-06 06:48:44 +00:00
|
|
|
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(),
|
2010-09-07 12:55:45 +01:00
|
|
|
'defaults' => array(),
|
2010-07-21 16:43:12 +01:00
|
|
|
);
|
|
|
|
|
2010-12-21 00:50:16 +00:00
|
|
|
$class = new \ReflectionClass($class);
|
2011-08-30 19:57:43 +01:00
|
|
|
if ($class->isAbstract()) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class));
|
|
|
|
}
|
|
|
|
|
2011-03-19 16:40:24 +00:00
|
|
|
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
|
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();
|
|
|
|
}
|
2010-09-07 12:55:45 +01:00
|
|
|
|
|
|
|
if (null !== $annot->getDefaults()) {
|
|
|
|
$globals['defaults'] = $annot->getDefaults();
|
|
|
|
}
|
2010-07-21 16:43:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
2010-12-21 13:18:51 +00:00
|
|
|
$collection->addResource(new FileResource($class->getFileName()));
|
2011-03-19 16:40:24 +00:00
|
|
|
|
2010-07-21 16:43:12 +01:00
|
|
|
foreach ($class->getMethods() as $method) {
|
2011-07-12 14:30:32 +01:00
|
|
|
$this->defaultRouteIndex = 0;
|
2011-04-29 16:44:56 +01:00
|
|
|
foreach ($this->reader->getMethodAnnotations($method) as $annot) {
|
|
|
|
if ($annot instanceof $this->routeAnnotationClass) {
|
2011-04-25 14:45:53 +01:00
|
|
|
$this->addRoute($collection, $annot, $globals, $class, $method);
|
2010-07-21 16:43:12 +01:00
|
|
|
}
|
2011-03-19 16:40:24 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-21 16:43:12 +01:00
|
|
|
|
2011-03-19 16:40:24 +00:00
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2011-04-25 14:45:53 +01:00
|
|
|
protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
|
2011-03-19 16:40:24 +00:00
|
|
|
{
|
2011-08-30 20:37:30 +01:00
|
|
|
$name = $annot->getName();
|
|
|
|
if (null === $name) {
|
|
|
|
$name = $this->getDefaultRouteName($class, $method);
|
2011-03-19 16:40:24 +00:00
|
|
|
}
|
2010-07-21 16:43:12 +01:00
|
|
|
|
2011-03-19 16:40:24 +00:00
|
|
|
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
|
|
|
|
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
|
|
|
|
$options = array_merge($globals['options'], $annot->getOptions());
|
2010-11-30 13:00:48 +00:00
|
|
|
|
2011-03-19 16:40:24 +00:00
|
|
|
$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
|
2010-11-30 13:00:48 +00:00
|
|
|
|
2011-03-19 16:40:24 +00:00
|
|
|
$this->configureRoute($route, $class, $method, $annot);
|
2010-07-21 16:43:12 +01:00
|
|
|
|
2011-08-30 20:37:30 +01:00
|
|
|
$collection->add($name, $route);
|
2010-07-21 16:43:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this class supports the given resource.
|
|
|
|
*
|
2010-12-06 06:48:44 +00:00
|
|
|
* @param mixed $resource A resource
|
|
|
|
* @param string $type The resource type
|
2010-07-21 16:43:12 +01:00
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean True if this class supports the given resource, false otherwise
|
2010-07-21 16:43:12 +01:00
|
|
|
*/
|
2010-12-06 06:48:44 +00:00
|
|
|
public function supports($resource, $type = null)
|
2010-07-21 16:43:12 +01:00
|
|
|
{
|
2010-12-06 06:48:44 +00: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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-06 06:48:44 +00:00
|
|
|
/**
|
|
|
|
* Gets the default route name for a class method.
|
|
|
|
*
|
|
|
|
* @param \ReflectionClass $class
|
|
|
|
* @param \ReflectionMethod $method
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
|
|
|
|
{
|
2011-07-12 14:30:32 +01:00
|
|
|
$name = strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
|
|
|
|
if ($this->defaultRouteIndex > 0) {
|
|
|
|
$name .= '_'.$this->defaultRouteIndex;
|
|
|
|
}
|
|
|
|
$this->defaultRouteIndex++;
|
|
|
|
|
|
|
|
return $name;
|
2010-12-06 06:48:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 16:40:24 +00:00
|
|
|
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
|
2010-07-21 16:43:12 +01:00
|
|
|
}
|