Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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 \LogicException('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|null 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, string $type = null)
54
    {
55
        $path = $this->locator->locate($file);
56
 
57
        $collection = new RouteCollection();
58
        if ($class = $this->findClass($path)) {
59
            $refl = new \ReflectionClass($class);
60
            if ($refl->isAbstract()) {
61
                return null;
62
            }
63
 
64
            $collection->addResource(new FileResource($path));
65
            $collection->addCollection($this->loader->load($class, $type));
66
        }
67
 
68
        gc_mem_caches();
69
 
70
        return $collection;
71
    }
72
 
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function supports($resource, string $type = null)
77
    {
78
        return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
79
    }
80
 
81
    /**
82
     * Returns the full class name for the first class in the file.
83
     *
84
     * @return string|false Full class name if found, false otherwise
85
     */
86
    protected function findClass(string $file)
87
    {
88
        $class = false;
89
        $namespace = false;
90
        $tokens = token_get_all(file_get_contents($file));
91
 
92
        if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
93
            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));
94
        }
95
 
96
        $nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true];
97
        if (\defined('T_NAME_QUALIFIED')) {
98
            $nsTokens[T_NAME_QUALIFIED] = true;
99
        }
100
 
101
        for ($i = 0; isset($tokens[$i]); ++$i) {
102
            $token = $tokens[$i];
103
 
104
            if (!isset($token[1])) {
105
                continue;
106
            }
107
 
108
            if (true === $class && \T_STRING === $token[0]) {
109
                return $namespace.'\\'.$token[1];
110
            }
111
 
112
            if (true === $namespace && isset($nsTokens[$token[0]])) {
113
                $namespace = $token[1];
114
                while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) {
115
                    $namespace .= $tokens[$i][1];
116
                }
117
                $token = $tokens[$i];
118
            }
119
 
120
            if (\T_CLASS === $token[0]) {
121
                // Skip usage of ::class constant and anonymous classes
122
                $skipClassToken = false;
123
                for ($j = $i - 1; $j > 0; --$j) {
124
                    if (!isset($tokens[$j][1])) {
125
                        break;
126
                    }
127
 
128
                    if (\T_DOUBLE_COLON === $tokens[$j][0] || \T_NEW === $tokens[$j][0]) {
129
                        $skipClassToken = true;
130
                        break;
131
                    } elseif (!\in_array($tokens[$j][0], [\T_WHITESPACE, \T_DOC_COMMENT, \T_COMMENT])) {
132
                        break;
133
                    }
134
                }
135
 
136
                if (!$skipClassToken) {
137
                    $class = true;
138
                }
139
            }
140
 
141
            if (\T_NAMESPACE === $token[0]) {
142
                $namespace = true;
143
            }
144
        }
145
 
146
        return false;
147
    }
148
}