[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/routing/Loader/ -> AnnotationClassLoader.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1