|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Attributes; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineReader; |
|
15
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
|
|
|
|
|
17
|
|
|
use Spiral\Attributes\Exception\InitializationException; |
|
18
|
|
|
use Spiral\Attributes\Reader as BaseReader; |
|
19
|
|
|
|
|
20
|
|
|
final class AnnotationReader extends BaseReader |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Reader|null |
|
24
|
|
|
*/ |
|
25
|
|
|
private $reader; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Reader|null $reader |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Reader $reader = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->checkAvailability(); |
|
33
|
|
|
|
|
34
|
|
|
$this->reader = $reader ?? new DoctrineReader(); |
|
35
|
|
|
|
|
36
|
|
|
// doctrine/annotations ^1.0 compatibility. |
|
37
|
|
|
if (\method_exists(AnnotationRegistry::class, 'registerLoader')) { |
|
38
|
|
|
AnnotationRegistry::registerLoader('\\class_exists'); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
private function checkAvailability(): void |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->isAvailable()) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new InitializationException('Requires the "doctrine/annotations" package'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function isAvailable(): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return \interface_exists(Reader::class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritDoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getClassMetadata(\ReflectionClass $class, string $name = null): iterable |
|
66
|
|
|
{ |
|
67
|
|
|
$result = $this->reader->getClassAnnotations($class); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return $this->filter($name, $result); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string|null $name |
|
74
|
|
|
* @param iterable|object[] $annotations |
|
75
|
|
|
* @return object[] |
|
76
|
|
|
*/ |
|
77
|
|
|
private function filter(?string $name, iterable $annotations): iterable |
|
78
|
|
|
{ |
|
79
|
|
|
if ($name === null) { |
|
80
|
|
|
yield from $annotations; |
|
81
|
|
|
|
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
foreach ($annotations as $annotation) { |
|
86
|
|
|
if ($annotation instanceof $name) { |
|
87
|
|
|
yield $annotation; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getFunctionMetadata(\ReflectionFunctionAbstract $function, string $name = null): iterable |
|
96
|
|
|
{ |
|
97
|
|
|
if ($function instanceof \ReflectionMethod) { |
|
98
|
|
|
$result = $this->reader->getMethodAnnotations($function); |
|
99
|
|
|
|
|
100
|
|
|
return $this->filter($name, $result); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return []; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getPropertyMetadata(\ReflectionProperty $property, string $name = null): iterable |
|
110
|
|
|
{ |
|
111
|
|
|
$result = $this->reader->getPropertyAnnotations($property); |
|
112
|
|
|
|
|
113
|
|
|
return $this->filter($name, $result); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritDoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getConstantMetadata(\ReflectionClassConstant $constant, string $name = null): iterable |
|
120
|
|
|
{ |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritDoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getParameterMetadata(\ReflectionParameter $parameter, string $name = null): iterable |
|
128
|
|
|
{ |
|
129
|
|
|
return []; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: