123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace Symfony\Component\Routing\Loader;
- use Symfony\Component\Config\FileLocatorInterface;
- use Symfony\Component\Config\Loader\FileLoader;
- use Symfony\Component\Config\Resource\FileResource;
- use Symfony\Component\Routing\RouteCollection;
- class AnnotationFileLoader extends FileLoader
- {
- protected $loader;
-
- public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
- {
- if (!\function_exists('token_get_all')) {
- throw new \LogicException('The Tokenizer extension is required for the routing annotation loaders.');
- }
- parent::__construct($locator);
- $this->loader = $loader;
- }
-
- public function load($file, $type = null)
- {
- $path = $this->locator->locate($file);
- $collection = new RouteCollection();
- if ($class = $this->findClass($path)) {
- $collection->addResource(new FileResource($path));
- $collection->addCollection($this->loader->load($class, $type));
- }
-
- gc_mem_caches();
- return $collection;
- }
-
- public function supports($resource, $type = null)
- {
- return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
- }
-
- protected function findClass($file)
- {
- $class = false;
- $namespace = false;
- $tokens = token_get_all(file_get_contents($file));
- if (1 === \count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
- throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file));
- }
- for ($i = 0; isset($tokens[$i]); ++$i) {
- $token = $tokens[$i];
- if (!isset($token[1])) {
- continue;
- }
- if (true === $class && T_STRING === $token[0]) {
- return $namespace.'\\'.$token[1];
- }
- if (true === $namespace && T_STRING === $token[0]) {
- $namespace = $token[1];
- while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], [T_NS_SEPARATOR, T_STRING])) {
- $namespace .= $tokens[$i][1];
- }
- $token = $tokens[$i];
- }
- if (T_CLASS === $token[0]) {
-
- $skipClassToken = false;
- for ($j = $i - 1; $j > 0; --$j) {
- if (!isset($tokens[$j][1])) {
- break;
- }
- if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) {
- $skipClassToken = true;
- break;
- } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT])) {
- break;
- }
- }
- if (!$skipClassToken) {
- $class = true;
- }
- }
- if (T_NAMESPACE === $token[0]) {
- $namespace = true;
- }
- }
- return false;
- }
- }
|