AnnotationClassLoader.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Annotations\ReaderInterface;
  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. /**
  59. * Constructor.
  60. *
  61. * @param ReaderInterface $reader
  62. */
  63. public function __construct(ReaderInterface $reader)
  64. {
  65. $this->reader = $reader;
  66. }
  67. /**
  68. * Sets the annotation class to read route properties from.
  69. *
  70. * @param string $class A fully-qualified class name
  71. */
  72. public function setRouteAnnotationClass($class)
  73. {
  74. $this->routeAnnotationClass = $class;
  75. }
  76. /**
  77. * Loads from annotations from a class.
  78. *
  79. * @param string $class A class name
  80. * @param string $type The resource type
  81. *
  82. * @return RouteCollection A RouteCollection instance
  83. *
  84. * @throws \InvalidArgumentException When route can't be parsed
  85. */
  86. public function load($class, $type = null)
  87. {
  88. if (!class_exists($class)) {
  89. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  90. }
  91. $globals = array(
  92. 'pattern' => '',
  93. 'requirements' => array(),
  94. 'options' => array(),
  95. 'defaults' => array(),
  96. );
  97. $class = new \ReflectionClass($class);
  98. if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
  99. if (null !== $annot->getPattern()) {
  100. $globals['pattern'] = $annot->getPattern();
  101. }
  102. if (null !== $annot->getRequirements()) {
  103. $globals['requirements'] = $annot->getRequirements();
  104. }
  105. if (null !== $annot->getOptions()) {
  106. $globals['options'] = $annot->getOptions();
  107. }
  108. if (null !== $annot->getDefaults()) {
  109. $globals['defaults'] = $annot->getDefaults();
  110. }
  111. }
  112. $collection = new RouteCollection();
  113. $collection->addResource(new FileResource($class->getFileName()));
  114. foreach ($class->getMethods() as $method) {
  115. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  116. if ($annot instanceof $this->routeAnnotationClass) {
  117. $this->addRoute($collection, $annot, $globals, $class, $method);
  118. }
  119. }
  120. }
  121. return $collection;
  122. }
  123. protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
  124. {
  125. if (null === $annot->getName()) {
  126. $annot->setName($this->getDefaultRouteName($class, $method));
  127. }
  128. $defaults = array_merge($globals['defaults'], $annot->getDefaults());
  129. $requirements = array_merge($globals['requirements'], $annot->getRequirements());
  130. $options = array_merge($globals['options'], $annot->getOptions());
  131. $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
  132. $this->configureRoute($route, $class, $method, $annot);
  133. $collection->add($annot->getName(), $route);
  134. }
  135. /**
  136. * Returns true if this class supports the given resource.
  137. *
  138. * @param mixed $resource A resource
  139. * @param string $type The resource type
  140. *
  141. * @return Boolean True if this class supports the given resource, false otherwise
  142. */
  143. public function supports($resource, $type = null)
  144. {
  145. return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
  146. }
  147. /**
  148. * Sets the loader resolver.
  149. *
  150. * @param LoaderResolver $resolver A LoaderResolver instance
  151. */
  152. public function setResolver(LoaderResolver $resolver)
  153. {
  154. }
  155. /**
  156. * Gets the loader resolver.
  157. *
  158. * @return LoaderResolver A LoaderResolver instance
  159. */
  160. public function getResolver()
  161. {
  162. }
  163. /**
  164. * Gets the default route name for a class method.
  165. *
  166. * @param \ReflectionClass $class
  167. * @param \ReflectionMethod $method
  168. *
  169. * @return string
  170. */
  171. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  172. {
  173. return strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
  174. }
  175. abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
  176. }