annotate vendor/symfony/routing/Loader/AnnotationClassLoader.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 1fec387a4317
children 129ea1e6d783
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@0 15 use Symfony\Component\Config\Resource\FileResource;
Chris@0 16 use Symfony\Component\Routing\Route;
Chris@0 17 use Symfony\Component\Routing\RouteCollection;
Chris@0 18 use Symfony\Component\Config\Loader\LoaderInterface;
Chris@0 19 use Symfony\Component\Config\Loader\LoaderResolverInterface;
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@16 123 foreach ($this->reader->getClassAnnotations($class) as $annot) {
Chris@16 124 if ($annot instanceof $this->routeAnnotationClass) {
Chris@16 125 $globals['path'] = '';
Chris@16 126 $globals['name'] = '';
Chris@16 127
Chris@16 128 $this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
Chris@16 129 }
Chris@16 130 }
Chris@14 131 }
Chris@14 132
Chris@0 133 return $collection;
Chris@0 134 }
Chris@0 135
Chris@0 136 protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
Chris@0 137 {
Chris@0 138 $name = $annot->getName();
Chris@0 139 if (null === $name) {
Chris@0 140 $name = $this->getDefaultRouteName($class, $method);
Chris@0 141 }
Chris@14 142 $name = $globals['name'].$name;
Chris@0 143
Chris@0 144 $defaults = array_replace($globals['defaults'], $annot->getDefaults());
Chris@0 145 foreach ($method->getParameters() as $param) {
Chris@0 146 if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
Chris@0 147 $defaults[$param->getName()] = $param->getDefaultValue();
Chris@0 148 }
Chris@0 149 }
Chris@0 150 $requirements = array_replace($globals['requirements'], $annot->getRequirements());
Chris@0 151 $options = array_replace($globals['options'], $annot->getOptions());
Chris@0 152 $schemes = array_merge($globals['schemes'], $annot->getSchemes());
Chris@0 153 $methods = array_merge($globals['methods'], $annot->getMethods());
Chris@0 154
Chris@0 155 $host = $annot->getHost();
Chris@0 156 if (null === $host) {
Chris@0 157 $host = $globals['host'];
Chris@0 158 }
Chris@0 159
Chris@0 160 $condition = $annot->getCondition();
Chris@0 161 if (null === $condition) {
Chris@0 162 $condition = $globals['condition'];
Chris@0 163 }
Chris@0 164
Chris@0 165 $route = $this->createRoute($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
Chris@0 166
Chris@0 167 $this->configureRoute($route, $class, $method, $annot);
Chris@0 168
Chris@0 169 $collection->add($name, $route);
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * {@inheritdoc}
Chris@0 174 */
Chris@0 175 public function supports($resource, $type = null)
Chris@0 176 {
Chris@0 177 return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * {@inheritdoc}
Chris@0 182 */
Chris@0 183 public function setResolver(LoaderResolverInterface $resolver)
Chris@0 184 {
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * {@inheritdoc}
Chris@0 189 */
Chris@0 190 public function getResolver()
Chris@0 191 {
Chris@0 192 }
Chris@0 193
Chris@0 194 /**
Chris@0 195 * Gets the default route name for a class method.
Chris@0 196 *
Chris@0 197 * @param \ReflectionClass $class
Chris@0 198 * @param \ReflectionMethod $method
Chris@0 199 *
Chris@0 200 * @return string
Chris@0 201 */
Chris@0 202 protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
Chris@0 203 {
Chris@0 204 $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name);
Chris@0 205 if ($this->defaultRouteIndex > 0) {
Chris@0 206 $name .= '_'.$this->defaultRouteIndex;
Chris@0 207 }
Chris@0 208 ++$this->defaultRouteIndex;
Chris@0 209
Chris@0 210 return $name;
Chris@0 211 }
Chris@0 212
Chris@0 213 protected function getGlobals(\ReflectionClass $class)
Chris@0 214 {
Chris@0 215 $globals = array(
Chris@0 216 'path' => '',
Chris@0 217 'requirements' => array(),
Chris@0 218 'options' => array(),
Chris@0 219 'defaults' => array(),
Chris@0 220 'schemes' => array(),
Chris@0 221 'methods' => array(),
Chris@0 222 'host' => '',
Chris@0 223 'condition' => '',
Chris@14 224 'name' => '',
Chris@0 225 );
Chris@0 226
Chris@0 227 if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
Chris@14 228 if (null !== $annot->getName()) {
Chris@14 229 $globals['name'] = $annot->getName();
Chris@14 230 }
Chris@14 231
Chris@0 232 if (null !== $annot->getPath()) {
Chris@0 233 $globals['path'] = $annot->getPath();
Chris@0 234 }
Chris@0 235
Chris@0 236 if (null !== $annot->getRequirements()) {
Chris@0 237 $globals['requirements'] = $annot->getRequirements();
Chris@0 238 }
Chris@0 239
Chris@0 240 if (null !== $annot->getOptions()) {
Chris@0 241 $globals['options'] = $annot->getOptions();
Chris@0 242 }
Chris@0 243
Chris@0 244 if (null !== $annot->getDefaults()) {
Chris@0 245 $globals['defaults'] = $annot->getDefaults();
Chris@0 246 }
Chris@0 247
Chris@0 248 if (null !== $annot->getSchemes()) {
Chris@0 249 $globals['schemes'] = $annot->getSchemes();
Chris@0 250 }
Chris@0 251
Chris@0 252 if (null !== $annot->getMethods()) {
Chris@0 253 $globals['methods'] = $annot->getMethods();
Chris@0 254 }
Chris@0 255
Chris@0 256 if (null !== $annot->getHost()) {
Chris@0 257 $globals['host'] = $annot->getHost();
Chris@0 258 }
Chris@0 259
Chris@0 260 if (null !== $annot->getCondition()) {
Chris@0 261 $globals['condition'] = $annot->getCondition();
Chris@0 262 }
Chris@0 263 }
Chris@0 264
Chris@0 265 return $globals;
Chris@0 266 }
Chris@0 267
Chris@0 268 protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
Chris@0 269 {
Chris@0 270 return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
Chris@0 271 }
Chris@0 272
Chris@0 273 abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
Chris@0 274 }