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