AnnotationClassLoader.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Config\Resource\FileResource;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. use Symfony\Component\Config\Loader\LoaderInterface;
  16. use Symfony\Component\Config\Loader\LoaderResolverInterface;
  17. /**
  18. * AnnotationClassLoader loads routing information from a PHP class and its methods.
  19. *
  20. * You need to define an implementation for the getRouteDefaults() method. Most of the
  21. * time, this method should define some PHP callable to be called for the route
  22. * (a controller in MVC speak).
  23. *
  24. * The @Route annotation can be set on the class (for global parameters),
  25. * and on each method.
  26. *
  27. * The @Route annotation main value is the route path. The annotation also
  28. * recognizes several parameters: requirements, options, defaults, schemes,
  29. * methods, host, and name. The name parameter is mandatory.
  30. * 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. /**
  57. * @var Reader
  58. */
  59. protected $reader;
  60. /**
  61. * @var string
  62. */
  63. protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
  64. /**
  65. * @var integer
  66. */
  67. protected $defaultRouteIndex = 0;
  68. /**
  69. * Constructor.
  70. *
  71. * @param Reader $reader
  72. */
  73. public function __construct(Reader $reader)
  74. {
  75. $this->reader = $reader;
  76. }
  77. /**
  78. * Sets the annotation class to read route properties from.
  79. *
  80. * @param string $class A fully-qualified class name
  81. */
  82. public function setRouteAnnotationClass($class)
  83. {
  84. $this->routeAnnotationClass = $class;
  85. }
  86. /**
  87. * Loads from annotations from a class.
  88. *
  89. * @param string $class A class name
  90. * @param string|null $type The resource type
  91. *
  92. * @return RouteCollection A RouteCollection instance
  93. *
  94. * @throws \InvalidArgumentException When route can't be parsed
  95. */
  96. public function load($class, $type = null)
  97. {
  98. if (!class_exists($class)) {
  99. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  100. }
  101. $globals = array(
  102. 'path' => '',
  103. 'requirements' => array(),
  104. 'options' => array(),
  105. 'defaults' => array(),
  106. 'schemes' => array(),
  107. 'methods' => array(),
  108. 'host' => '',
  109. );
  110. $class = new \ReflectionClass($class);
  111. if ($class->isAbstract()) {
  112. throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class));
  113. }
  114. if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
  115. // for BC reasons
  116. if (null !== $annot->getPath()) {
  117. $globals['path'] = $annot->getPath();
  118. } elseif (null !== $annot->getPattern()) {
  119. $globals['path'] = $annot->getPattern();
  120. }
  121. if (null !== $annot->getRequirements()) {
  122. $globals['requirements'] = $annot->getRequirements();
  123. }
  124. if (null !== $annot->getOptions()) {
  125. $globals['options'] = $annot->getOptions();
  126. }
  127. if (null !== $annot->getDefaults()) {
  128. $globals['defaults'] = $annot->getDefaults();
  129. }
  130. if (null !== $annot->getSchemes()) {
  131. $globals['schemes'] = $annot->getSchemes();
  132. }
  133. if (null !== $annot->getMethods()) {
  134. $globals['methods'] = $annot->getMethods();
  135. }
  136. if (null !== $annot->getHost()) {
  137. $globals['host'] = $annot->getHost();
  138. }
  139. }
  140. $collection = new RouteCollection();
  141. $collection->addResource(new FileResource($class->getFileName()));
  142. foreach ($class->getMethods() as $method) {
  143. $this->defaultRouteIndex = 0;
  144. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  145. if ($annot instanceof $this->routeAnnotationClass) {
  146. $this->addRoute($collection, $annot, $globals, $class, $method);
  147. }
  148. }
  149. }
  150. return $collection;
  151. }
  152. protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
  153. {
  154. $name = $annot->getName();
  155. if (null === $name) {
  156. $name = $this->getDefaultRouteName($class, $method);
  157. }
  158. $defaults = array_replace($globals['defaults'], $annot->getDefaults());
  159. foreach ($method->getParameters() as $param) {
  160. if (!isset($defaults[$param->getName()]) && $param->isOptional()) {
  161. $defaults[$param->getName()] = $param->getDefaultValue();
  162. }
  163. }
  164. $requirements = array_replace($globals['requirements'], $annot->getRequirements());
  165. $options = array_replace($globals['options'], $annot->getOptions());
  166. $schemes = array_replace($globals['schemes'], $annot->getSchemes());
  167. $methods = array_replace($globals['methods'], $annot->getMethods());
  168. $host = $annot->getHost();
  169. if (null === $host) {
  170. $host = $globals['host'];
  171. }
  172. $route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods);
  173. $this->configureRoute($route, $class, $method, $annot);
  174. $collection->add($name, $route);
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function supports($resource, $type = null)
  180. {
  181. return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function setResolver(LoaderResolverInterface $resolver)
  187. {
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function getResolver()
  193. {
  194. }
  195. /**
  196. * Gets the default route name for a class method.
  197. *
  198. * @param \ReflectionClass $class
  199. * @param \ReflectionMethod $method
  200. *
  201. * @return string
  202. */
  203. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  204. {
  205. $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name);
  206. if ($this->defaultRouteIndex > 0) {
  207. $name .= '_'.$this->defaultRouteIndex;
  208. }
  209. $this->defaultRouteIndex++;
  210. return $name;
  211. }
  212. abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
  213. }