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