1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\unbox; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionFunction; |
9
|
|
|
use ReflectionParameter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class implements a simple dependency injection container. |
13
|
|
|
*/ |
14
|
|
|
class Container implements ContainerInterface, FactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var bool[] map where component name => TRUE, if the component has been initialized |
18
|
|
|
*/ |
19
|
|
|
protected $active = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var mixed[] map where component name => value |
23
|
|
|
*/ |
24
|
|
|
protected $values = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var callable[] map where component name => factory function |
28
|
|
|
*/ |
29
|
|
|
protected $factory = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array map where component name => mixed list/map of parameter names |
33
|
|
|
*/ |
34
|
|
|
protected $factory_map = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var (callable[])[] map where component name => list of configuration functions |
38
|
|
|
*/ |
39
|
|
|
protected $config = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array map where component name => mixed list/map of parameter names |
43
|
|
|
*/ |
44
|
|
|
protected $config_map = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed[] $values |
48
|
|
|
* @param callable[] $factory |
49
|
|
|
* @param array $factory_map |
50
|
|
|
* @param (callable[])[] $config |
51
|
|
|
* @param array $config_map |
52
|
|
|
*/ |
53
|
1 |
|
public function __construct(array $values, array $factory, array $factory_map, array $config, array $config_map) |
54
|
|
|
{ |
55
|
1 |
|
$this->values = $values + |
|
|
|
|
56
|
|
|
[ |
57
|
1 |
|
get_class($this) => $this, |
58
|
1 |
|
__CLASS__ => $this, |
59
|
1 |
|
ContainerInterface::class => $this, |
60
|
1 |
|
FactoryInterface::class => $this, |
61
|
1 |
|
]; |
62
|
|
|
|
63
|
1 |
|
$this->factory = $factory; |
64
|
1 |
|
$this->factory_map = $factory_map; |
65
|
1 |
|
$this->config = $config; |
66
|
1 |
|
$this->config_map = $config_map; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Resolve the registered component with the given name. |
71
|
|
|
* |
72
|
|
|
* @param string $name component name |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
* |
76
|
|
|
* @throws ContainerException |
77
|
|
|
* @throws NotFoundException |
78
|
|
|
*/ |
79
|
1 |
|
public function get($name) |
80
|
|
|
{ |
81
|
1 |
|
if (! isset($this->active[$name])) { |
82
|
1 |
|
if (isset($this->factory[$name])) { |
83
|
1 |
|
$factory = $this->factory[$name]; |
84
|
|
|
|
85
|
1 |
|
$reflection = new ReflectionFunction($factory); |
86
|
|
|
|
87
|
1 |
|
$params = $this->resolve($reflection->getParameters(), $this->factory_map[$name]); |
88
|
|
|
|
89
|
1 |
|
$this->values[$name] = call_user_func_array($factory, $params); |
90
|
1 |
|
} elseif (!array_key_exists($name, $this->values)) { |
91
|
1 |
|
throw new NotFoundException($name); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$this->active[$name] = true; |
95
|
|
|
|
96
|
1 |
|
$this->initialize($name); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
return $this->values[$name]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check for the existence of a component with a given name. |
104
|
|
|
* |
105
|
|
|
* @param string $name component name |
106
|
|
|
* |
107
|
|
|
* @return bool true, if a component with the given name has been defined |
108
|
|
|
*/ |
109
|
1 |
|
public function has($name) |
110
|
|
|
{ |
111
|
1 |
|
return array_key_exists($name, $this->values) || isset($this->factory[$name]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check if a component has been unboxed and is currently active. |
116
|
|
|
* |
117
|
|
|
* @param string $name component name |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
1 |
|
public function isActive($name) |
122
|
|
|
{ |
123
|
1 |
|
return isset($this->active[$name]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Call any given callable, using dependency injection to satisfy it's arguments, and/or |
128
|
|
|
* manually specifying some of those arguments - then return the value from the call. |
129
|
|
|
* |
130
|
|
|
* This will work for any callable: |
131
|
|
|
* |
132
|
|
|
* $container->call('foo'); // function foo() |
133
|
|
|
* $container->call($foo, 'baz'); // instance method $foo->baz() |
134
|
|
|
* $container->call([Foo::class, 'bar']); // static method Foo::bar() |
135
|
|
|
* $container->call($foo); // closure (or class implementing __invoke) |
136
|
|
|
* |
137
|
|
|
* In any of those examples, you can also supply custom arguments, either named or |
138
|
|
|
* positional, or mixed, as per the `$map` argument in `register()`, `configure()`, etc. |
139
|
|
|
* |
140
|
|
|
* See also {@see create()} which lets you invoke any constructor. |
141
|
|
|
* |
142
|
|
|
* @param callable|object $callback any arbitrary closure or callable, or object implementing __invoke() |
143
|
|
|
* @param mixed|mixed[] $map mixed list/map of parameter values (and/or boxed values) |
144
|
|
|
* |
145
|
|
|
* @return mixed return value from the given callable |
146
|
|
|
*/ |
147
|
1 |
|
public function call($callback, $map = []) |
148
|
|
|
{ |
149
|
1 |
|
$params = Reflection::createFromCallable($callback)->getParameters(); |
150
|
|
|
|
151
|
1 |
|
return call_user_func_array($callback, $this->resolve($params, $map)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create an instance of a given class. |
156
|
|
|
* |
157
|
|
|
* The container will internally resolve and inject any constructor arguments |
158
|
|
|
* not explicitly provided in the (optional) second parameter. |
159
|
|
|
* |
160
|
|
|
* @param string $class_name fully-qualified class-name |
161
|
|
|
* @param mixed|mixed[] $map mixed list/map of parameter values (and/or boxed values) |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
1 |
|
public function create($class_name, $map = []) |
166
|
|
|
{ |
167
|
1 |
|
if (!class_exists($class_name)) { |
168
|
1 |
|
throw new InvalidArgumentException("unable to create component: {$class_name}"); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
$reflection = new ReflectionClass($class_name); |
172
|
|
|
|
173
|
1 |
|
if (!$reflection->isInstantiable()) { |
174
|
1 |
|
throw new InvalidArgumentException("unable to create instance of abstract class: {$class_name}"); |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
$constructor = $reflection->getConstructor(); |
178
|
|
|
|
179
|
|
|
$params = $constructor |
180
|
1 |
|
? $this->resolve($constructor->getParameters(), $map, false) |
181
|
1 |
|
: []; |
182
|
|
|
|
183
|
1 |
|
return $reflection->newInstanceArgs($params); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Internally resolves parameters to functions or constructors. |
188
|
|
|
* |
189
|
|
|
* This is the heart of the beast. |
190
|
|
|
* |
191
|
|
|
* @param ReflectionParameter[] $params parameter reflections |
192
|
|
|
* @param array $map mixed list/map of parameter values (and/or boxed values) |
193
|
|
|
* @param bool $safe if TRUE, it's considered safe to resolve against parameter names |
194
|
|
|
* |
195
|
|
|
* @return array parameters |
196
|
|
|
* |
197
|
|
|
* @throws ContainerException |
198
|
|
|
* @throws NotFoundException |
199
|
|
|
*/ |
200
|
1 |
|
protected function resolve(array $params, $map, $safe = true) |
201
|
|
|
{ |
202
|
1 |
|
$args = []; |
203
|
|
|
|
204
|
1 |
|
foreach ($params as $index => $param) { |
205
|
1 |
|
$param_name = $param->name; |
206
|
|
|
|
207
|
1 |
|
if (array_key_exists($param_name, $map)) { |
208
|
1 |
|
$value = $map[$param_name]; // // resolve as user-provided named argument |
209
|
1 |
|
} elseif (array_key_exists($index, $map)) { |
210
|
1 |
|
$value = $map[$index]; // resolve as user-provided positional argument |
211
|
1 |
|
} else { |
212
|
|
|
// as on optimization, obtain the argument type without triggering autoload: |
213
|
|
|
|
214
|
1 |
|
$type = Reflection::getParameterType($param); |
215
|
|
|
|
216
|
1 |
|
if ($type && isset($map[$type])) { |
|
|
|
|
217
|
1 |
|
$value = $map[$type]; // resolve as user-provided type-hinted argument |
218
|
1 |
|
} elseif ($type && $this->has($type)) { |
|
|
|
|
219
|
1 |
|
$value = $this->get($type); // resolve as component registered by class/interface name |
220
|
1 |
|
} elseif ($safe && $this->has($param_name)) { |
221
|
1 |
|
$value = $this->get($param_name); // resolve as component with matching parameter name |
222
|
1 |
|
} elseif ($param->isOptional()) { |
223
|
1 |
|
$value = $param->getDefaultValue(); // unresolved: resolve using default value |
224
|
1 |
|
} else { |
225
|
|
|
// unresolved - throw a container exception: |
226
|
|
|
|
227
|
1 |
|
$reflection = $param->getDeclaringFunction(); |
228
|
|
|
|
229
|
1 |
|
throw new ContainerException( |
230
|
1 |
|
"unable to resolve parameter: \${$param_name} " . ($type ? "({$type}) " : "") . |
231
|
1 |
|
"in file: " . $reflection->getFileName() . ", line " . $reflection->getStartLine() |
232
|
1 |
|
); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
1 |
|
if ($value instanceof BoxedValueInterface) { |
237
|
1 |
|
$value = $value->unbox($this); // unbox a boxed value |
238
|
1 |
|
} |
239
|
|
|
|
240
|
1 |
|
$args[] = $value; // argument resolved! |
241
|
1 |
|
} |
242
|
|
|
|
243
|
1 |
|
return $args; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Dynamically inject a component into this Container. |
248
|
|
|
* |
249
|
|
|
* Enables classes that extend `Container` to dynamically inject components (to implement "auto-wiring") |
250
|
|
|
* |
251
|
|
|
* @param string $name |
252
|
|
|
* @param mixed $value |
253
|
|
|
*/ |
254
|
1 |
|
protected function inject($name, $value) |
255
|
|
|
{ |
256
|
1 |
|
$this->values[$name] = $value; |
257
|
1 |
|
$this->active[$name] = true; |
258
|
1 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Internally initialize an active component. |
262
|
|
|
* |
263
|
|
|
* @param string $name component name |
264
|
|
|
* |
265
|
|
|
* @return void |
266
|
|
|
*/ |
267
|
1 |
|
private function initialize($name) |
268
|
|
|
{ |
269
|
1 |
|
if (isset($this->config[$name])) { |
270
|
1 |
|
foreach ($this->config[$name] as $index => $config) { |
271
|
1 |
|
$map = $this->config_map[$name][$index]; |
272
|
|
|
|
273
|
1 |
|
$reflection = Reflection::createFromCallable($config); |
274
|
|
|
|
275
|
1 |
|
$params = $this->resolve($reflection->getParameters(), $map); |
276
|
|
|
|
277
|
1 |
|
$value = call_user_func_array($config, $params); |
278
|
|
|
|
279
|
1 |
|
if ($value !== null) { |
280
|
1 |
|
$this->values[$name] = $value; |
281
|
1 |
|
} |
282
|
1 |
|
} |
283
|
1 |
|
} |
284
|
1 |
|
} |
285
|
|
|
} |
286
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..