AnnotationClassLoader.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Loader;
  11. use Doctrine\Common\Annotations\Reader;
  12. use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\Routing\Route;
  15. use Symfony\Component\Routing\RouteCollection;
  16. use Symfony\Component\Config\Loader\LoaderInterface;
  17. use Symfony\Component\Config\Loader\LoaderResolver;
  18. /**
  19. * AnnotationClassLoader loads routing information from a PHP class and its methods.
  20. *
  21. * You need to define an implementation for the getRouteDefaults() method. Most of the
  22. * time, this method should define some PHP callable to be called for the route
  23. * (a controller in MVC speak).
  24. *
  25. * The @Route annotation can be set on the class (for global parameters),
  26. * and on each method.
  27. *
  28. * The @Route annotation main value is the route pattern. The annotation also
  29. * recognizes three parameters: requirements, options, and name. The name parameter
  30. * is mandatory. Here is an example of how you should be able to use it:
  31. *
  32. * /**
  33. * * @Route("/Blog")
  34. * * /
  35. * class Blog
  36. * {
  37. * /**
  38. * * @Route("/", name="blog_index")
  39. * * /
  40. * public function index()
  41. * {
  42. * }
  43. *
  44. * /**
  45. * * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
  46. * * /
  47. * public function show()
  48. * {
  49. * }
  50. * }
  51. *
  52. * @author Fabien Potencier <fabien@symfony.com>
  53. */
  54. abstract class AnnotationClassLoader implements LoaderInterface
  55. {
  56. protected $reader;
  57. protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
  58. protected $defaultRouteIndex;
  59. /**
  60. * Constructor.
  61. *
  62. * @param Reader $reader
  63. */
  64. public function __construct(Reader $reader)
  65. {
  66. $this->reader = $reader;
  67. }
  68. /**
  69. * Sets the annotation class to read route properties from.
  70. *
  71. * @param string $class A fully-qualified class name
  72. */
  73. public function setRouteAnnotationClass($class)
  74. {
  75. $this->routeAnnotationClass = $class;
  76. }
  77. /**
  78. * Loads from annotations from a class.
  79. *
  80. * @param string $class A class name
  81. * @param string $type The resource type
  82. *
  83. * @return RouteCollection A RouteCollection instance
  84. *
  85. * @throws \InvalidArgumentException When route can't be parsed
  86. */
  87. public function load($class, $type = null)
  88. {
  89. if (!class_exists($class)) {
  90. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  91. }
  92. $globals = array(
  93. 'pattern' => '',
  94. 'requirements' => array(),
  95. 'options' => array(),
  96. 'defaults' => array(),
  97. );
  98. $class = new \ReflectionClass($class);
  99. if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
  100. if (null !== $annot->getPattern()) {
  101. $globals['pattern'] = $annot->getPattern();
  102. }
  103. if (null !== $annot->getRequirements()) {
  104. $globals['requirements'] = $annot->getRequirements();
  105. }
  106. if (null !== $annot->getOptions()) {
  107. $globals['options'] = $annot->getOptions();
  108. }
  109. if (null !== $annot->getDefaults()) {
  110. $globals['defaults'] = $annot->getDefaults();
  111. }
  112. }
  113. $collection = new RouteCollection();
  114. $collection->addResource(new FileResource($class->getFileName()));
  115. foreach ($class->getMethods() as $method) {
  116. $this->defaultRouteIndex = 0;
  117. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  118. if ($annot instanceof $this->routeAnnotationClass) {
  119. $this->addRoute($collection, $annot, $globals, $class, $method);
  120. }
  121. }
  122. }
  123. return $collection;
  124. }
  125. protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
  126. {
  127. if (null === $annot->getName()) {
  128. $annot->setName($this->getDefaultRouteName($class, $method));
  129. }
  130. $defaults = array_merge($globals['defaults'], $annot->getDefaults());
  131. $requirements = array_merge($globals['requirements'], $annot->getRequirements());
  132. $options = array_merge($globals['options'], $annot->getOptions());
  133. $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
  134. $this->configureRoute($route, $class, $method, $annot);
  135. $collection->add($annot->getName(), $route);
  136. }
  137. /**
  138. * Returns true if this class supports the given resource.
  139. *
  140. * @param mixed $resource A resource
  141. * @param string $type The resource type
  142. *
  143. * @return Boolean True if this class supports the given resource, false otherwise
  144. */
  145. public function supports($resource, $type = null)
  146. {
  147. return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
  148. }
  149. /**
  150. * Sets the loader resolver.
  151. *
  152. * @param LoaderResolver $resolver A LoaderResolver instance
  153. */
  154. public function setResolver(LoaderResolver $resolver)
  155. {
  156. }
  157. /**
  158. * Gets the loader resolver.
  159. *
  160. * @return LoaderResolver A LoaderResolver instance
  161. */
  162. public function getResolver()
  163. {
  164. }
  165. /**
  166. * Gets the default route name for a class method.
  167. *
  168. * @param \ReflectionClass $class
  169. * @param \ReflectionMethod $method
  170. *
  171. * @return string
  172. */
  173. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  174. {
  175. $name = strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
  176. if ($this->defaultRouteIndex > 0) {
  177. $name .= '_'.$this->defaultRouteIndex;
  178. }
  179. $this->defaultRouteIndex++;
  180. return $name;
  181. }
  182. abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
  183. }