4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Loader;
14 use Doctrine\Common\Annotations\Reader;
15 use Symfony\Component\Config\Loader\LoaderInterface;
16 use Symfony\Component\Config\Loader\LoaderResolverInterface;
17 use Symfony\Component\Config\Resource\FileResource;
18 use Symfony\Component\Routing\Route;
19 use Symfony\Component\Routing\RouteCollection;
22 * AnnotationClassLoader loads routing information from a PHP class and its methods.
24 * You need to define an implementation for the getRouteDefaults() method. Most of the
25 * time, this method should define some PHP callable to be called for the route
26 * (a controller in MVC speak).
28 * The @Route annotation can be set on the class (for global parameters),
31 * The @Route annotation main value is the route path. The annotation also
32 * recognizes several parameters: requirements, options, defaults, schemes,
33 * methods, host, and name. The name parameter is mandatory.
34 * Here is an example of how you should be able to use it:
42 * * @Route("/", name="blog_index")
44 * public function index()
49 * * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
51 * public function show()
56 * @author Fabien Potencier <fabien@symfony.com>
58 abstract class AnnotationClassLoader implements LoaderInterface
65 protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
70 protected $defaultRouteIndex = 0;
72 public function __construct(Reader $reader)
74 $this->reader = $reader;
78 * Sets the annotation class to read route properties from.
80 * @param string $class A fully-qualified class name
82 public function setRouteAnnotationClass($class)
84 $this->routeAnnotationClass = $class;
88 * Loads from annotations from a class.
90 * @param string $class A class name
91 * @param string|null $type The resource type
93 * @return RouteCollection A RouteCollection instance
95 * @throws \InvalidArgumentException When route can't be parsed
97 public function load($class, $type = null)
99 if (!class_exists($class)) {
100 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
103 $class = new \ReflectionClass($class);
104 if ($class->isAbstract()) {
105 throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
108 $globals = $this->getGlobals($class);
110 $collection = new RouteCollection();
111 $collection->addResource(new FileResource($class->getFileName()));
113 foreach ($class->getMethods() as $method) {
114 $this->defaultRouteIndex = 0;
115 foreach ($this->reader->getMethodAnnotations($method) as $annot) {
116 if ($annot instanceof $this->routeAnnotationClass) {
117 $this->addRoute($collection, $annot, $globals, $class, $method);
122 if (0 === $collection->count() && $class->hasMethod('__invoke')) {
123 foreach ($this->reader->getClassAnnotations($class) as $annot) {
124 if ($annot instanceof $this->routeAnnotationClass) {
125 $globals['path'] = '';
126 $globals['name'] = '';
128 $this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
136 protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
138 $name = $annot->getName();
139 if (null === $name) {
140 $name = $this->getDefaultRouteName($class, $method);
142 $name = $globals['name'].$name;
144 $defaults = array_replace($globals['defaults'], $annot->getDefaults());
145 foreach ($method->getParameters() as $param) {
146 if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
147 $defaults[$param->getName()] = $param->getDefaultValue();
150 $requirements = array_replace($globals['requirements'], $annot->getRequirements());
151 $options = array_replace($globals['options'], $annot->getOptions());
152 $schemes = array_merge($globals['schemes'], $annot->getSchemes());
153 $methods = array_merge($globals['methods'], $annot->getMethods());
155 $host = $annot->getHost();
156 if (null === $host) {
157 $host = $globals['host'];
160 $condition = $annot->getCondition();
161 if (null === $condition) {
162 $condition = $globals['condition'];
165 $route = $this->createRoute($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
167 $this->configureRoute($route, $class, $method, $annot);
169 $collection->add($name, $route);
175 public function supports($resource, $type = null)
177 return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
183 public function setResolver(LoaderResolverInterface $resolver)
190 public function getResolver()
195 * Gets the default route name for a class method.
197 * @param \ReflectionClass $class
198 * @param \ReflectionMethod $method
202 protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
204 $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name);
205 if ($this->defaultRouteIndex > 0) {
206 $name .= '_'.$this->defaultRouteIndex;
208 ++$this->defaultRouteIndex;
213 protected function getGlobals(\ReflectionClass $class)
217 'requirements' => array(),
218 'options' => array(),
219 'defaults' => array(),
220 'schemes' => array(),
221 'methods' => array(),
227 if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
228 if (null !== $annot->getName()) {
229 $globals['name'] = $annot->getName();
232 if (null !== $annot->getPath()) {
233 $globals['path'] = $annot->getPath();
236 if (null !== $annot->getRequirements()) {
237 $globals['requirements'] = $annot->getRequirements();
240 if (null !== $annot->getOptions()) {
241 $globals['options'] = $annot->getOptions();
244 if (null !== $annot->getDefaults()) {
245 $globals['defaults'] = $annot->getDefaults();
248 if (null !== $annot->getSchemes()) {
249 $globals['schemes'] = $annot->getSchemes();
252 if (null !== $annot->getMethods()) {
253 $globals['methods'] = $annot->getMethods();
256 if (null !== $annot->getHost()) {
257 $globals['host'] = $annot->getHost();
260 if (null !== $annot->getCondition()) {
261 $globals['condition'] = $annot->getCondition();
268 protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
270 return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
273 abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);