annotate vendor/symfony/routing/Loader/AnnotationClassLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Routing\Loader;
Chris@0 13
Chris@0 14 use Doctrine\Common\Annotations\Reader;
Chris@17 15 use Symfony\Component\Config\Loader\LoaderInterface;
Chris@17 16 use Symfony\Component\Config\Loader\LoaderResolverInterface;
Chris@0 17 use Symfony\Component\Config\Resource\FileResource;
Chris@0 18 use Symfony\Component\Routing\Route;
Chris@0 19 use Symfony\Component\Routing\RouteCollection;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * AnnotationClassLoader loads routing information from a PHP class and its methods.
Chris@0 23 *
Chris@0 24 * You need to define an implementation for the getRouteDefaults() method. Most of the
Chris@0 25 * time, this method should define some PHP callable to be called for the route
Chris@0 26 * (a controller in MVC speak).
Chris@0 27 *
Chris@0 28 * The @Route annotation can be set on the class (for global parameters),
Chris@0 29 * and on each method.
Chris@0 30 *
Chris@0 31 * The @Route annotation main value is the route path. The annotation also
Chris@0 32 * recognizes several parameters: requirements, options, defaults, schemes,
Chris@0 33 * methods, host, and name. The name parameter is mandatory.
Chris@0 34 * Here is an example of how you should be able to use it:
Chris@0 35 *
Chris@0 36 * /**
Chris@0 37 * * @Route("/Blog")
Chris@0 38 * * /
Chris@0 39 * class Blog
Chris@0 40 * {
Chris@0 41 * /**
Chris@0 42 * * @Route("/", name="blog_index")
Chris@0 43 * * /
Chris@0 44 * public function index()
Chris@0 45 * {
Chris@0 46 * }
Chris@0 47 *
Chris@0 48 * /**
Chris@0 49 * * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
Chris@0 50 * * /
Chris@0 51 * public function show()
Chris@0 52 * {
Chris@0 53 * }
Chris@0 54 * }
Chris@0 55 *
Chris@0 56 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 57 */
Chris@0 58 abstract class AnnotationClassLoader implements LoaderInterface
Chris@0 59 {
Chris@0 60 protected $reader;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * @var string
Chris@0 64 */
Chris@0 65 protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
Chris@0 66
Chris@0 67 /**
Chris@0 68 * @var int
Chris@0 69 */
Chris@0 70 protected $defaultRouteIndex = 0;
Chris@0 71
Chris@0 72 public function __construct(Reader $reader)
Chris@0 73 {
Chris@0 74 $this->reader = $reader;
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Sets the annotation class to read route properties from.
Chris@0 79 *
Chris@0 80 * @param string $class A fully-qualified class name
Chris@0 81 */
Chris@0 82 public function setRouteAnnotationClass($class)
Chris@0 83 {
Chris@0 84 $this->routeAnnotationClass = $class;
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * Loads from annotations from a class.
Chris@0 89 *
Chris@0 90 * @param string $class A class name
Chris@0 91 * @param string|null $type The resource type
Chris@0 92 *
Chris@0 93 * @return RouteCollection A RouteCollection instance
Chris@0 94 *
Chris@0 95 * @throws \InvalidArgumentException When route can't be parsed
Chris@0 96 */
Chris@0 97 public function load($class, $type = null)
Chris@0 98 {
Chris@0 99 if (!class_exists($class)) {
Chris@0 100 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
Chris@0 101 }
Chris@0 102
Chris@0 103 $class = new \ReflectionClass($class);
Chris@0 104 if ($class->isAbstract()) {
Chris@0 105 throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
Chris@0 106 }
Chris@0 107
Chris@0 108 $globals = $this->getGlobals($class);
Chris@0 109
Chris@0 110 $collection = new RouteCollection();
Chris@0 111 $collection->addResource(new FileResource($class->getFileName()));
Chris@0 112
Chris@0 113 foreach ($class->getMethods() as $method) {
Chris@0 114 $this->defaultRouteIndex = 0;
Chris@0 115 foreach ($this->reader->getMethodAnnotations($method) as $annot) {
Chris@0 116 if ($annot instanceof $this->routeAnnotationClass) {
Chris@0 117 $this->addRoute($collection, $annot, $globals, $class, $method);
Chris@0 118 }
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@16 122 if (0 === $collection->count() && $class->hasMethod('__invoke')) {
Chris@17 123 $globals = $this->resetGlobals();
Chris@16 124 foreach ($this->reader->getClassAnnotations($class) as $annot) {
Chris@16 125 if ($annot instanceof $this->routeAnnotationClass) {
Chris@16 126 $this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
Chris@16 127 }
Chris@16 128 }
Chris@14 129 }
Chris@14 130
Chris@0 131 return $collection;
Chris@0 132 }
Chris@0 133
Chris@0 134 protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
Chris@0 135 {
Chris@0 136 $name = $annot->getName();
Chris@0 137 if (null === $name) {
Chris@0 138 $name = $this->getDefaultRouteName($class, $method);
Chris@0 139 }
Chris@14 140 $name = $globals['name'].$name;
Chris@0 141
Chris@0 142 $defaults = array_replace($globals['defaults'], $annot->getDefaults());
Chris@0 143 foreach ($method->getParameters() as $param) {
Chris@0 144 if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
Chris@0 145 $defaults[$param->getName()] = $param->getDefaultValue();
Chris@0 146 }
Chris@0 147 }
Chris@0 148 $requirements = array_replace($globals['requirements'], $annot->getRequirements());
Chris@0 149 $options = array_replace($globals['options'], $annot->getOptions());
Chris@0 150 $schemes = array_merge($globals['schemes'], $annot->getSchemes());
Chris@0 151 $methods = array_merge($globals['methods'], $annot->getMethods());
Chris@0 152
Chris@0 153 $host = $annot->getHost();
Chris@0 154 if (null === $host) {
Chris@0 155 $host = $globals['host'];
Chris@0 156 }
Chris@0 157
Chris@0 158 $condition = $annot->getCondition();
Chris@0 159 if (null === $condition) {
Chris@0 160 $condition = $globals['condition'];
Chris@0 161 }
Chris@0 162
Chris@0 163 $route = $this->createRoute($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
Chris@0 164
Chris@0 165 $this->configureRoute($route, $class, $method, $annot);
Chris@0 166
Chris@0 167 $collection->add($name, $route);
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * {@inheritdoc}
Chris@0 172 */
Chris@0 173 public function supports($resource, $type = null)
Chris@0 174 {
Chris@17 175 return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * {@inheritdoc}
Chris@0 180 */
Chris@0 181 public function setResolver(LoaderResolverInterface $resolver)
Chris@0 182 {
Chris@0 183 }
Chris@0 184
Chris@0 185 /**
Chris@0 186 * {@inheritdoc}
Chris@0 187 */
Chris@0 188 public function getResolver()
Chris@0 189 {
Chris@0 190 }
Chris@0 191
Chris@0 192 /**
Chris@0 193 * Gets the default route name for a class method.
Chris@0 194 *
Chris@0 195 * @param \ReflectionClass $class
Chris@0 196 * @param \ReflectionMethod $method
Chris@0 197 *
Chris@0 198 * @return string
Chris@0 199 */
Chris@0 200 protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
Chris@0 201 {
Chris@0 202 $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name);
Chris@0 203 if ($this->defaultRouteIndex > 0) {
Chris@0 204 $name .= '_'.$this->defaultRouteIndex;
Chris@0 205 }
Chris@0 206 ++$this->defaultRouteIndex;
Chris@0 207
Chris@0 208 return $name;
Chris@0 209 }
Chris@0 210
Chris@0 211 protected function getGlobals(\ReflectionClass $class)
Chris@0 212 {
Chris@17 213 $globals = $this->resetGlobals();
Chris@0 214
Chris@0 215 if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
Chris@14 216 if (null !== $annot->getName()) {
Chris@14 217 $globals['name'] = $annot->getName();
Chris@14 218 }
Chris@14 219
Chris@0 220 if (null !== $annot->getPath()) {
Chris@0 221 $globals['path'] = $annot->getPath();
Chris@0 222 }
Chris@0 223
Chris@0 224 if (null !== $annot->getRequirements()) {
Chris@0 225 $globals['requirements'] = $annot->getRequirements();
Chris@0 226 }
Chris@0 227
Chris@0 228 if (null !== $annot->getOptions()) {
Chris@0 229 $globals['options'] = $annot->getOptions();
Chris@0 230 }
Chris@0 231
Chris@0 232 if (null !== $annot->getDefaults()) {
Chris@0 233 $globals['defaults'] = $annot->getDefaults();
Chris@0 234 }
Chris@0 235
Chris@0 236 if (null !== $annot->getSchemes()) {
Chris@0 237 $globals['schemes'] = $annot->getSchemes();
Chris@0 238 }
Chris@0 239
Chris@0 240 if (null !== $annot->getMethods()) {
Chris@0 241 $globals['methods'] = $annot->getMethods();
Chris@0 242 }
Chris@0 243
Chris@0 244 if (null !== $annot->getHost()) {
Chris@0 245 $globals['host'] = $annot->getHost();
Chris@0 246 }
Chris@0 247
Chris@0 248 if (null !== $annot->getCondition()) {
Chris@0 249 $globals['condition'] = $annot->getCondition();
Chris@0 250 }
Chris@0 251 }
Chris@0 252
Chris@0 253 return $globals;
Chris@0 254 }
Chris@0 255
Chris@17 256 private function resetGlobals()
Chris@17 257 {
Chris@17 258 return [
Chris@17 259 'path' => '',
Chris@17 260 'requirements' => [],
Chris@17 261 'options' => [],
Chris@17 262 'defaults' => [],
Chris@17 263 'schemes' => [],
Chris@17 264 'methods' => [],
Chris@17 265 'host' => '',
Chris@17 266 'condition' => '',
Chris@17 267 'name' => '',
Chris@17 268 ];
Chris@17 269 }
Chris@17 270
Chris@0 271 protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
Chris@0 272 {
Chris@0 273 return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
Chris@0 274 }
Chris@0 275
Chris@0 276 abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
Chris@0 277 }