[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/routing/Loader/ -> AnnotationFileLoader.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 Symfony\Component\Config\FileLocatorInterface;
  15  use Symfony\Component\Config\Loader\FileLoader;
  16  use Symfony\Component\Config\Resource\FileResource;
  17  use Symfony\Component\Routing\RouteCollection;
  18  
  19  /**
  20   * AnnotationFileLoader loads routing information from annotations set
  21   * on a PHP class and its methods.
  22   *
  23   * @author Fabien Potencier <fabien@symfony.com>
  24   */
  25  class AnnotationFileLoader extends FileLoader
  26  {
  27      protected $loader;
  28  
  29      /**
  30       * @throws \RuntimeException
  31       */
  32      public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
  33      {
  34          if (!\function_exists('token_get_all')) {
  35              throw new \RuntimeException('The Tokenizer extension is required for the routing annotation loaders.');
  36          }
  37  
  38          parent::__construct($locator);
  39  
  40          $this->loader = $loader;
  41      }
  42  
  43      /**
  44       * Loads from annotations from a file.
  45       *
  46       * @param string      $file A PHP file path
  47       * @param string|null $type The resource type
  48       *
  49       * @return RouteCollection A RouteCollection instance
  50       *
  51       * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
  52       */
  53      public function load($file, $type = null)
  54      {
  55          $path = $this->locator->locate($file);
  56  
  57          $collection = new RouteCollection();
  58          if ($class = $this->findClass($path)) {
  59              $collection->addResource(new FileResource($path));
  60              $collection->addCollection($this->loader->load($class, $type));
  61          }
  62          if (\PHP_VERSION_ID >= 70000) {
  63              // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
  64              gc_mem_caches();
  65          }
  66  
  67          return $collection;
  68      }
  69  
  70      /**
  71       * {@inheritdoc}
  72       */
  73      public function supports($resource, $type = null)
  74      {
  75          return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
  76      }
  77  
  78      /**
  79       * Returns the full class name for the first class in the file.
  80       *
  81       * @param string $file A PHP file path
  82       *
  83       * @return string|false Full class name if found, false otherwise
  84       */
  85      protected function findClass($file)
  86      {
  87          $class = false;
  88          $namespace = false;
  89          $tokens = token_get_all(file_get_contents($file));
  90          for ($i = 0; isset($tokens[$i]); ++$i) {
  91              $token = $tokens[$i];
  92  
  93              if (!isset($token[1])) {
  94                  continue;
  95              }
  96  
  97              if (true === $class && T_STRING === $token[0]) {
  98                  return $namespace.'\\'.$token[1];
  99              }
 100  
 101              if (true === $namespace && T_STRING === $token[0]) {
 102                  $namespace = $token[1];
 103                  while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], array(T_NS_SEPARATOR, T_STRING))) {
 104                      $namespace .= $tokens[$i][1];
 105                  }
 106                  $token = $tokens[$i];
 107              }
 108  
 109              if (T_CLASS === $token[0]) {
 110                  // Skip usage of ::class constant and anonymous classes
 111                  $skipClassToken = false;
 112                  for ($j = $i - 1; $j > 0; --$j) {
 113                      if (!isset($tokens[$j][1])) {
 114                          break;
 115                      }
 116  
 117                      if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) {
 118                          $skipClassToken = true;
 119                          break;
 120                      } elseif (!\in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
 121                          break;
 122                      }
 123                  }
 124  
 125                  if (!$skipClassToken) {
 126                      $class = true;
 127                  }
 128              }
 129  
 130              if (T_NAMESPACE === $token[0]) {
 131                  $namespace = true;
 132              }
 133          }
 134  
 135          return false;
 136      }
 137  }


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