|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Reflection; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
|
7
|
|
|
InjectionStrategyInterface, |
|
|
|
|
|
|
8
|
|
|
ExtractionStrategyInterface, |
|
|
|
|
|
|
9
|
|
|
ExtractionStrategy\ExtractionStrategies, |
|
10
|
|
|
InjectionStrategy\InjectionStrategies, |
|
11
|
|
|
Exception\InvalidArgumentException |
|
12
|
|
|
}; |
|
13
|
|
|
use Innmind\Immutable\{ |
|
14
|
|
|
MapInterface, |
|
15
|
|
|
Map |
|
16
|
|
|
}; |
|
17
|
|
|
|
|
18
|
|
|
class ReflectionObject |
|
19
|
|
|
{ |
|
20
|
|
|
private $object; |
|
21
|
|
|
private $properties; |
|
22
|
|
|
private $injectionStrategy; |
|
23
|
|
|
private $extractionStrategy; |
|
24
|
|
|
|
|
25
|
13 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
26
|
|
|
$object, |
|
27
|
|
|
MapInterface $properties = null, |
|
28
|
|
|
InjectionStrategyInterface $injectionStrategy = null, |
|
29
|
|
|
ExtractionStrategyInterface $extractionStrategy = null |
|
30
|
|
|
) { |
|
31
|
13 |
|
$properties = $properties ?? new Map('string', 'mixed'); |
|
32
|
|
|
|
|
33
|
|
|
if ( |
|
34
|
13 |
|
!is_object($object) || |
|
35
|
12 |
|
(string) $properties->keyType() !== 'string' || |
|
36
|
13 |
|
(string) $properties->valueType() !== 'mixed' |
|
37
|
|
|
) { |
|
38
|
1 |
|
throw new InvalidArgumentException; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
12 |
|
$this->object = $object; |
|
42
|
12 |
|
$this->properties = $properties; |
|
43
|
12 |
|
$this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default(); |
|
44
|
12 |
|
$this->extractionStrategy = $extractionStrategy ?? ExtractionStrategies::default(); |
|
45
|
12 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add a property that will be injected |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param mixed $value |
|
52
|
|
|
* |
|
53
|
|
|
* @return self |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function withProperty(string $name, $value) |
|
56
|
|
|
{ |
|
57
|
4 |
|
return new self( |
|
58
|
4 |
|
$this->object, |
|
59
|
4 |
|
$this->properties->put($name, $value), |
|
|
|
|
|
|
60
|
4 |
|
$this->injectionStrategy, |
|
61
|
4 |
|
$this->extractionStrategy |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add a set of properties that need to be injected |
|
67
|
|
|
* |
|
68
|
|
|
* @param array<string, mixed> $properties |
|
69
|
|
|
* |
|
70
|
|
|
* @return self |
|
71
|
|
|
*/ |
|
72
|
1 |
View Code Duplication |
public function withProperties(array $properties): self |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
1 |
|
$map = $this->properties; |
|
75
|
|
|
|
|
76
|
1 |
|
foreach ($properties as $key => $value) { |
|
77
|
1 |
|
$map = $map->put($key, $value); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return new self( |
|
81
|
1 |
|
$this->object, |
|
82
|
|
|
$map, |
|
83
|
1 |
|
$this->injectionStrategy, |
|
84
|
1 |
|
$this->extractionStrategy |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return the collection of properties that will be injected in the object |
|
90
|
|
|
* |
|
91
|
|
|
* @return MapInterface<string, mixed> |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function properties(): MapInterface |
|
94
|
|
|
{ |
|
95
|
2 |
|
return $this->properties; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the list of injection strategies used |
|
100
|
|
|
* |
|
101
|
|
|
* @return InjectionStrategyInterface |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function injectionStrategy(): InjectionStrategyInterface |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->injectionStrategy; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return the list of extraction strategies used |
|
110
|
|
|
* |
|
111
|
|
|
* @return ExtractionStrategyInterface |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function extractionStrategy(): ExtractionStrategyInterface |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->extractionStrategy; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return the object with the list of properties set on it |
|
120
|
|
|
* |
|
121
|
|
|
* @return object |
|
122
|
|
|
*/ |
|
123
|
|
|
public function build() |
|
124
|
|
|
{ |
|
125
|
7 |
|
$this->properties->foreach(function(string $key, $value): void { |
|
126
|
5 |
|
$this->inject($key, $value); |
|
127
|
7 |
|
}); |
|
128
|
|
|
|
|
129
|
5 |
|
return $this->object; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Extract the given list of properties |
|
134
|
|
|
* |
|
135
|
|
|
* @param string[] $properties |
|
136
|
|
|
* |
|
137
|
|
|
* @return MapInterface<string, mixed> |
|
|
|
|
|
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function extract(array $properties): MapInterface |
|
140
|
|
|
{ |
|
141
|
2 |
|
$map = new Map('string', 'mixed'); |
|
142
|
|
|
|
|
143
|
2 |
|
foreach ($properties as $property) { |
|
144
|
2 |
|
$map = $map->put( |
|
145
|
|
|
$property, |
|
|
|
|
|
|
146
|
2 |
|
$this->extractProperty($property) |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return $map; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Inject the given key/value pair into the object |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $key |
|
157
|
|
|
* @param mixed $value |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
5 |
|
private function inject(string $key, $value): void |
|
162
|
|
|
{ |
|
163
|
5 |
|
$this->injectionStrategy->inject($this->object, $key, $value); |
|
164
|
3 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Extract the given property out of the object |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $property |
|
170
|
|
|
* |
|
171
|
|
|
* @return mixed |
|
172
|
|
|
*/ |
|
173
|
2 |
|
private function extractProperty(string $property) |
|
174
|
|
|
{ |
|
175
|
2 |
|
return $this->extractionStrategy->extract($this->object, $property); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: