Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

AnnotationClassLoader.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 8.00 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\Routing\Loader;
013   
014  use Doctrine\Common\Annotations\Reader;
015  use Symfony\Component\Config\Resource\FileResource;
016  use Symfony\Component\Routing\Route;
017  use Symfony\Component\Routing\RouteCollection;
018  use Symfony\Component\Config\Loader\LoaderInterface;
019  use Symfony\Component\Config\Loader\LoaderResolverInterface;
020   
021  /**
022   * AnnotationClassLoader loads routing information from a PHP class and its methods.
023   *
024   * You need to define an implementation for the getRouteDefaults() method. Most of the
025   * time, this method should define some PHP callable to be called for the route
026   * (a controller in MVC speak).
027   *
028   * The @Route annotation can be set on the class (for global parameters),
029   * and on each method.
030   *
031   * The @Route annotation main value is the route path. The annotation also
032   * recognizes several parameters: requirements, options, defaults, schemes,
033   * methods, host, and name. The name parameter is mandatory.
034   * Here is an example of how you should be able to use it:
035   *
036   *     /**
037   *      * @Route("/Blog")
038   *      * /
039   *     class Blog
040   *     {
041   *         /**
042   *          * @Route("/", name="blog_index")
043   *          * /
044   *         public function index()
045   *         {
046   *         }
047   *
048   *         /**
049   *          * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
050   *          * /
051   *         public function show()
052   *         {
053   *         }
054   *     }
055   *
056   * @author Fabien Potencier <fabien@symfony.com>
057   */
058  abstract class AnnotationClassLoader implements LoaderInterface
059  {
060      /**
061       * @var Reader
062       */
063      protected $reader;
064   
065      /**
066       * @var string
067       */
068      protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
069   
070      /**
071       * @var int
072       */
073      protected $defaultRouteIndex = 0;
074   
075      /**
076       * Constructor.
077       *
078       * @param Reader $reader
079       */
080      public function __construct(Reader $reader)
081      {
082          $this->reader = $reader;
083      }
084   
085      /**
086       * Sets the annotation class to read route properties from.
087       *
088       * @param string $class A fully-qualified class name
089       */
090      public function setRouteAnnotationClass($class)
091      {
092          $this->routeAnnotationClass = $class;
093      }
094   
095      /**
096       * Loads from annotations from a class.
097       *
098       * @param string      $class A class name
099       * @param string|null $type  The resource type
100       *
101       * @return RouteCollection A RouteCollection instance
102       *
103       * @throws \InvalidArgumentException When route can't be parsed
104       */
105      public function load($class, $type = null)
106      {
107          if (!class_exists($class)) {
108              throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
109          }
110   
111          $class = new \ReflectionClass($class);
112          if ($class->isAbstract()) {
113              throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
114          }
115   
116          $globals = $this->getGlobals($class);
117   
118          $collection = new RouteCollection();
119          $collection->addResource(new FileResource($class->getFileName()));
120   
121          foreach ($class->getMethods() as $method) {
122              $this->defaultRouteIndex = 0;
123              foreach ($this->reader->getMethodAnnotations($method) as $annot) {
124                  if ($annot instanceof $this->routeAnnotationClass) {
125                      $this->addRoute($collection, $annot, $globals, $class, $method);
126                  }
127              }
128          }
129   
130          return $collection;
131      }
132   
133      protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
134      {
135          $name = $annot->getName();
136          if (null === $name) {
137              $name = $this->getDefaultRouteName($class, $method);
138          }
139   
140          $defaults = array_replace($globals['defaults'], $annot->getDefaults());
141          foreach ($method->getParameters() as $param) {
142              if (!isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
143                  $defaults[$param->getName()] = $param->getDefaultValue();
144              }
145          }
146          $requirements = array_replace($globals['requirements'], $annot->getRequirements());
147          $options = array_replace($globals['options'], $annot->getOptions());
148          $schemes = array_merge($globals['schemes'], $annot->getSchemes());
149          $methods = array_merge($globals['methods'], $annot->getMethods());
150   
151          $host = $annot->getHost();
152          if (null === $host) {
153              $host = $globals['host'];
154          }
155   
156          $condition = $annot->getCondition();
157          if (null === $condition) {
158              $condition = $globals['condition'];
159          }
160   
161          $route = $this->createRoute($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
162   
163          $this->configureRoute($route, $class, $method, $annot);
164   
165          $collection->add($name, $route);
166      }
167   
168      /**
169       * {@inheritdoc}
170       */
171      public function supports($resource, $type = null)
172      {
173          return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
174      }
175   
176      /**
177       * {@inheritdoc}
178       */
179      public function setResolver(LoaderResolverInterface $resolver)
180      {
181      }
182   
183      /**
184       * {@inheritdoc}
185       */
186      public function getResolver()
187      {
188      }
189   
190      /**
191       * Gets the default route name for a class method.
192       *
193       * @param \ReflectionClass  $class
194       * @param \ReflectionMethod $method
195       *
196       * @return string
197       */
198      protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
199      {
200          $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name);
201          if ($this->defaultRouteIndex > 0) {
202              $name .= '_'.$this->defaultRouteIndex;
203          }
204          ++$this->defaultRouteIndex;
205   
206          return $name;
207      }
208   
209      protected function getGlobals(\ReflectionClass $class)
210      {
211          $globals = array(
212              'path' => '',
213              'requirements' => array(),
214              'options' => array(),
215              'defaults' => array(),
216              'schemes' => array(),
217              'methods' => array(),
218              'host' => '',
219              'condition' => '',
220          );
221   
222          if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
223              // for BC reasons
224              if (null !== $annot->getPath()) {
225                  $globals['path'] = $annot->getPath();
226              } elseif (null !== $annot->getPattern()) {
227                  $globals['path'] = $annot->getPattern();
228              }
229   
230              if (null !== $annot->getRequirements()) {
231                  $globals['requirements'] = $annot->getRequirements();
232              }
233   
234              if (null !== $annot->getOptions()) {
235                  $globals['options'] = $annot->getOptions();
236              }
237   
238              if (null !== $annot->getDefaults()) {
239                  $globals['defaults'] = $annot->getDefaults();
240              }
241   
242              if (null !== $annot->getSchemes()) {
243                  $globals['schemes'] = $annot->getSchemes();
244              }
245   
246              if (null !== $annot->getMethods()) {
247                  $globals['methods'] = $annot->getMethods();
248              }
249   
250              if (null !== $annot->getHost()) {
251                  $globals['host'] = $annot->getHost();
252              }
253   
254              if (null !== $annot->getCondition()) {
255                  $globals['condition'] = $annot->getCondition();
256              }
257          }
258   
259          return $globals;
260      }
261   
262      protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
263      {
264          return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
265      }
266   
267      abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
268  }
269