/vendor/symfony/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

https://github.com/flubbers/cloudFutbol · PHP · 214 lines · 97 code · 27 blank · 90 comment · 13 complexity · ed82423ae96cae4ce6b844c81cb56556 MD5 · raw file

  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 ($class->isAbstract()) {
  100. throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class));
  101. }
  102. if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
  103. if (null !== $annot->getPattern()) {
  104. $globals['pattern'] = $annot->getPattern();
  105. }
  106. if (null !== $annot->getRequirements()) {
  107. $globals['requirements'] = $annot->getRequirements();
  108. }
  109. if (null !== $annot->getOptions()) {
  110. $globals['options'] = $annot->getOptions();
  111. }
  112. if (null !== $annot->getDefaults()) {
  113. $globals['defaults'] = $annot->getDefaults();
  114. }
  115. }
  116. $collection = new RouteCollection();
  117. $collection->addResource(new FileResource($class->getFileName()));
  118. foreach ($class->getMethods() as $method) {
  119. $this->defaultRouteIndex = 0;
  120. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  121. if ($annot instanceof $this->routeAnnotationClass) {
  122. $this->addRoute($collection, $annot, $globals, $class, $method);
  123. }
  124. }
  125. }
  126. return $collection;
  127. }
  128. protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
  129. {
  130. $name = $annot->getName();
  131. if (null === $name) {
  132. $name = $this->getDefaultRouteName($class, $method);
  133. }
  134. $defaults = array_merge($globals['defaults'], $annot->getDefaults());
  135. $requirements = array_merge($globals['requirements'], $annot->getRequirements());
  136. $options = array_merge($globals['options'], $annot->getOptions());
  137. $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
  138. $this->configureRoute($route, $class, $method, $annot);
  139. $collection->add($name, $route);
  140. }
  141. /**
  142. * Returns true if this class supports the given resource.
  143. *
  144. * @param mixed $resource A resource
  145. * @param string $type The resource type
  146. *
  147. * @return Boolean True if this class supports the given resource, false otherwise
  148. */
  149. public function supports($resource, $type = null)
  150. {
  151. return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
  152. }
  153. /**
  154. * Sets the loader resolver.
  155. *
  156. * @param LoaderResolver $resolver A LoaderResolver instance
  157. */
  158. public function setResolver(LoaderResolver $resolver)
  159. {
  160. }
  161. /**
  162. * Gets the loader resolver.
  163. *
  164. * @return LoaderResolver A LoaderResolver instance
  165. */
  166. public function getResolver()
  167. {
  168. }
  169. /**
  170. * Gets the default route name for a class method.
  171. *
  172. * @param \ReflectionClass $class
  173. * @param \ReflectionMethod $method
  174. *
  175. * @return string
  176. */
  177. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  178. {
  179. $name = strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
  180. if ($this->defaultRouteIndex > 0) {
  181. $name .= '_'.$this->defaultRouteIndex;
  182. }
  183. $this->defaultRouteIndex++;
  184. return $name;
  185. }
  186. abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
  187. }