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