1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Annotations; |
13
|
|
|
|
14
|
|
|
use Spiral\Attributes\AnnotationReader; |
15
|
|
|
use Spiral\Attributes\Factory; |
16
|
|
|
use Spiral\Attributes\ReaderInterface; |
17
|
|
|
use Spiral\Core\Container\SingletonInterface; |
18
|
|
|
use Spiral\Tokenizer\ClassesInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Locate all available annotations for methods, classes and properties across all the codebase. |
22
|
|
|
* |
23
|
|
|
* @deprecated since v2.9. Please, use combination of {@see ClassesInterface} and {@see ReaderInterface} |
24
|
|
|
*/ |
25
|
|
|
final class AnnotationLocator implements SingletonInterface |
26
|
|
|
{ |
27
|
|
|
/** @var ClassesInterface */ |
28
|
|
|
private $classLocator; |
29
|
|
|
|
30
|
|
|
/** @var ReaderInterface */ |
31
|
|
|
private $reader; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $targets = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* AnnotationLocator constructor. |
38
|
|
|
* |
39
|
|
|
* @param ReaderInterface|null $reader |
40
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ClassesInterface $classLocator, ReaderInterface $reader = null) |
43
|
|
|
{ |
44
|
|
|
$this->classLocator = $classLocator; |
45
|
|
|
$this->reader = $reader ?? (new Factory())->create(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Limit locator to only specific class types. |
50
|
|
|
*/ |
51
|
|
|
public function withTargets(array $targets): self |
52
|
|
|
{ |
53
|
|
|
$locator = clone $this; |
54
|
|
|
$locator->targets = $targets; |
55
|
|
|
|
56
|
|
|
return $locator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Find all classes with given annotation. |
61
|
|
|
* |
62
|
|
|
* @return iterable|AnnotatedClass[] |
63
|
|
|
*/ |
64
|
|
|
public function findClasses(string $annotation): iterable |
65
|
|
|
{ |
66
|
|
|
foreach ($this->getTargets() as $target) { |
67
|
|
|
$found = $this->reader->firstClassMetadata($target, $annotation); |
68
|
|
|
if ($found !== null) { |
69
|
|
|
yield new AnnotatedClass($target, $found); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Find all methods with given annotation. |
76
|
|
|
* |
77
|
|
|
* @return iterable|AnnotatedMethod[] |
78
|
|
|
*/ |
79
|
|
|
public function findMethods(string $annotation): iterable |
80
|
|
|
{ |
81
|
|
|
foreach ($this->getTargets() as $target) { |
82
|
|
|
foreach ($target->getMethods() as $method) { |
83
|
|
|
$found = $this->reader->firstFunctionMetadata($method, $annotation); |
84
|
|
|
if ($found !== null) { |
85
|
|
|
yield new AnnotatedMethod($method, $found); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Find all properties with given annotation. |
93
|
|
|
* |
94
|
|
|
* @return iterable|AnnotatedProperty[] |
95
|
|
|
*/ |
96
|
|
|
public function findProperties(string $annotation): iterable |
97
|
|
|
{ |
98
|
|
|
foreach ($this->getTargets() as $target) { |
99
|
|
|
foreach ($target->getProperties() as $property) { |
100
|
|
|
$found = $this->reader->firstPropertyMetadata($property, $annotation); |
101
|
|
|
if ($found !== null) { |
102
|
|
|
yield new AnnotatedProperty($property, $found); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return iterable|\ReflectionClass[] |
110
|
|
|
*/ |
111
|
|
|
private function getTargets(): iterable |
112
|
|
|
{ |
113
|
|
|
if ($this->targets === []) { |
114
|
|
|
yield from $this->classLocator->getClasses(); |
|
|
|
|
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($this->targets as $target) { |
119
|
|
|
yield from $this->classLocator->getClasses($target); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|