-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resolver.php
375 lines (319 loc) · 13.1 KB
/
Resolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* This file is part of the Vection package.
*
* (c) David M. Lung <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\DependencyInjection;
use ArrayObject;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
use Vection\Component\DependencyInjection\Attributes\Inject;
use Vection\Component\DependencyInjection\Attributes\PreventInjection;
use Vection\Component\DependencyInjection\Exception\DependencyResolverException;
use Vection\Component\DependencyInjection\Traits\AnnotationInjection;
use Vection\Contracts\Cache\CacheInterface;
use Vection\Contracts\DependencyInjection\InstructionInterface;
use Vection\Contracts\DependencyInjection\ResolverInterface;
/**
* Class Resolver
*
* This class resolves the dependencies for a given class name and
* saves only the information of the dependency classes and how
* they have to be injected by the Injector class.
*
* @package Vection\Component\DependencyInjection
* @author David M. Lung <[email protected]>
*/
class Resolver implements ResolverInterface
{
/**
* The cache saves all resolved dependency tree information
* to avoid resolving on each request. Each new resolved information
* will be saved directly into the cache storage.
*
* @var CacheInterface|null
*/
protected CacheInterface|null $cache = null;
/**
* This array object contains custom dependency definitions
* which will be considered by the resolving process.
*
* @var ArrayObject<string, InstructionInterface>
*/
protected ArrayObject $instructions;
/**
* This property contains all resolved dependency information
* which will be cached and reused on each injection by the Injector class.
*
* @var ArrayObject<string, mixed>
*/
protected ArrayObject $dependencies;
/**
* Resolver constructor.
*
* @param ArrayObject<string, InstructionInterface>|null $instructions
* @param ArrayObject<string, mixed>|null $dependencies
*/
public function __construct(ArrayObject|null $instructions = null, ArrayObject|null $dependencies = null)
{
$this->instructions = $instructions ?: new ArrayObject();
$this->dependencies = $dependencies ?: new ArrayObject();
}
/**
* @param CacheInterface $cache
*/
public function setCache(CacheInterface $cache): void
{
$this->cache = $cache;
if ( $cache->contains('dependencies') ) {
if ( $existing = $this->dependencies->getArrayCopy() ) {
$cachedDependencies = $cache->getArray('dependencies');
$dependencies = array_merge($existing, $cachedDependencies);
$this->dependencies->exchangeArray($dependencies);
} else {
$this->dependencies->exchangeArray($cache->getArray('dependencies'));
}
} else if ( $this->dependencies->count() > 0 ) {
# Apply dependencies to cache which have been resolved before set the cache
$cache->setArray('dependencies', $this->dependencies->getArrayCopy());
}
}
/**
* @inheritDoc
*/
public function addInstruction(InstructionInterface $instruction): void
{
$this->instructions[$instruction->getClassName()] = $instruction;
}
/**
* @inheritDoc
*/
public function getInstruction(string $className): InstructionInterface|null
{
return $this->instructions[$className] ?? null;
}
/**
* @param string $className
*
* @return array<string, mixed>|null
*/
public function getClassDependencies(string $className): array|null
{
return $this->dependencies[$className] ?? null;
}
/**
* @inheritDoc
*/
public function resolveDependencies(string $className): array
{
if (isset($this->dependencies[$className])) {
return $this->dependencies[$className];
}
$this->dependencies[$className] = [
'by' => null,
'constructor' => [],
'setter' => [],
'annotation' => [],
'magic' => [],
'factory' => null,
];
try {
$dependencies = &$this->dependencies[$className];
if (isset($this->instructions[$className])) {
$instruction = $this->instructions[$className];
$dependencies['by'] = $instruction->getBy();
$dependencies['factory'] = $this->instructions[$className]->getFactory();
}
if (!$dependencies['factory']) {
$dependencies['constructor'] = $this->resolveConstructorDependencies($className);
}
# This dependencies will be passed by setter defined by interfaces
$dependencies['setter'] = $this->resolveInterfaceDependencies($className);
# This will use the __annotationInjection method from AnnotationInjection trait
$dependencies['annotation'] = $this->resolveAnnotatedDependencies($className);
# This is for the magic inject by __inject method
$dependencies['magic'] = $this->resolveMagicInjectionDependencies($className);
$this->cache?->setArray('dependencies', $this->dependencies->getArrayCopy());
}
catch ( ReflectionException $e ) {
throw new DependencyResolverException(
"Reflection Error while resolving dependencies of class '$className'",
0,
$e
);
}
return $this->dependencies[$className];
}
/**
* Resolves the dependency information defined by the constructor of the given class.
*
* Returns an array contains the class names of the dependencies in the same order
* as defined by the constructor.
*
* @param string $className
*
* @return string[] Contains class names of the dependencies.
*
* @throws ReflectionException
*/
protected function resolveConstructorDependencies(string $className): array
{
$dependencies = [];
$constructor = (new ReflectionClass($className))->getConstructor();
if ($constructor && ($constructParams = $constructor->getParameters())) {
if ($constructor->getAttributes(PreventInjection::class)) {
return $dependencies;
}
foreach ($constructParams as $param) {
$type = $param->getType();
if ($type === null) {
throw new DependencyResolverException(
'Constructor parameter injection requires typed, non built-in parameters in '.$className
);
}
if (!$type instanceof ReflectionNamedType || $type->isBuiltin()) {
if (!$param->isDefaultValueAvailable()) {
$this->dependencies[$className]['constructor_has_primitives'] = true;
}
break;
}
$reflection = new ReflectionClass($type->getName());
if ($reflection->isInterface() && $param->allowsNull()) {
if (!isset($this->dependencies[$className]['constructor_nullable_interface'])) {
$this->dependencies[$className]['constructor_nullable_interface'] = [];
}
$this->dependencies[$className]['constructor_nullable_interface'][] = $reflection->getName();
}
if ($param->allowsNull() && $param->getAttributes(PreventInjection::class)) {
if (!isset($this->dependencies[$className]['constructor_prevent_injection'])) {
$this->dependencies[$className]['constructor_prevent_injection'] = [];
}
$this->dependencies[$className]['constructor_prevent_injection'][] = $reflection->getName();
}
$dependencies[] = $reflection->getName();
if ( $reflection->isInstantiable() ) {
$this->resolveDependencies($reflection->getName());
}
}
}
if ( ! $dependencies && ($parent = get_parent_class($className)) ) {
do {
$dependencies = $this->resolveConstructorDependencies($parent);
} while ( ! $dependencies && ($parent = get_parent_class($parent)) );
}
return $dependencies;
}
/**
* Resolves the dependencies information by interface definition.
*
* Returns an array contains the setter as key and class name of the
* dependencies which will be injected as value.
*
* @param string $className
*
* @return string[] Contains setter methods as key and class as value.
*/
protected function resolveInterfaceDependencies(string $className): array
{
$dependencyClassNames = [];
# Add setter injections by interfaces
foreach (class_implements($className) ?: [] as $interface) {
if (isset($this->instructions[$interface])) {
foreach ($this->instructions[$interface]->getSetterInjections() as $method => $dependency) {
$this->resolveDependencies($dependency);
$dependencyClassNames[$method] = $dependency;
}
}
}
return $dependencyClassNames;
}
/**
* Resolves the annotated dependencies defined by the given class.
*
* Returns an array with the internal property name as key and the
* class name of the dependency as value.
*
* @param string $className
*
* @return string[]
*
* @throws ReflectionException
*/
protected function resolveAnnotatedDependencies(string $className): array
{
$dependencyClassNames = [];
$reflection = new ReflectionClass($className);
$useInjectionTrait = isset($reflection->getTraits()[AnnotationInjection::class]);
foreach ( class_parents($className) ?: [] as $parentClassName ) {
if ( isset((new ReflectionClass($parentClassName))->getTraits()[AnnotationInjection::class]) ) {
$useInjectionTrait = true;
foreach($this->resolveAnnotatedDependencies($parentClassName) as $name => $dependencyClassName) {
$dependencyClassNames[$name] = $dependencyClassName;
}
}
}
if ( $useInjectionTrait ) {
foreach ( $reflection->getProperties() as $property ) {
$propertyType = null;
if ($property->getAttributes(Inject::class)) {
if (!$property->hasType()) {
throw new DependencyResolverException(
'The use of attribute based injection requires typed properties. '.
"Property {$property->getName()} in class $className has no type."
);
}
$propertyType = $property->getType();
if (!$propertyType instanceof ReflectionNamedType || $propertyType->isBuiltin()) {
throw new DependencyResolverException(
'The use of attribute based injection requires non built-in typed properties. '.
"Property {$property->getName()} in class $className has an invalid or built-in type."
);
}
$dependencyClassName = $propertyType->getName();
$dependencyClassNames[$property->getName()] = $dependencyClassName;
$this->resolveDependencies($dependencyClassName);
}
}
}
return $dependencyClassNames;
}
/**
* Resolves the magic requested dependencies by the __inject method.
*
* Returns an array contains the class names of the dependencies in same
* order as defined by the given class __inject method.
*
* @param string $className
*
* @return string[]
*
* @throws ReflectionException
*/
protected function resolveMagicInjectionDependencies(string $className): array
{
$dependencyClassNames = [];
$reflection = new ReflectionClass($className);
if ( $reflection->hasMethod('__inject') ) {
$method = $reflection->getMethod('__inject');
foreach ($method->getParameters() as $param) {
if ($param->hasType()) {
$type = $param->getType();
if ( $type instanceof ReflectionNamedType && ! $type->isBuiltin() ) {
$dependencyClassNames[] = $type->getName();
continue;
}
}
throw new DependencyResolverException(
"Then magic $className::__inject method expects arguments to be non build-in typed parameters."
);
}
}
return $dependencyClassNames;
}
}