1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2017 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Garden\Container; |
9
|
|
|
|
10
|
|
|
use Interop\Container\ContainerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* An inversion of control container. |
14
|
|
|
*/ |
15
|
|
|
class Container implements ContainerInterface { |
16
|
|
|
private $currentRule; |
17
|
|
|
private $currentRuleName; |
18
|
|
|
private $instances; |
19
|
|
|
private $rules; |
20
|
|
|
private $factories; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Construct a new instance of the {@link Container} class. |
24
|
|
|
*/ |
25
|
88 |
|
public function __construct() { |
26
|
88 |
|
$this->rules = ['*' => ['inherit' => true, 'constructorArgs' => null]]; |
27
|
88 |
|
$this->instances = []; |
28
|
88 |
|
$this->factories = []; |
29
|
|
|
|
30
|
88 |
|
$this->rule('*'); |
31
|
88 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Deep clone rules. |
35
|
|
|
*/ |
36
|
1 |
|
public function __clone() { |
37
|
1 |
|
$this->rules = $this->arrayClone($this->rules); |
38
|
1 |
|
$this->rule($this->currentRuleName); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Clear all instances |
43
|
|
|
* |
44
|
|
|
*/ |
45
|
1 |
|
public function clearInstances() { |
46
|
1 |
|
$this->instances = []; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Deep clone an array. |
51
|
|
|
* |
52
|
|
|
* @param array $array The array to clone. |
53
|
|
|
* @return array Returns the cloned array. |
54
|
|
|
* @see http://stackoverflow.com/a/17729234 |
55
|
|
|
*/ |
56
|
1 |
|
private function arrayClone(array $array) { |
57
|
|
|
return array_map(function ($element) { |
58
|
1 |
|
return ((is_array($element)) |
59
|
1 |
|
? $this->arrayClone($element) |
60
|
1 |
|
: ((is_object($element)) |
61
|
1 |
|
? clone $element |
62
|
|
|
: $element |
63
|
|
|
) |
64
|
1 |
|
); |
65
|
1 |
|
}, $array); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Normalize a container entry ID. |
70
|
|
|
* |
71
|
|
|
* @param string $id The ID to normalize. |
72
|
|
|
* @return string Returns a normalized ID as a string. |
73
|
|
|
*/ |
74
|
89 |
|
private function normalizeID($id) { |
75
|
89 |
|
return ltrim($id, '\\'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the current rule to the default rule. |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
1 |
|
public function defaultRule() { |
84
|
1 |
|
return $this->rule('*'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the current rule. |
89
|
|
|
* |
90
|
|
|
* @param string $id The ID of the rule. |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
88 |
|
public function rule($id) { |
94
|
88 |
|
$id = $this->normalizeID($id); |
95
|
|
|
|
96
|
88 |
|
if (!isset($this->rules[$id])) { |
97
|
43 |
|
$this->rules[$id] = []; |
98
|
43 |
|
} |
99
|
88 |
|
$this->currentRuleName = $id; |
100
|
88 |
|
$this->currentRule = &$this->rules[$id]; |
101
|
|
|
|
102
|
88 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the class name of the current rule. |
107
|
|
|
* |
108
|
|
|
* @return string Returns a class name. |
109
|
|
|
*/ |
110
|
2 |
|
public function getClass() { |
111
|
2 |
|
return empty($this->currentRule['class']) ? '' : $this->currentRule['class']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the name of the class for the current rule. |
116
|
|
|
* |
117
|
|
|
* @param string $className A valid class name. |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
6 |
|
public function setClass($className) { |
121
|
6 |
|
$this->currentRule['class'] = $className; |
122
|
6 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the rule that the current rule references. |
127
|
|
|
* |
128
|
|
|
* @return string Returns a reference name or an empty string if there is no reference. |
129
|
|
|
*/ |
130
|
3 |
|
public function getAliasOf() { |
131
|
3 |
|
return empty($this->currentRule['aliasOf']) ? '' : $this->currentRule['aliasOf']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set the rule that the current rule is an alias of. |
136
|
|
|
* |
137
|
|
|
* @param string $alias The name of an entry in the container to point to. |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
4 |
|
public function setAliasOf($alias) { |
141
|
4 |
|
$alias = $this->normalizeID($alias); |
142
|
|
|
|
143
|
4 |
|
if ($alias === $this->currentRuleName) { |
144
|
1 |
|
trigger_error("You cannot set alias '$alias' to itself.", E_USER_NOTICE); |
145
|
1 |
|
} else { |
146
|
3 |
|
$this->currentRule['aliasOf'] = $alias; |
147
|
|
|
} |
148
|
4 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add an alias of the current rule. |
153
|
|
|
* |
154
|
|
|
* Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
155
|
|
|
* with the current rule. If the current rule is shared then the same shared instance will be returned. You can add |
156
|
|
|
* multiple aliases by passing additional arguments to this method. |
157
|
|
|
* |
158
|
|
|
* If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
159
|
|
|
* level error is raised and the alias is not added. |
160
|
|
|
* |
161
|
|
|
* @param string ...$alias The alias to set. |
162
|
|
|
* @return $this |
163
|
|
|
* @since 1.4 Added the ability to pass multiple aliases. |
164
|
|
|
*/ |
|
|
|
|
165
|
8 |
|
public function addAlias(...$alias) { |
166
|
8 |
|
foreach ($alias as $name) { |
167
|
8 |
|
$name = $this->normalizeID($name); |
168
|
|
|
|
169
|
8 |
|
if ($name === $this->currentRuleName) { |
170
|
1 |
|
trigger_error("Tried to set alias '$name' to self.", E_USER_NOTICE); |
171
|
1 |
|
} else { |
172
|
7 |
|
$this->rules[$name]['aliasOf'] = $this->currentRuleName; |
173
|
|
|
} |
174
|
8 |
|
} |
175
|
8 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Remove an alias of the current rule. |
180
|
|
|
* |
181
|
|
|
* If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
182
|
|
|
* level error is raised, but the alias is still removed. |
183
|
|
|
* |
184
|
|
|
* @param string $alias The alias to remove. |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
2 |
|
public function removeAlias($alias) { |
188
|
2 |
|
$alias = $this->normalizeID($alias); |
189
|
|
|
|
190
|
2 |
|
if (!empty($this->rules[$alias]['aliasOf']) && $this->rules[$alias]['aliasOf'] !== $this->currentRuleName) { |
191
|
1 |
|
trigger_error("Alias '$alias' does not point to the current rule.", E_USER_NOTICE); |
192
|
1 |
|
} |
193
|
|
|
|
194
|
2 |
|
unset($this->rules[$alias]['aliasOf']); |
195
|
2 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get all of the aliases of the current rule. |
200
|
|
|
* |
201
|
|
|
* This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
202
|
|
|
* |
203
|
|
|
* @return array Returns an array of strings representing aliases. |
204
|
|
|
*/ |
205
|
6 |
|
public function getAliases() { |
206
|
6 |
|
$result = []; |
207
|
|
|
|
208
|
6 |
|
foreach ($this->rules as $name => $rule) { |
209
|
6 |
|
if (!empty($rule['aliasOf']) && $rule['aliasOf'] === $this->currentRuleName) { |
210
|
4 |
|
$result[] = $name; |
211
|
4 |
|
} |
212
|
6 |
|
} |
213
|
|
|
|
214
|
6 |
|
return $result; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the factory callback for the current rule. |
219
|
|
|
* |
220
|
|
|
* @return callable|null Returns the rule's factory or **null** if it has none. |
221
|
|
|
*/ |
222
|
2 |
|
public function getFactory() { |
223
|
2 |
|
return isset($this->currentRule['factory']) ? $this->currentRule['factory'] : null; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Set the factory that will be used to create the instance for the current rule. |
228
|
|
|
* |
229
|
|
|
* @param callable $factory This callback will be called to create the instance for the rule. |
230
|
|
|
* @return $this |
231
|
|
|
*/ |
232
|
10 |
|
public function setFactory(callable $factory) { |
233
|
10 |
|
$this->currentRule['factory'] = $factory; |
234
|
10 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Whether or not the current rule is shared. |
239
|
|
|
* |
240
|
|
|
* @return bool Returns **true** if the rule is shared or **false** otherwise. |
241
|
|
|
*/ |
242
|
2 |
|
public function isShared() { |
243
|
2 |
|
return !empty($this->currentRule['shared']); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set whether or not the current rule is shared. |
248
|
|
|
* |
249
|
|
|
* @param bool $shared Whether or not the current rule is shared. |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
38 |
|
public function setShared($shared) { |
253
|
38 |
|
$this->currentRule['shared'] = $shared; |
254
|
38 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Whether or not the current rule will inherit to subclasses. |
259
|
|
|
* |
260
|
|
|
* @return bool Returns **true** if the current rule inherits or **false** otherwise. |
261
|
|
|
*/ |
262
|
2 |
|
public function getInherit() { |
263
|
2 |
|
return !empty($this->currentRule['inherit']); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Set whether or not the current rule extends to subclasses. |
268
|
|
|
* |
269
|
|
|
* @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
270
|
|
|
* @return $this |
271
|
|
|
*/ |
272
|
3 |
|
public function setInherit($inherit) { |
273
|
3 |
|
$this->currentRule['inherit'] = $inherit; |
274
|
3 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the constructor arguments for the current rule. |
279
|
|
|
* |
280
|
|
|
* @return array Returns the constructor arguments for the current rule. |
281
|
|
|
*/ |
282
|
2 |
|
public function getConstructorArgs() { |
283
|
2 |
|
return empty($this->currentRule['constructorArgs']) ? [] : $this->currentRule['constructorArgs']; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set the constructor arguments for the current rule. |
288
|
|
|
* |
289
|
|
|
* @param array $args An array of constructor arguments. |
290
|
|
|
* @return $this |
291
|
|
|
*/ |
292
|
25 |
|
public function setConstructorArgs(array $args) { |
293
|
25 |
|
$this->currentRule['constructorArgs'] = $args; |
294
|
25 |
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Set a specific shared instance into the container. |
299
|
|
|
* |
300
|
|
|
* When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
301
|
|
|
* rule is configured that says that instances should not be shared. |
302
|
|
|
* |
303
|
|
|
* @param string $name The name of the container entry. |
304
|
|
|
* @param mixed $instance This instance. |
305
|
|
|
* @return $this |
306
|
|
|
*/ |
307
|
9 |
|
public function setInstance($name, $instance) { |
308
|
9 |
|
$this->instances[$this->normalizeID($name)] = $instance; |
309
|
9 |
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Add a method call to a rule. |
314
|
|
|
* |
315
|
|
|
* @param string $method The name of the method to call. |
316
|
|
|
* @param array $args The arguments to pass to the method. |
317
|
|
|
* @return $this |
318
|
|
|
*/ |
319
|
6 |
|
public function addCall($method, array $args = []) { |
320
|
6 |
|
$this->currentRule['calls'][] = [$method, $args]; |
321
|
|
|
|
322
|
6 |
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Finds an entry of the container by its identifier and returns it. |
327
|
|
|
* |
328
|
|
|
* @param string $id Identifier of the entry to look for. |
329
|
|
|
* @param array $args Additional arguments to pass to the constructor. |
330
|
|
|
* |
331
|
|
|
* @throws NotFoundException No entry was found for this identifier. |
332
|
|
|
* @throws ContainerException Error while retrieving the entry. |
333
|
|
|
* |
334
|
|
|
* @return mixed Entry. |
335
|
|
|
*/ |
336
|
69 |
|
public function getArgs($id, array $args = []) { |
337
|
69 |
|
$id = $this->normalizeID($id); |
338
|
|
|
|
339
|
69 |
|
if (isset($this->instances[$id])) { |
340
|
|
|
// A shared instance just gets returned. |
341
|
16 |
|
return $this->instances[$id]; |
342
|
|
|
} |
343
|
|
|
|
344
|
65 |
|
if (isset($this->factories[$id])) { |
345
|
|
|
// The factory for this object type is already there so call it to create the instance. |
346
|
4 |
|
return $this->factories[$id]($args); |
347
|
|
|
} |
348
|
|
|
|
349
|
65 |
|
if (!empty($this->rules[$id]['aliasOf'])) { |
350
|
|
|
// This rule references another rule. |
351
|
3 |
|
return $this->getArgs($this->rules[$id]['aliasOf'], $args); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
// The factory or instance isn't registered so do that now. |
355
|
|
|
// This call also caches the instance or factory fo faster access next time. |
356
|
65 |
|
return $this->createInstance($id, $args); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Make a rule based on an ID. |
361
|
|
|
* |
362
|
|
|
* @param string $nid A normalized ID. |
363
|
|
|
* @return array Returns an array representing a rule. |
364
|
|
|
*/ |
365
|
65 |
|
private function makeRule($nid) { |
366
|
65 |
|
$rule = isset($this->rules[$nid]) ? $this->rules[$nid] : []; |
367
|
|
|
|
368
|
65 |
|
if (class_exists($nid)) { |
369
|
57 |
|
for ($class = get_parent_class($nid); !empty($class); $class = get_parent_class($class)) { |
370
|
|
|
// Don't add the rule if it doesn't say to inherit. |
371
|
6 |
|
if (!isset($this->rules[$class]) || (isset($this->rules[$class]['inherit']) && !$this->rules[$class]['inherit'])) { |
372
|
4 |
|
break; |
373
|
|
|
} |
374
|
2 |
|
$rule += $this->rules[$class]; |
375
|
2 |
|
} |
376
|
|
|
|
377
|
|
|
// Add the default rule. |
378
|
57 |
|
if (!empty($this->rules['*']['inherit'])) { |
379
|
57 |
|
$rule += $this->rules['*']; |
380
|
57 |
|
} |
381
|
|
|
|
382
|
|
|
// Add interface calls to the rule. |
383
|
57 |
|
$interfaces = class_implements($nid); |
384
|
57 |
|
foreach ($interfaces as $interface) { |
385
|
42 |
|
if (isset($this->rules[$interface])) { |
386
|
10 |
|
$interfaceRule = $this->rules[$interface]; |
387
|
|
|
|
388
|
10 |
|
if (isset($interfaceRule['inherit']) && $interfaceRule['inherit'] === false) { |
389
|
1 |
|
continue; |
390
|
|
|
} |
391
|
|
|
|
392
|
9 |
|
if (!isset($rule['shared']) && isset($interfaceRule['shared'])) { |
393
|
3 |
|
$rule['shared'] = $interfaceRule['shared']; |
394
|
3 |
|
} |
395
|
|
|
|
396
|
9 |
|
if (!isset($rule['constructorArgs']) && isset($interfaceRule['constructorArgs'])) { |
397
|
3 |
|
$rule['constructorArgs'] = $interfaceRule['constructorArgs']; |
398
|
3 |
|
} |
399
|
|
|
|
400
|
9 |
|
if (!empty($interfaceRule['calls'])) { |
401
|
2 |
|
$rule['calls'] = array_merge( |
402
|
2 |
|
isset($rule['calls']) ? $rule['calls'] : [], |
403
|
2 |
|
$interfaceRule['calls'] |
404
|
2 |
|
); |
405
|
2 |
|
} |
406
|
9 |
|
} |
407
|
57 |
|
} |
408
|
65 |
|
} elseif (!empty($this->rules['*']['inherit'])) { |
409
|
|
|
// Add the default rule. |
410
|
10 |
|
$rule += $this->rules['*']; |
411
|
10 |
|
} |
412
|
|
|
|
413
|
65 |
|
return $rule; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Make a function that creates objects from a rule. |
418
|
|
|
* |
419
|
|
|
* @param string $nid The normalized ID of the container item. |
420
|
|
|
* @param array $rule The resolved rule for the ID. |
421
|
|
|
* @return \Closure Returns a function that when called will create a new instance of the class. |
422
|
|
|
* @throws NotFoundException No entry was found for this identifier. |
423
|
|
|
*/ |
424
|
43 |
|
private function makeFactory($nid, array $rule) { |
425
|
43 |
|
$className = empty($rule['class']) ? $nid : $rule['class']; |
426
|
|
|
|
427
|
43 |
|
if (!empty($rule['factory'])) { |
428
|
|
|
// The instance is created with a user-supplied factory function. |
429
|
6 |
|
$callback = $rule['factory']; |
430
|
6 |
|
$function = $this->reflectCallback($callback); |
431
|
|
|
|
432
|
6 |
|
if ($function->getNumberOfParameters() > 0) { |
433
|
3 |
|
$callbackArgs = $this->makeDefaultArgs($function, (array)$rule['constructorArgs'], $rule); |
434
|
|
|
$factory = function ($args) use ($callback, $callbackArgs) { |
435
|
3 |
|
return call_user_func_array($callback, $this->resolveArgs($callbackArgs, $args)); |
436
|
3 |
|
}; |
437
|
3 |
|
} else { |
438
|
3 |
|
$factory = $callback; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
// If a class is specified then still reflect on it so that calls can be made against it. |
442
|
6 |
|
if (class_exists($className)) { |
443
|
1 |
|
$class = new \ReflectionClass($className); |
444
|
1 |
|
} |
445
|
6 |
|
} else { |
446
|
|
|
// The instance is created by newing up a class. |
447
|
37 |
|
if (!class_exists($className)) { |
448
|
1 |
|
throw new NotFoundException("Class $className does not exist.", 404); |
449
|
|
|
} |
450
|
36 |
|
$class = new \ReflectionClass($className); |
451
|
36 |
|
$constructor = $class->getConstructor(); |
452
|
|
|
|
453
|
36 |
|
if ($constructor && $constructor->getNumberOfParameters() > 0) { |
454
|
33 |
|
$constructorArgs = $this->makeDefaultArgs($constructor, (array)$rule['constructorArgs'], $rule); |
455
|
|
|
|
456
|
|
|
$factory = function ($args) use ($className, $constructorArgs) { |
457
|
33 |
|
return new $className(...array_values($this->resolveArgs($constructorArgs, $args))); |
458
|
33 |
|
}; |
459
|
33 |
|
} else { |
460
|
|
|
$factory = function () use ($className) { |
461
|
5 |
|
return new $className; |
462
|
5 |
|
}; |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
// Add calls to the factory. |
467
|
42 |
|
if (isset($class) && !empty($rule['calls'])) { |
468
|
4 |
|
$calls = []; |
469
|
|
|
|
470
|
|
|
// Generate the calls array. |
471
|
4 |
|
foreach ($rule['calls'] as $call) { |
472
|
4 |
|
list($methodName, $args) = $call; |
473
|
4 |
|
$method = $class->getMethod($methodName); |
474
|
4 |
|
$calls[] = [$methodName, $this->makeDefaultArgs($method, $args, $rule)]; |
475
|
4 |
|
} |
476
|
|
|
|
477
|
|
|
// Wrap the factory in one that makes the calls. |
478
|
|
|
$factory = function ($args) use ($factory, $calls) { |
479
|
4 |
|
$instance = $factory($args); |
480
|
|
|
|
481
|
4 |
|
foreach ($calls as $call) { |
482
|
4 |
|
call_user_func_array( |
483
|
4 |
|
[$instance, $call[0]], |
484
|
4 |
|
$this->resolveArgs($call[1], [], $instance) |
485
|
4 |
|
); |
486
|
4 |
|
} |
487
|
|
|
|
488
|
4 |
|
return $instance; |
489
|
4 |
|
}; |
490
|
4 |
|
} |
491
|
|
|
|
492
|
42 |
|
return $factory; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Create a shared instance of a class from a rule. |
497
|
|
|
* |
498
|
|
|
* This method has the side effect of adding the new instance to the internal instances array of this object. |
499
|
|
|
* |
500
|
|
|
* @param string $nid The normalized ID of the container item. |
501
|
|
|
* @param array $rule The resolved rule for the ID. |
502
|
|
|
* @param array $args Additional arguments passed during creation. |
503
|
|
|
* @return object Returns the the new instance. |
|
|
|
|
504
|
|
|
* @throws NotFoundException Throws an exception if the class does not exist. |
505
|
|
|
*/ |
506
|
23 |
|
private function createSharedInstance($nid, array $rule, array $args) { |
507
|
23 |
|
if (!empty($rule['factory'])) { |
508
|
|
|
// The instance is created with a user-supplied factory function. |
509
|
3 |
|
$callback = $rule['factory']; |
510
|
3 |
|
$function = $this->reflectCallback($callback); |
511
|
|
|
|
512
|
3 |
|
if ($function->getNumberOfParameters() > 0) { |
513
|
1 |
|
$callbackArgs = $this->resolveArgs( |
514
|
1 |
|
$this->makeDefaultArgs($function, (array)$rule['constructorArgs'], $rule), |
515
|
|
|
$args |
516
|
1 |
|
); |
517
|
|
|
|
518
|
1 |
|
$this->instances[$nid] = null; // prevent cyclic dependency from infinite loop. |
519
|
1 |
|
$this->instances[$nid] = $instance = call_user_func_array($callback, $callbackArgs); |
520
|
1 |
|
} else { |
521
|
2 |
|
$this->instances[$nid] = $instance = $callback(); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
// Reflect on the instance so that calls can be made against it. |
525
|
3 |
|
if (is_object($instance)) { |
526
|
2 |
|
$class = new \ReflectionClass(get_class($instance)); |
527
|
2 |
|
} |
528
|
3 |
|
} else { |
529
|
20 |
|
$className = empty($rule['class']) ? $nid : $rule['class']; |
530
|
20 |
|
if (!class_exists($className)) { |
531
|
1 |
|
throw new NotFoundException("Class $className does not exist.", 404); |
532
|
|
|
} |
533
|
19 |
|
$class = new \ReflectionClass($className); |
534
|
19 |
|
$constructor = $class->getConstructor(); |
535
|
|
|
|
536
|
19 |
|
if ($constructor && $constructor->getNumberOfParameters() > 0) { |
537
|
|
|
// Instantiate the object first so that this instance can be used for cyclic dependencies. |
538
|
18 |
|
$this->instances[$nid] = $instance = $class->newInstanceWithoutConstructor(); |
539
|
|
|
|
540
|
18 |
|
$constructorArgs = $this->resolveArgs( |
541
|
18 |
|
$this->makeDefaultArgs($constructor, (array)$rule['constructorArgs'], $rule), |
542
|
|
|
$args |
543
|
18 |
|
); |
544
|
17 |
|
$constructor->invokeArgs($instance, $constructorArgs); |
545
|
17 |
|
} else { |
546
|
3 |
|
$this->instances[$nid] = $instance = new $class->name; |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
// Call subsequent calls on the new object. |
551
|
21 |
|
if (isset($class) && !empty($rule['calls'])) { |
552
|
2 |
|
foreach ($rule['calls'] as $call) { |
553
|
2 |
|
list($methodName, $args) = $call; |
554
|
2 |
|
$method = $class->getMethod($methodName); |
555
|
|
|
|
556
|
2 |
|
$args = $this->resolveArgs( |
557
|
2 |
|
$this->makeDefaultArgs($method, $args, $rule), |
558
|
2 |
|
[], |
559
|
|
|
$instance |
560
|
2 |
|
); |
561
|
|
|
|
562
|
2 |
|
$method->invokeArgs($instance, $args); |
563
|
2 |
|
} |
564
|
2 |
|
} |
565
|
|
|
|
566
|
21 |
|
return $instance; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Make an array of default arguments for a given function. |
571
|
|
|
* |
572
|
|
|
* @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
573
|
|
|
* @param array $ruleArgs An array of default arguments specifically for the function. |
574
|
|
|
* @param array $rule The entire rule. |
575
|
|
|
* @return array Returns an array in the form `name => defaultValue`. |
576
|
|
|
*/ |
577
|
58 |
|
private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
|
|
|
578
|
58 |
|
$ruleArgs = array_change_key_case($ruleArgs); |
579
|
58 |
|
$result = []; |
580
|
|
|
|
581
|
58 |
|
$pos = 0; |
582
|
58 |
|
foreach ($function->getParameters() as $i => $param) { |
583
|
58 |
|
$name = strtolower($param->name); |
584
|
|
|
|
585
|
58 |
|
if (array_key_exists($name, $ruleArgs)) { |
586
|
3 |
|
$value = $ruleArgs[$name]; |
587
|
58 |
|
} elseif ($param->getClass() |
588
|
56 |
|
&& !(isset($ruleArgs[$pos]) && is_object($ruleArgs[$pos]) && get_class($ruleArgs[$pos]) === $param->getClass()->name) |
589
|
56 |
|
&& ($param->getClass()->isInstantiable() || isset($this->rules[$param->getClass()->name]) || array_key_exists($param->getClass()->name, $this->instances)) |
590
|
56 |
|
) { |
591
|
10 |
|
$value = new DefaultReference($this->normalizeID($param->getClass()->name)); |
592
|
56 |
|
} elseif (array_key_exists($pos, $ruleArgs)) { |
593
|
24 |
|
$value = $ruleArgs[$pos]; |
594
|
24 |
|
$pos++; |
595
|
55 |
|
} elseif ($param->isDefaultValueAvailable()) { |
596
|
28 |
|
$value = $param->getDefaultValue(); |
597
|
28 |
|
} else { |
598
|
7 |
|
$value = new RequiredParameter($param); |
599
|
|
|
} |
600
|
|
|
|
601
|
58 |
|
$result[$name] = $value; |
602
|
58 |
|
} |
603
|
|
|
|
604
|
58 |
|
return $result; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Replace an array of default args with called args. |
609
|
|
|
* |
610
|
|
|
* @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
611
|
|
|
* @param array $args The arguments passed into a creation. |
612
|
|
|
* @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
613
|
|
|
* @return array Returns an array suitable to be applied to a function call. |
614
|
|
|
* @throws MissingArgumentException Throws an exception when a required parameter is missing. |
615
|
|
|
*/ |
616
|
58 |
|
private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
617
|
|
|
// First resolve all passed arguments so their types are known. |
618
|
58 |
|
$args = array_map( |
619
|
58 |
|
function ($arg) use ($instance) { |
620
|
15 |
|
return $arg instanceof ReferenceInterface ? $arg->resolve($this, $instance) : $arg; |
621
|
58 |
|
}, |
622
|
58 |
|
array_change_key_case($args) |
623
|
58 |
|
); |
624
|
|
|
|
625
|
58 |
|
$pos = 0; |
626
|
58 |
|
foreach ($defaultArgs as $name => &$default) { |
627
|
58 |
|
if (array_key_exists($name, $args)) { |
628
|
|
|
// This is a named arg and should be used. |
629
|
2 |
|
$value = $args[$name]; |
630
|
58 |
|
} elseif (isset($args[$pos]) && (!($default instanceof DefaultReference) || empty($default->getClass()) || is_a($args[$pos], $default->getClass()))) { |
631
|
|
|
// There is an arg at this position and it's the same type as the default arg or the default arg is typeless. |
632
|
13 |
|
$value = $args[$pos]; |
633
|
13 |
|
$pos++; |
634
|
13 |
|
} else { |
635
|
|
|
// There is no passed arg, so use the default arg. |
636
|
47 |
|
$value = $default; |
637
|
|
|
} |
638
|
|
|
|
639
|
58 |
|
if ($value instanceof ReferenceInterface) { |
640
|
14 |
|
$value = $value->resolve($this, $instance); |
641
|
12 |
|
} |
642
|
|
|
|
643
|
56 |
|
$default = $value; |
644
|
56 |
|
} |
645
|
|
|
|
646
|
56 |
|
return $defaultArgs; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Create an instance of a container item. |
651
|
|
|
* |
652
|
|
|
* This method either creates a new instance or returns an already created shared instance. |
653
|
|
|
* |
654
|
|
|
* @param string $nid The normalized ID of the container item. |
655
|
|
|
* @param array $args Additional arguments to pass to the constructor. |
656
|
|
|
* @return object Returns an object instance. |
657
|
|
|
*/ |
658
|
65 |
|
private function createInstance($nid, array $args) { |
659
|
65 |
|
$rule = $this->makeRule($nid); |
660
|
|
|
|
661
|
|
|
// Cache the instance or its factory for future use. |
662
|
65 |
|
if (empty($rule['shared'])) { |
663
|
43 |
|
$factory = $this->makeFactory($nid, $rule); |
664
|
42 |
|
$instance = $factory($args); |
665
|
41 |
|
$this->factories[$nid] = $factory; |
666
|
41 |
|
} else { |
667
|
23 |
|
$instance = $this->createSharedInstance($nid, $rule, $args); |
668
|
|
|
} |
669
|
61 |
|
return $instance; |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* Call a callback with argument injection. |
674
|
|
|
* |
675
|
|
|
* @param callable $callback The callback to call. |
676
|
|
|
* @param array $args Additional arguments to pass to the callback. |
677
|
|
|
* @return mixed Returns the result of the callback. |
678
|
|
|
* @throws ContainerException Throws an exception if the callback cannot be understood. |
679
|
|
|
*/ |
680
|
3 |
|
public function call(callable $callback, array $args = []) { |
681
|
3 |
|
$instance = null; |
682
|
|
|
|
683
|
3 |
|
if (is_array($callback)) { |
684
|
1 |
|
$function = new \ReflectionMethod($callback[0], $callback[1]); |
685
|
|
|
|
686
|
1 |
|
if (is_object($callback[0])) { |
687
|
1 |
|
$instance = $callback[0]; |
688
|
1 |
|
} |
689
|
1 |
|
} else { |
690
|
2 |
|
$function = new \ReflectionFunction($callback); |
691
|
|
|
} |
692
|
|
|
|
693
|
3 |
|
$args = $this->resolveArgs($this->makeDefaultArgs($function, $args), [], $instance); |
694
|
|
|
|
695
|
3 |
|
return call_user_func_array($callback, $args); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
700
|
|
|
* |
701
|
|
|
* @param string $id Identifier of the entry to look for. |
702
|
|
|
* |
703
|
|
|
* @return boolean |
704
|
|
|
*/ |
705
|
5 |
|
public function has($id) { |
706
|
5 |
|
$id = $this->normalizeID($id); |
707
|
|
|
|
708
|
5 |
|
return isset($this->instances[$id]) || !empty($this->rules[$id]) || class_exists($id); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Determines whether a rule has been defined at a given ID. |
713
|
|
|
* |
714
|
|
|
* @param string $id Identifier of the entry to look for. |
715
|
|
|
* @return bool Returns **true** if a rule has been defined or **false** otherwise. |
716
|
|
|
*/ |
717
|
4 |
|
public function hasRule($id) { |
718
|
4 |
|
$id = $this->normalizeID($id); |
719
|
4 |
|
return !empty($this->rules[$id]); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Returns true if the container already has an instance for the given identifier. Returns false otherwise. |
724
|
|
|
* |
725
|
|
|
* @param string $id Identifier of the entry to look for. |
726
|
|
|
* |
727
|
|
|
* @return bool |
728
|
|
|
*/ |
729
|
1 |
|
public function hasInstance($id) { |
730
|
1 |
|
$id = $this->normalizeID($id); |
731
|
|
|
|
732
|
1 |
|
return isset($this->instances[$id]); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Finds an entry of the container by its identifier and returns it. |
737
|
|
|
* |
738
|
|
|
* @param string $id Identifier of the entry to look for. |
739
|
|
|
* |
740
|
|
|
* @throws NotFoundException No entry was found for this identifier. |
741
|
|
|
* @throws ContainerException Error while retrieving the entry. |
742
|
|
|
* |
743
|
|
|
* @return mixed Entry. |
744
|
|
|
*/ |
745
|
57 |
|
public function get($id) { |
746
|
57 |
|
return $this->getArgs($id); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Determine the reflection information for a callback. |
751
|
|
|
* |
752
|
|
|
* @param callable $callback The callback to reflect. |
753
|
|
|
* @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
754
|
|
|
*/ |
755
|
9 |
|
private function reflectCallback(callable $callback) { |
756
|
9 |
|
if (is_array($callback)) { |
757
|
2 |
|
return new \ReflectionMethod($callback[0], $callback[1]); |
758
|
|
|
} else { |
759
|
7 |
|
return new \ReflectionFunction($callback); |
760
|
|
|
} |
761
|
|
|
} |
762
|
|
|
} |
763
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.