1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Reflection\Instanciator; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
Instanciator, |
8
|
|
|
Exception\InstanciationFailed, |
9
|
|
|
}; |
10
|
|
|
use Innmind\Immutable\{ |
11
|
|
|
Map, |
12
|
|
|
Set, |
13
|
|
|
Sequence, |
14
|
|
|
}; |
15
|
|
|
use function Innmind\Immutable\unwrap; |
16
|
|
|
|
17
|
|
|
final class ReflectionInstanciator implements Instanciator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
4 |
|
public function build(string $class, Map $properties): object |
23
|
|
|
{ |
24
|
|
|
try { |
25
|
4 |
|
$refl = new \ReflectionClass($class); |
26
|
|
|
|
27
|
4 |
|
if (!$refl->hasMethod('__construct')) { |
28
|
3 |
|
return $refl->newInstance(); |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
$constructor = $refl->getMethod('__construct'); |
32
|
|
|
|
33
|
3 |
|
return $refl->newInstanceArgs( |
34
|
|
|
unwrap($this->computeArguments($constructor, $properties)), |
|
|
|
|
35
|
3 |
|
); |
36
|
3 |
|
} catch (\TypeError $e) { |
37
|
3 |
|
throw new InstanciationFailed($class, $e); |
38
|
|
|
} |
39
|
2 |
|
} |
40
|
|
|
|
41
|
2 |
|
/** |
42
|
3 |
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function parameters(string $class): Set |
45
|
1 |
|
{ |
46
|
1 |
|
$parameters = Set::strings(); |
47
|
|
|
$refl = new \ReflectionClass($class); |
48
|
|
|
|
49
|
|
|
if (!$refl->hasMethod('__construct')) { |
50
|
|
|
return $parameters; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
$refl = $refl->getMethod('__construct'); |
54
|
|
|
|
55
|
3 |
|
foreach ($refl->getParameters() as $parameter) { |
56
|
3 |
|
$parameters = ($parameters)($parameter->name); |
57
|
|
|
} |
58
|
3 |
|
|
59
|
3 |
|
return $parameters; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
/** |
63
|
|
|
* @param Map<string, mixed> $properties |
64
|
2 |
|
* |
65
|
2 |
|
* @return Sequence<mixed> |
66
|
|
|
*/ |
67
|
|
|
private function computeArguments( |
68
|
2 |
|
\ReflectionMethod $constructor, |
69
|
|
|
Map $properties |
70
|
|
|
): Sequence { |
71
|
|
|
$arguments = Sequence::mixed(); |
72
|
|
|
|
73
|
|
|
foreach ($constructor->getParameters() as $parameter) { |
74
|
|
|
if ($this->canInject($parameter, $properties)) { |
75
|
|
|
$arguments = ($arguments)($properties->get($parameter->name)); |
76
|
|
|
} |
77
|
3 |
|
} |
78
|
|
|
|
79
|
|
|
return $arguments; |
80
|
|
|
} |
81
|
3 |
|
|
82
|
|
|
/** |
83
|
3 |
|
* @param Map<string, mixed> $properties |
84
|
3 |
|
*/ |
85
|
2 |
|
private function canInject( |
86
|
2 |
|
\ReflectionParameter $parameter, |
87
|
3 |
|
Map $properties |
88
|
|
|
): bool { |
89
|
|
|
if ( |
90
|
|
|
!$parameter->allowsNull() && |
91
|
|
|
!$properties->contains($parameter->name) |
92
|
3 |
|
) { |
93
|
|
|
return false; |
94
|
|
|
} else if ( |
95
|
|
|
$parameter->allowsNull() && |
96
|
|
|
!$properties->contains($parameter->name) |
97
|
|
|
) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
/** @var mixed */ |
102
|
|
|
$property = $properties->get($parameter->name); |
103
|
|
|
|
104
|
|
|
if ($parameter->hasType()) { |
105
|
|
|
/** @var \ReflectionNamedType $type */ |
106
|
3 |
|
$type = $parameter->getType(); |
107
|
3 |
|
|
108
|
|
|
if ($type->isBuiltin()) { |
109
|
1 |
|
return $type->getName() === \gettype($property); |
110
|
|
|
} |
111
|
3 |
|
|
112
|
3 |
|
if (!\is_object($property)) { |
113
|
|
|
return false; |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
$refl = new \ReflectionObject($property); |
117
|
2 |
|
$wishedClass = $type->getName(); |
118
|
|
|
|
119
|
2 |
|
return \get_class($property) === $wishedClass || |
120
|
1 |
|
$refl->isSubClassOf($wishedClass); |
121
|
|
|
} |
122
|
1 |
|
|
123
|
1 |
|
return true; |
124
|
1 |
|
} |
125
|
|
|
} |
126
|
|
|
|