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\Resource\FileResource;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Component\Routing\RouteCollection;
18 use Symfony\Component\Config\Loader\LoaderInterface;
19 use Symfony\Component\Config\Loader\LoaderResolverInterface;
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
68 protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
73 protected $defaultRouteIndex = 0;
78 * @param Reader $reader
80 public function __construct(Reader $reader)
82 $this->reader = $reader;
86 * Sets the annotation class to read route properties from.
88 * @param string $class A fully-qualified class name
90 public function setRouteAnnotationClass($class)
92 $this->routeAnnotationClass = $class;
96 * Loads from annotations from a class.
98 * @param string $class A class name
99 * @param string|null $type The resource type
101 * @return RouteCollection A RouteCollection instance
103 * @throws \InvalidArgumentException When route can't be parsed
105 public function load($class, $type = null)
107 if (!class_exists($class)) {
108 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
111 $class = new \ReflectionClass($class);
112 if ($class->isAbstract()) {
113 throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
116 $globals = $this->getGlobals($class);
118 $collection = new RouteCollection();
119 $collection->addResource(new FileResource($class->getFileName()));
121 foreach ($class->getMethods() as $method) {
122 $this->defaultRouteIndex = 0;
123 foreach ($this->reader->getMethodAnnotations($method) as $annot) {
124 if ($annot instanceof $this->routeAnnotationClass) {
125 $this->addRoute($collection, $annot, $globals, $class, $method);
133 protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
135 $name = $annot->getName();
136 if (null === $name) {
137 $name = $this->getDefaultRouteName($class, $method);
140 $defaults = array_replace($globals['defaults'], $annot->getDefaults());
141 foreach ($method->getParameters() as $param) {
142 if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
143 $defaults[$param->getName()] = $param->getDefaultValue();
146 $requirements = array_replace($globals['requirements'], $annot->getRequirements());
147 $options = array_replace($globals['options'], $annot->getOptions());
148 $schemes = array_merge($globals['schemes'], $annot->getSchemes());
149 $methods = array_merge($globals['methods'], $annot->getMethods());
151 $host = $annot->getHost();
152 if (null === $host) {
153 $host = $globals['host'];
156 $condition = $annot->getCondition();
157 if (null === $condition) {
158 $condition = $globals['condition'];
161 $route = $this->createRoute($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
163 $this->configureRoute($route, $class, $method, $annot);
165 $collection->add($name, $route);
171 public function supports($resource, $type = null)
173 return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
179 public function setResolver(LoaderResolverInterface $resolver)
186 public function getResolver()
191 * Gets the default route name for a class method.
193 * @param \ReflectionClass $class
194 * @param \ReflectionMethod $method
198 protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
200 $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name);
201 if ($this->defaultRouteIndex > 0) {
202 $name .= '_'.$this->defaultRouteIndex;
204 ++$this->defaultRouteIndex;
209 protected function getGlobals(\ReflectionClass $class)
213 'requirements' => array(),
214 'options' => array(),
215 'defaults' => array(),
216 'schemes' => array(),
217 'methods' => array(),
222 if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
224 if (null !== $annot->getPath()) {
225 $globals['path'] = $annot->getPath();
226 } elseif (null !== $annot->getPattern()) {
227 $globals['path'] = $annot->getPattern();
230 if (null !== $annot->getRequirements()) {
231 $globals['requirements'] = $annot->getRequirements();
234 if (null !== $annot->getOptions()) {
235 $globals['options'] = $annot->getOptions();
238 if (null !== $annot->getDefaults()) {
239 $globals['defaults'] = $annot->getDefaults();
242 if (null !== $annot->getSchemes()) {
243 $globals['schemes'] = $annot->getSchemes();
246 if (null !== $annot->getMethods()) {
247 $globals['methods'] = $annot->getMethods();
250 if (null !== $annot->getHost()) {
251 $globals['host'] = $annot->getHost();
254 if (null !== $annot->getCondition()) {
255 $globals['condition'] = $annot->getCondition();
262 protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
264 return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
267 abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);