1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Reflection; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
InjectionStrategy\InjectionStrategies, |
8
|
|
|
Instanciator\ReflectionInstanciator, |
9
|
|
|
Exception\InvalidArgumentException, |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
Map, |
13
|
|
|
Set, |
14
|
|
|
}; |
15
|
|
|
use function Innmind\Immutable\assertMap; |
16
|
|
|
|
17
|
|
|
final class ReflectionClass |
18
|
|
|
{ |
19
|
|
|
/** @var class-string */ |
20
|
|
|
private string $class; |
21
|
|
|
/** @var Map<string, mixed> */ |
22
|
|
|
private Map $properties; |
23
|
|
|
private InjectionStrategy $injectionStrategy; |
24
|
|
|
private Instanciator $instanciator; |
25
|
3 |
|
|
26
|
|
|
/** |
27
|
|
|
* @param class-string $class |
28
|
|
|
* @param Map<string, mixed>|null $properties |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
3 |
|
string $class, |
32
|
|
|
Map $properties = null, |
33
|
|
|
InjectionStrategy $injectionStrategy = null, |
34
|
3 |
|
Instanciator $instanciator = null |
35
|
3 |
|
) { |
36
|
|
|
/** @var Map<string, mixed> $default */ |
37
|
|
|
$default = Map::of('string', 'mixed'); |
38
|
|
|
$properties ??= $default; |
39
|
|
|
|
40
|
3 |
|
assertMap('string', 'mixed', $properties, 2); |
41
|
3 |
|
|
42
|
3 |
|
$this->class = $class; |
43
|
3 |
|
$this->properties = $properties; |
44
|
3 |
|
$this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default(); |
45
|
|
|
$this->instanciator = $instanciator ?? new ReflectionInstanciator; |
46
|
3 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param class-string $class |
50
|
|
|
* @param Map<string, mixed>|null $properties |
51
|
|
|
*/ |
52
|
3 |
|
public static function of( |
53
|
|
|
string $class, |
54
|
|
|
Map $properties = null, |
55
|
|
|
InjectionStrategy $injectionStrategy = null, |
56
|
|
|
Instanciator $instanciator = null |
57
|
|
|
): self { |
58
|
|
|
return new self($class, $properties, $injectionStrategy, $instanciator); |
59
|
|
|
} |
60
|
1 |
|
|
61
|
|
|
/** |
62
|
1 |
|
* Add a property to be injected in the new object |
63
|
1 |
|
* |
64
|
1 |
|
* @param mixed $value |
65
|
1 |
|
*/ |
66
|
1 |
|
public function withProperty(string $property, $value): self |
67
|
|
|
{ |
68
|
|
|
return new self( |
69
|
|
|
$this->class, |
70
|
|
|
($this->properties)($property, $value), |
71
|
|
|
$this->injectionStrategy, |
72
|
|
|
$this->instanciator, |
73
|
|
|
); |
74
|
|
|
} |
75
|
1 |
|
|
76
|
|
|
/** |
77
|
1 |
|
* Add a set of properties that need to be injected |
78
|
|
|
* |
79
|
1 |
|
* @param array<string, mixed> $properties |
80
|
1 |
|
*/ |
81
|
|
|
public function withProperties(array $properties): self |
82
|
|
|
{ |
83
|
1 |
|
$map = $this->properties; |
84
|
1 |
|
|
85
|
1 |
|
/** @var mixed $value */ |
86
|
1 |
|
foreach ($properties as $key => $value) { |
87
|
1 |
|
$map = ($map)($key, $value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return new self( |
91
|
|
|
$this->class, |
92
|
|
|
$map, |
93
|
|
|
$this->injectionStrategy, |
94
|
2 |
|
$this->instanciator, |
95
|
|
|
); |
96
|
2 |
|
} |
97
|
2 |
|
|
98
|
|
|
/** |
99
|
|
|
* Return a new instance of the class |
100
|
|
|
*/ |
101
|
2 |
|
public function build(): object |
102
|
|
|
{ |
103
|
1 |
|
$object = $this->instanciator->build($this->class, $this->properties); |
104
|
2 |
|
$parameters = $this->instanciator->parameters($this->class); |
105
|
2 |
|
|
106
|
2 |
|
//avoid injecting the properties already used in the constructor |
107
|
2 |
|
$properties = $this |
108
|
2 |
|
->properties |
109
|
|
|
->filter(static fn(string $property) => !$parameters->contains($property)); |
|
|
|
|
110
|
|
|
$refl = new ReflectionObject( |
111
|
2 |
|
$object, |
112
|
|
|
$properties, |
113
|
|
|
$this->injectionStrategy, |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return $refl->build(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return all the properties defined on the class |
121
|
1 |
|
* |
122
|
|
|
* It will not extract properties defined in a parent class |
123
|
1 |
|
* |
124
|
1 |
|
* @return Set<string> |
125
|
|
|
*/ |
126
|
1 |
|
public function properties(): Set |
127
|
1 |
|
{ |
128
|
|
|
$refl = new \ReflectionClass($this->class); |
129
|
|
|
$properties = Set::strings(); |
130
|
1 |
|
|
131
|
|
|
foreach ($refl->getProperties() as $property) { |
132
|
|
|
$properties = ($properties)($property->getName()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $properties; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|