| 148 |
lars |
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 |
public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
|
|
|
30 |
{
|
|
|
31 |
if (!\function_exists('token_get_all')) {
|
|
|
32 |
throw new \LogicException('The Tokenizer extension is required for the routing annotation loaders.');
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
parent::__construct($locator);
|
|
|
36 |
|
|
|
37 |
$this->loader = $loader;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Loads from annotations from a file.
|
|
|
42 |
*
|
|
|
43 |
* @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
|
|
|
44 |
*/
|
|
|
45 |
public function load(mixed $file, string $type = null): ?RouteCollection
|
|
|
46 |
{
|
|
|
47 |
$path = $this->locator->locate($file);
|
|
|
48 |
|
|
|
49 |
$collection = new RouteCollection();
|
|
|
50 |
if ($class = $this->findClass($path)) {
|
|
|
51 |
$refl = new \ReflectionClass($class);
|
|
|
52 |
if ($refl->isAbstract()) {
|
|
|
53 |
return null;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
$collection->addResource(new FileResource($path));
|
|
|
57 |
$collection->addCollection($this->loader->load($class, $type));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
gc_mem_caches();
|
|
|
61 |
|
|
|
62 |
return $collection;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public function supports(mixed $resource, string $type = null): bool
|
|
|
66 |
{
|
|
|
67 |
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Returns the full class name for the first class in the file.
|
|
|
72 |
*/
|
|
|
73 |
protected function findClass(string $file): string|false
|
|
|
74 |
{
|
|
|
75 |
$class = false;
|
|
|
76 |
$namespace = false;
|
|
|
77 |
$tokens = token_get_all(file_get_contents($file));
|
|
|
78 |
|
|
|
79 |
if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
|
|
|
80 |
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));
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true];
|
|
|
84 |
if (\defined('T_NAME_QUALIFIED')) {
|
|
|
85 |
$nsTokens[\T_NAME_QUALIFIED] = true;
|
|
|
86 |
}
|
|
|
87 |
for ($i = 0; isset($tokens[$i]); ++$i) {
|
|
|
88 |
$token = $tokens[$i];
|
|
|
89 |
if (!isset($token[1])) {
|
|
|
90 |
continue;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
if (true === $class && \T_STRING === $token[0]) {
|
|
|
94 |
return $namespace.'\\'.$token[1];
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (true === $namespace && isset($nsTokens[$token[0]])) {
|
|
|
98 |
$namespace = $token[1];
|
|
|
99 |
while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) {
|
|
|
100 |
$namespace .= $tokens[$i][1];
|
|
|
101 |
}
|
|
|
102 |
$token = $tokens[$i];
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
if (\T_CLASS === $token[0]) {
|
|
|
106 |
// Skip usage of ::class constant and anonymous classes
|
|
|
107 |
$skipClassToken = false;
|
|
|
108 |
for ($j = $i - 1; $j > 0; --$j) {
|
|
|
109 |
if (!isset($tokens[$j][1])) {
|
|
|
110 |
if ('(' === $tokens[$j] || ',' === $tokens[$j]) {
|
|
|
111 |
$skipClassToken = true;
|
|
|
112 |
}
|
|
|
113 |
break;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if (\T_DOUBLE_COLON === $tokens[$j][0] || \T_NEW === $tokens[$j][0]) {
|
|
|
117 |
$skipClassToken = true;
|
|
|
118 |
break;
|
|
|
119 |
} elseif (!\in_array($tokens[$j][0], [\T_WHITESPACE, \T_DOC_COMMENT, \T_COMMENT])) {
|
|
|
120 |
break;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if (!$skipClassToken) {
|
|
|
125 |
$class = true;
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if (\T_NAMESPACE === $token[0]) {
|
|
|
130 |
$namespace = true;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return false;
|
|
|
135 |
}
|
|
|
136 |
}
|