|
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\Internal; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Attributes\Internal\Instantiator\Factory; |
|
15
|
|
|
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface; |
|
16
|
|
|
use Spiral\Attributes\Reader; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @internal AttributeReader is an internal library class, please do not use it in your code. |
|
20
|
|
|
* @psalm-internal Spiral\Attributes |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AttributeReader extends Reader |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var InstantiatorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $instantiator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Context |
|
31
|
|
|
*/ |
|
32
|
|
|
private $ctx; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AttributeReader constructor. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->instantiator = new Factory(); |
|
40
|
|
|
$this->ctx = new Context(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
* @throws \ReflectionException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getClassMetadata(\ReflectionClass $class, string $name = null): iterable |
|
48
|
|
|
{ |
|
49
|
|
|
$attributes = $this->getClassAttributes($class, $name); |
|
50
|
|
|
$context = $this->ctx->getClassContext($class); |
|
51
|
|
|
|
|
52
|
|
|
foreach ($attributes as $attribute => $arguments) { |
|
53
|
|
|
yield $this->instantiate($attribute, $arguments, $context); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param \ReflectionClass $class |
|
59
|
|
|
* @param string|null $name |
|
60
|
|
|
* @return iterable<\ReflectionClass, array> |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract protected function getClassAttributes(\ReflectionClass $class, ?string $name): iterable; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param \ReflectionClass $attribute |
|
66
|
|
|
* @param array $arguments |
|
67
|
|
|
* @param string $context |
|
68
|
|
|
* @return object |
|
69
|
|
|
* @throws \ReflectionException |
|
70
|
|
|
*/ |
|
71
|
|
|
private function instantiate(\ReflectionClass $attribute, array $arguments, string $context): object |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->instantiator->instantiate($attribute, $arguments, $context); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritDoc} |
|
78
|
|
|
* @throws \ReflectionException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getFunctionMetadata(\ReflectionFunctionAbstract $function, string $name = null): iterable |
|
81
|
|
|
{ |
|
82
|
|
|
$attributes = $this->getFunctionAttributes($function, $name); |
|
83
|
|
|
$context = $this->ctx->getCallableContext($function); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($attributes as $attribute => $arguments) { |
|
86
|
|
|
yield $this->instantiate($attribute, $arguments, $context); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param \ReflectionFunctionAbstract $function |
|
92
|
|
|
* @param string|null $name |
|
93
|
|
|
* @return iterable<\ReflectionClass, array> |
|
94
|
|
|
*/ |
|
95
|
|
|
abstract protected function getFunctionAttributes(\ReflectionFunctionAbstract $function, ?string $name): iterable; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritDoc} |
|
99
|
|
|
* @throws \ReflectionException |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getPropertyMetadata(\ReflectionProperty $property, string $name = null): iterable |
|
102
|
|
|
{ |
|
103
|
|
|
$attributes = $this->getPropertyAttributes($property, $name); |
|
104
|
|
|
$context = $this->ctx->getPropertyContext($property); |
|
105
|
|
|
|
|
106
|
|
|
foreach ($attributes as $attribute => $arguments) { |
|
107
|
|
|
yield $this->instantiate($attribute, $arguments, $context); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param \ReflectionProperty $property |
|
113
|
|
|
* @param string|null $name |
|
114
|
|
|
* @return iterable<\ReflectionClass, array> |
|
115
|
|
|
*/ |
|
116
|
|
|
abstract protected function getPropertyAttributes(\ReflectionProperty $property, ?string $name): iterable; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritDoc} |
|
120
|
|
|
* @throws \ReflectionException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getConstantMetadata(\ReflectionClassConstant $constant, string $name = null): iterable |
|
123
|
|
|
{ |
|
124
|
|
|
$attributes = $this->getConstantAttributes($constant, $name); |
|
125
|
|
|
$context = $this->ctx->getConstantContext($constant); |
|
126
|
|
|
|
|
127
|
|
|
foreach ($attributes as $attribute => $arguments) { |
|
128
|
|
|
yield $this->instantiate($attribute, $arguments, $context); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param \ReflectionClassConstant $const |
|
134
|
|
|
* @param string|null $name |
|
135
|
|
|
* @return iterable<\ReflectionClass, array> |
|
136
|
|
|
*/ |
|
137
|
|
|
abstract protected function getConstantAttributes(\ReflectionClassConstant $const, ?string $name): iterable; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritDoc} |
|
141
|
|
|
* @throws \ReflectionException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getParameterMetadata(\ReflectionParameter $parameter, string $name = null): iterable |
|
144
|
|
|
{ |
|
145
|
|
|
$attributes = $this->getParameterAttributes($parameter, $name); |
|
146
|
|
|
$context = $this->ctx->getParameterContext($parameter); |
|
147
|
|
|
|
|
148
|
|
|
foreach ($attributes as $attribute => $arguments) { |
|
149
|
|
|
yield $this->instantiate($attribute, $arguments, $context); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param \ReflectionParameter $param |
|
155
|
|
|
* @param string|null $name |
|
156
|
|
|
* @return iterable<\ReflectionClass, array> |
|
157
|
|
|
*/ |
|
158
|
|
|
abstract protected function getParameterAttributes(\ReflectionParameter $param, ?string $name): iterable; |
|
159
|
|
|
} |
|
160
|
|
|
|