1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laganica\Di\Resolver; |
4
|
|
|
|
5
|
|
|
use Laganica\Di\Exception\ClassNotFoundException; |
6
|
|
|
use PhpDocReader\AnnotationException; |
7
|
|
|
use PhpDocReader\PhpDocReader; |
8
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
use ReflectionProperty; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ReflectionResolver |
15
|
|
|
* |
16
|
|
|
* @package Laganica\Di\Resolver |
17
|
|
|
*/ |
18
|
|
|
abstract class ReflectionResolver extends Resolver |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param string $class |
22
|
|
|
* |
23
|
|
|
* @throws ClassNotFoundException |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
12 |
|
protected function getConstructorParams(string $class): array |
28
|
|
|
{ |
29
|
12 |
|
$params = []; |
30
|
|
|
|
31
|
|
|
try { |
32
|
12 |
|
$reflection = new ReflectionClass($class); |
33
|
2 |
|
} catch (ReflectionException $e) { |
34
|
2 |
|
throw ClassNotFoundException::create($class); |
35
|
|
|
} |
36
|
|
|
|
37
|
10 |
|
$constructor = $reflection->getConstructor(); |
38
|
|
|
|
39
|
10 |
|
if ($constructor === null) { |
40
|
9 |
|
return $params; |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
foreach ($constructor->getParameters() AS $param) { |
44
|
4 |
|
$dependencyId = $param->getClass()->name; |
45
|
4 |
|
$params[] = $this->getContainer()->get($dependencyId); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return $params; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param object $entry |
53
|
|
|
* |
54
|
|
|
* @throws ClassNotFoundException |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
1 |
|
protected function injectProperties(object $entry): void |
59
|
|
|
{ |
60
|
1 |
|
$class = get_class($entry); |
61
|
|
|
|
62
|
|
|
try { |
63
|
1 |
|
$reflectionClass = new ReflectionClass($class); |
64
|
|
|
} catch (ReflectionException $e) { |
65
|
|
|
throw ClassNotFoundException::create($class); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$phpDocReader = new PhpDocReader(); |
69
|
|
|
|
70
|
1 |
|
foreach ($this->getInjectableProperties($reflectionClass) as $property) { |
71
|
|
|
try { |
72
|
1 |
|
$identifier = $phpDocReader->getPropertyClass($property); |
73
|
1 |
|
$property->setAccessible(true); |
74
|
1 |
|
$property->setValue($entry, $this->getContainer()->get($identifier)); |
75
|
|
|
} catch (AnnotationException $ex) { |
76
|
|
|
throw new ClassNotFoundException($ex->getMessage()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ReflectionClass $class |
83
|
|
|
* |
84
|
|
|
* @return ReflectionProperty[] |
85
|
|
|
*/ |
86
|
1 |
|
private function getInjectableProperties(ReflectionClass $class): array |
87
|
|
|
{ |
88
|
1 |
|
$properties = []; |
89
|
1 |
|
$docBlockFactory = DocBlockFactory::createInstance(); |
90
|
|
|
|
91
|
1 |
|
foreach ($class->getProperties() as $property) { |
92
|
1 |
|
if ($property->isStatic()) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$docBlock = $docBlockFactory->create($property->getDocComment()); |
97
|
|
|
|
98
|
1 |
|
if (!$docBlock->hasTag('Inject')) { |
99
|
1 |
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$properties[] = $property; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return $properties; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|