Completed
Push — master ( 02dbf7...209aa4 )
by Todd
01:43
created

Container::resolveArgs()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 22
cts 22
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 18
nc 7
nop 3
crap 9
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 85
    public function __construct() {
26 85
        $this->rules = ['*' => ['inherit' => true, 'constructorArgs' => null]];
27 85
        $this->instances = [];
28 85
        $this->factories = [];
29
30 85
        $this->rule('*');
31 85
    }
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
     * Deep clone an array.
43
     *
44
     * @param array $array The array to clone.
45
     * @return array Returns the cloned array.
46
     * @see http://stackoverflow.com/a/17729234
47
     */
48 2
    private function arrayClone(array $array) {
49
        return array_map(function ($element) {
50 1
            return ((is_array($element))
51 1
                ? $this->arrayClone($element)
52 1
                : ((is_object($element))
53 1
                    ? clone $element
54
                    : $element
55
                )
56 2
            );
57 1
        }, $array);
58
    }
59
60
    /**
61
     * Normalize a container entry ID.
62
     *
63
     * @param string $id The ID to normalize.
64
     * @return string Returns a normalized ID as a string.
65
     */
66 86
    private function normalizeID($id) {
67 86
        return ltrim($id, '\\');
68
    }
69
70
    /**
71
     * Set the current rule to the default rule.
72
     *
73
     * @return $this
74
     */
75 1
    public function defaultRule() {
76 1
        return $this->rule('*');
77
    }
78
79
    /**
80
     * Set the current rule.
81
     *
82
     * @param string $id The ID of the rule.
83
     * @return $this
84
     */
85 85
    public function rule($id) {
86 85
        $id = $this->normalizeID($id);
87
88 85
        if (!isset($this->rules[$id])) {
89 41
            $this->rules[$id] = [];
90 41
        }
91 85
        $this->currentRuleName = $id;
92 85
        $this->currentRule = &$this->rules[$id];
93
94 85
        return $this;
95
    }
96
97
    /**
98
     * Get the class name of the current rule.
99
     *
100
     * @return string Returns a class name.
101
     */
102 2
    public function getClass() {
103 2
        return empty($this->currentRule['class']) ? '' : $this->currentRule['class'];
104
    }
105
106
    /**
107
     * Set the name of the class for the current rule.
108
     *
109
     * @param string $className A valid class name.
110
     * @return $this
111
     */
112 6
    public function setClass($className) {
113 6
        $this->currentRule['class'] = $className;
114 6
        return $this;
115
    }
116
117
    /**
118
     * Get the rule that the current rule references.
119
     *
120
     * @return string Returns a reference name or an empty string if there is no reference.
121
     */
122 3
    public function getAliasOf() {
123 3
        return empty($this->currentRule['aliasOf']) ? '' : $this->currentRule['aliasOf'];
124
    }
125
126
    /**
127
     * Set the rule that the current rule is an alias of.
128
     *
129
     * @param string $alias The name of an entry in the container to point to.
130
     * @return $this
131
     */
132 4
    public function setAliasOf($alias) {
133 4
        $alias = $this->normalizeID($alias);
134
135 4
        if ($alias === $this->currentRuleName) {
136 1
            trigger_error("You cannot set alias '$alias' to itself.", E_USER_NOTICE);
137 1
        } else {
138 3
            $this->currentRule['aliasOf'] = $alias;
139
        }
140 4
        return $this;
141
    }
142
143
    /**
144
     * Add an alias of the current rule.
145
     *
146
     * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item
147
     * with the current rule. If the current rule is shared then the same shared instance will be returned.
148
     *
149
     * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE**
150
     * level error is raised and the alias is not added.
151
     *
152
     * @param string $alias The alias to set.
153
     * @return $this
154
     */
155 7
    public function addAlias($alias) {
156 7
        $alias = $this->normalizeID($alias);
157
158 7
        if ($alias === $this->currentRuleName) {
159 1
            trigger_error("Tried to set alias '$alias' to self.", E_USER_NOTICE);
160 1
        } else {
161 6
            $this->rules[$alias]['aliasOf'] = $this->currentRuleName;
162
        }
163 7
        return $this;
164
    }
165
166
    /**
167
     * Remove an alias of the current rule.
168
     *
169
     * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE**
170
     * level error is raised, but the alias is still removed.
171
     *
172
     * @param string $alias The alias to remove.
173
     * @return $this
174
     */
175 2
    public function removeAlias($alias) {
176 2
        $alias = $this->normalizeID($alias);
177
178 2
        if (!empty($this->rules[$alias]['aliasOf']) && $this->rules[$alias]['aliasOf'] !== $this->currentRuleName) {
179 1
            trigger_error("Alias '$alias' does not point to the current rule.", E_USER_NOTICE);
180 1
        }
181
182 2
        unset($this->rules[$alias]['aliasOf']);
183 2
        return $this;
184
    }
185
186
    /**
187
     * Get all of the aliases of the current rule.
188
     *
189
     * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array.
190
     *
191
     * @return array Returns an array of strings representing aliases.
192
     */
193 6
    public function getAliases() {
194 6
        $result = [];
195
196 6
        foreach ($this->rules as $name => $rule) {
197 6
            if (!empty($rule['aliasOf']) && $rule['aliasOf'] === $this->currentRuleName) {
198 4
                $result[] = $name;
199 4
            }
200 6
        }
201
202 6
        return $result;
203
    }
204
205
    /**
206
     * Get the factory callback for the current rule.
207
     *
208
     * @return callable|null Returns the rule's factory or **null** if it has none.
209
     */
210 2
    public function getFactory() {
211 2
        return isset($this->currentRule['factory']) ? $this->currentRule['factory'] : null;
212
    }
213
214
    /**
215
     * Set the factory that will be used to create the instance for the current rule.
216
     *
217
     * @param callable $factory This callback will be called to create the instance for the rule.
218
     * @return $this
219
     */
220 10
    public function setFactory(callable $factory) {
221 10
        $this->currentRule['factory'] = $factory;
222 10
        return $this;
223
    }
224
225
    /**
226
     * Whether or not the current rule is shared.
227
     *
228
     * @return bool Returns **true** if the rule is shared or **false** otherwise.
229
     */
230 2
    public function isShared() {
231 2
        return !empty($this->currentRule['shared']);
232
    }
233
234
    /**
235
     * Set whether or not the current rule is shared.
236
     *
237
     * @param bool $shared Whether or not the current rule is shared.
238
     * @return $this
239
     */
240 38
    public function setShared($shared) {
241 38
        $this->currentRule['shared'] = $shared;
242 38
        return $this;
243
    }
244
245
    /**
246
     * Whether or not the current rule will inherit to subclasses.
247
     *
248
     * @return bool Returns **true** if the current rule inherits or **false** otherwise.
249
     */
250 2
    public function getInherit() {
251 2
        return !empty($this->currentRule['inherit']);
252
    }
253
254
    /**
255
     * Set whether or not the current rule extends to subclasses.
256
     *
257
     * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise.
258
     * @return $this
259
     */
260 3
    public function setInherit($inherit) {
261 3
        $this->currentRule['inherit'] = $inherit;
262 3
        return $this;
263
    }
264
265
    /**
266
     * Get the constructor arguments for the current rule.
267
     *
268
     * @return array Returns the constructor arguments for the current rule.
269
     */
270 2
    public function getConstructorArgs() {
271 2
        return empty($this->currentRule['constructorArgs']) ? [] : $this->currentRule['constructorArgs'];
272
    }
273
274
    /**
275
     * Set the constructor arguments for the current rule.
276
     *
277
     * @param array $args An array of constructor arguments.
278
     * @return $this
279
     */
280 24
    public function setConstructorArgs(array $args) {
281 24
        $this->currentRule['constructorArgs'] = $args;
282 24
        return $this;
283
    }
284
285
    /**
286
     * Set a specific shared instance into the container.
287
     *
288
     * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a
289
     * rule is configured that says that instances should not be shared.
290
     *
291
     * @param string $name The name of the container entry.
292
     * @param mixed $instance This instance.
293
     * @return $this
294
     */
295 9
    public function setInstance($name, $instance) {
296 9
        $this->instances[$this->normalizeID($name)] = $instance;
297 9
        return $this;
298
    }
299
300
    /**
301
     * Add a method call to a rule.
302
     *
303
     * @param string $method The name of the method to call.
304
     * @param array $args The arguments to pass to the method.
305
     * @return $this
306
     */
307 6
    public function addCall($method, array $args = []) {
308 6
        $this->currentRule['calls'][] = [$method, $args];
309
310 6
        return $this;
311
    }
312
313
    /**
314
     * Finds an entry of the container by its identifier and returns it.
315
     *
316
     * @param string $id Identifier of the entry to look for.
317
     * @param array $args Additional arguments to pass to the constructor.
318
     *
319
     * @throws NotFoundException No entry was found for this identifier.
320
     * @throws ContainerException Error while retrieving the entry.
321
     *
322
     * @return mixed Entry.
323
     */
324 66
    public function getArgs($id, array $args = []) {
325 66
        $id = $this->normalizeID($id);
326
327 66
        if (isset($this->instances[$id])) {
328
            // A shared instance just gets returned.
329 16
            return $this->instances[$id];
330
        }
331
332 62
        if (isset($this->factories[$id])) {
333
            // The factory for this object type is already there so call it to create the instance.
334 3
            return $this->factories[$id]($args);
335
        }
336
337 62
        if (!empty($this->rules[$id]['aliasOf'])) {
338
            // This rule references another rule.
339 2
            return $this->getArgs($this->rules[$id]['aliasOf'], $args);
340
        }
341
342
        // The factory or instance isn't registered so do that now.
343
        // This call also caches the instance or factory fo faster access next time.
344 62
        return $this->createInstance($id, $args);
345
    }
346
347
    /**
348
     * Make a rule based on an ID.
349
     *
350
     * @param string $nid A normalized ID.
351
     * @return array Returns an array representing a rule.
352
     */
353 62
    private function makeRule($nid) {
354 62
        $rule = isset($this->rules[$nid]) ? $this->rules[$nid] : [];
355
356 62
        if (class_exists($nid)) {
357 54
            for ($class = get_parent_class($nid); !empty($class); $class = get_parent_class($class)) {
358
                // Don't add the rule if it doesn't say to inherit.
359 6
                if (!isset($this->rules[$class]) || (isset($this->rules[$class]['inherit']) && !$this->rules[$class]['inherit'])) {
360 4
                    break;
361
                }
362 2
                $rule += $this->rules[$class];
363 2
            }
364
365
            // Add the default rule.
366 54
            if (!empty($this->rules['*']['inherit'])) {
367 54
                $rule += $this->rules['*'];
368 54
            }
369
370
            // Add interface calls to the rule.
371 54
            $interfaces = class_implements($nid);
372 54
            foreach ($interfaces as $interface) {
373 39
                if (isset($this->rules[$interface])) {
374 10
                    $interfaceRule = $this->rules[$interface];
375
376 10
                    if (isset($interfaceRule['inherit']) && $interfaceRule['inherit'] === false) {
377 1
                        continue;
378
                    }
379
380 9
                    if (!isset($rule['shared']) && isset($interfaceRule['shared'])) {
381 3
                        $rule['shared'] = $interfaceRule['shared'];
382 3
                    }
383
384 9
                    if (!isset($rule['constructorArgs']) && isset($interfaceRule['constructorArgs'])) {
385 3
                        $rule['constructorArgs'] = $interfaceRule['constructorArgs'];
386 3
                    }
387
388 9
                    if (!empty($interfaceRule['calls'])) {
389 2
                        $rule['calls'] = array_merge(
390 2
                            isset($rule['calls']) ? $rule['calls'] : [],
391 2
                            $interfaceRule['calls']
392 2
                        );
393 2
                    }
394 9
                }
395 54
            }
396 62
        } elseif (!empty($this->rules['*']['inherit'])) {
397
            // Add the default rule.
398 10
            $rule += $this->rules['*'];
399 10
        }
400
401 62
        return $rule;
402
    }
403
404
    /**
405
     * Make a function that creates objects from a rule.
406
     *
407
     * @param string $nid The normalized ID of the container item.
408
     * @param array $rule The resolved rule for the ID.
409
     * @return \Closure Returns a function that when called will create a new instance of the class.
410
     * @throws NotFoundException No entry was found for this identifier.
411
     */
412 40
    private function makeFactory($nid, array $rule) {
413 40
        $className = empty($rule['class']) ? $nid : $rule['class'];
414
415 40
        if (!empty($rule['factory'])) {
416
            // The instance is created with a user-supplied factory function.
417 6
            $callback = $rule['factory'];
418 6
            $function = $this->reflectCallback($callback);
419
420 6
            if ($function->getNumberOfParameters() > 0) {
421 3
                $callbackArgs = $this->makeDefaultArgs($function, (array)$rule['constructorArgs'], $rule);
422
                $factory = function ($args) use ($callback, $callbackArgs) {
423 3
                    return call_user_func_array($callback, $this->resolveArgs($callbackArgs, $args));
424 3
                };
425 3
            } else {
426 3
                $factory = $callback;
427
            }
428
429
            // If a class is specified then still reflect on it so that calls can be made against it.
430 6
            if (class_exists($className)) {
431 1
                $class = new \ReflectionClass($className);
432 1
            }
433 6
        } else {
434
            // The instance is created by newing up a class.
435 34
            if (!class_exists($className)) {
436 1
                throw new NotFoundException("Class $className does not exist.", 404);
437
            }
438 33
            $class = new \ReflectionClass($className);
439 33
            $constructor = $class->getConstructor();
440
441 33
            if ($constructor && $constructor->getNumberOfParameters() > 0) {
442 30
                $constructorArgs = $this->makeDefaultArgs($constructor, (array)$rule['constructorArgs'], $rule);
443
444
                $factory = function ($args) use ($className, $constructorArgs) {
445 30
                    return new $className(...array_values($this->resolveArgs($constructorArgs, $args)));
446 30
                };
447 30
            } else {
448
                $factory = function () use ($className) {
449 5
                    return new $className;
450 5
                };
451
            }
452
        }
453
454
        // Add calls to the factory.
455 39
        if (isset($class) && !empty($rule['calls'])) {
456 4
            $calls = [];
457
458
            // Generate the calls array.
459 4
            foreach ($rule['calls'] as $call) {
460 4
                list($methodName, $args) = $call;
461 4
                $method = $class->getMethod($methodName);
462 4
                $calls[] = [$methodName, $this->makeDefaultArgs($method, $args, $rule)];
463 4
            }
464
465
            // Wrap the factory in one that makes the calls.
466
            $factory = function ($args) use ($factory, $calls) {
467 4
                $instance = $factory($args);
468
469 4
                foreach ($calls as $call) {
470 4
                    call_user_func_array(
471 4
                        [$instance, $call[0]],
472 4
                        $this->resolveArgs($call[1], [], $instance)
473 4
                    );
474 4
                }
475
476 4
                return $instance;
477 4
            };
478 4
        }
479
480 39
        return $factory;
481
    }
482
483
    /**
484
     * Create a shared instance of a class from a rule.
485
     *
486
     * This method has the side effect of adding the new instance to the internal instances array of this object.
487
     *
488
     * @param string $nid The normalized ID of the container item.
489
     * @param array $rule The resolved rule for the ID.
490
     * @param array $args Additional arguments passed during creation.
491
     * @return object Returns the the new instance.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double|string|nu...boolean|resource|object?

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.

Loading history...
492
     * @throws NotFoundException Throws an exception if the class does not exist.
493
     */
494 23
    private function createSharedInstance($nid, array $rule, array $args) {
495 23
        if (!empty($rule['factory'])) {
496
            // The instance is created with a user-supplied factory function.
497 3
            $callback = $rule['factory'];
498 3
            $function = $this->reflectCallback($callback);
499
500 3
            if ($function->getNumberOfParameters() > 0) {
501 1
                $callbackArgs = $this->resolveArgs(
502 1
                    $this->makeDefaultArgs($function, (array)$rule['constructorArgs'], $rule),
503
                    $args
504 1
                );
505
506 1
                $this->instances[$nid] = null; // prevent cyclic dependency from infinite loop.
507 1
                $this->instances[$nid] = $instance = call_user_func_array($callback, $callbackArgs);
508 1
            } else {
509 2
                $this->instances[$nid] = $instance = $callback();
510
            }
511
512
            // Reflect on the instance so that calls can be made against it.
513 3
            if (is_object($instance)) {
514 2
                $class = new \ReflectionClass(get_class($instance));
515 2
            }
516 3
        } else {
517 20
            $className = empty($rule['class']) ? $nid : $rule['class'];
518 20
            if (!class_exists($className)) {
519 1
                throw new NotFoundException("Class $className does not exist.", 404);
520
            }
521 19
            $class = new \ReflectionClass($className);
522 19
            $constructor = $class->getConstructor();
523
524 19
            if ($constructor && $constructor->getNumberOfParameters() > 0) {
525
                // Instantiate the object first so that this instance can be used for cyclic dependencies.
526 18
                $this->instances[$nid] = $instance = $class->newInstanceWithoutConstructor();
527
528 18
                $constructorArgs = $this->resolveArgs(
529 18
                    $this->makeDefaultArgs($constructor, (array)$rule['constructorArgs'], $rule),
530
                    $args
531 18
                );
532 17
                $constructor->invokeArgs($instance, $constructorArgs);
533 17
            } else {
534 3
                $this->instances[$nid] = $instance = new $class->name;
535
            }
536
        }
537
538
        // Call subsequent calls on the new object.
539 21
        if (isset($class) && !empty($rule['calls'])) {
540 2
            foreach ($rule['calls'] as $call) {
541 2
                list($methodName, $args) = $call;
542 2
                $method = $class->getMethod($methodName);
543
544 2
                $args = $this->resolveArgs(
545 2
                    $this->makeDefaultArgs($method, $args, $rule),
546 2
                    [],
547
                    $instance
548 2
                );
549
550 2
                $method->invokeArgs($instance, $args);
551 2
            }
552 2
        }
553
554 21
        return $instance;
555
    }
556
557
    /**
558
     * Make an array of default arguments for a given function.
559
     *
560
     * @param \ReflectionFunctionAbstract $function The function to make the arguments for.
561
     * @param array $ruleArgs An array of default arguments specifically for the function.
562
     * @param array $rule The entire rule.
563
     * @return array Returns an array in the form `name => defaultValue`.
564
     */
565 55
    private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
566 55
        $ruleArgs = array_change_key_case($ruleArgs);
567 55
        $result = [];
568
569 55
        $pos = 0;
570 55
        foreach ($function->getParameters() as $i => $param) {
571 55
            $name = strtolower($param->name);
572
573 55
            if (array_key_exists($name, $ruleArgs)) {
574 2
                $value = $ruleArgs[$name];
575 55
            } elseif ($param->getClass()
576 53
                && !(isset($ruleArgs[$pos]) && is_object($ruleArgs[$pos]) && get_class($ruleArgs[$pos]) === $param->getClass()->name)
577 53
                && ($param->getClass()->isInstantiable() || isset($this->rules[$param->getClass()->name]) || array_key_exists($param->getClass()->name, $this->instances))
578 53
            ) {
579 9
                $value = new DefaultReference($this->normalizeID($param->getClass()->name));
580 53
            } elseif (array_key_exists($pos, $ruleArgs)) {
581 24
                $value = $ruleArgs[$pos];
582 24
                $pos++;
583 52
            } elseif ($param->isDefaultValueAvailable()) {
584 25
                $value = $param->getDefaultValue();
585 25
            } else {
586 7
                $value = new RequiredParameter($param);
587
            }
588
589 55
            $result[$name] = $value;
590 55
        }
591
592 55
        return $result;
593
    }
594
595
    /**
596
     * Replace an array of default args with called args.
597
     *
598
     * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}.
599
     * @param array $args The arguments passed into a creation.
600
     * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object.
601
     * @return array Returns an array suitable to be applied to a function call.
602
     * @throws MissingArgumentException Throws an exception when a required parameter is missing.
603
     */
604 55
    private function resolveArgs(array $defaultArgs, array $args, $instance = null) {
605
        // First resolve all passed arguments so their types are known.
606 55
        $args = array_map(
607 55
            function ($arg) use ($instance) {
608 14
                return $arg instanceof ReferenceInterface ? $arg->resolve($this, $instance) : $arg;
609 55
            },
610 55
            array_change_key_case($args)
611 55
        );
612
613 55
        $pos = 0;
614 55
        foreach ($defaultArgs as $name => &$default) {
615 55
            if (array_key_exists($name, $args)) {
616
                // This is a named arg and should be used.
617 2
                $value = $args[$name];
618 55
            } elseif (isset($args[$pos]) && (!($default instanceof DefaultReference) || empty($default->getClass()) || is_a($args[$pos], $default->getClass()))) {
619
                // There is an arg at this position and it's the same type as the default arg or the default arg is typeless.
620 12
                $value = $args[$pos];
621 12
                $pos++;
622 12
            } else {
623
                // There is no passed arg, so use the default arg.
624 44
                $value = $default;
625
            }
626
627 55
            if ($value instanceof ReferenceInterface) {
628 13
                $value = $value->resolve($this, $instance);
629 11
            }
630
631 53
            $default = $value;
632 53
        }
633
634 53
        return $defaultArgs;
635
    }
636
637
    /**
638
     * Create an instance of a container item.
639
     *
640
     * This method either creates a new instance or returns an already created shared instance.
641
     *
642
     * @param string $nid The normalized ID of the container item.
643
     * @param array $args Additional arguments to pass to the constructor.
644
     * @return object Returns an object instance.
645
     */
646 62
    private function createInstance($nid, array $args) {
647 62
        $rule = $this->makeRule($nid);
648
649
        // Cache the instance or its factory for future use.
650 62
        if (empty($rule['shared'])) {
651 40
            $factory = $this->makeFactory($nid, $rule);
652 39
            $instance = $factory($args);
653 38
            $this->factories[$nid] = $factory;
654 38
        } else {
655 23
            $instance = $this->createSharedInstance($nid, $rule, $args);
656
        }
657 58
        return $instance;
658
    }
659
660
    /**
661
     * Call a callback with argument injection.
662
     *
663
     * @param callable $callback The callback to call.
664
     * @param array $args Additional arguments to pass to the callback.
665
     * @return mixed Returns the result of the callback.
666
     * @throws ContainerException Throws an exception if the callback cannot be understood.
667
     */
668 3
    public function call(callable $callback, array $args = []) {
669 3
        $instance = null;
670
671 3
        if (is_array($callback)) {
672 1
            $function = new \ReflectionMethod($callback[0], $callback[1]);
673
674 1
            if (is_object($callback[0])) {
675 1
                $instance = $callback[0];
676 1
            }
677 1
        } else {
678 2
            $function = new \ReflectionFunction($callback);
679
        }
680
681 3
        $args = $this->resolveArgs($this->makeDefaultArgs($function, $args), [], $instance);
682
683 3
        return call_user_func_array($callback, $args);
684
    }
685
686
    /**
687
     * Returns true if the container can return an entry for the given identifier. Returns false otherwise.
688
     *
689
     * @param string $id Identifier of the entry to look for.
690
     *
691
     * @return boolean
692
     */
693 5
    public function has($id) {
694 5
        $id = $this->normalizeID($id);
695
696 5
        return isset($this->instances[$id]) || !empty($this->rules[$id]) || class_exists($id);
697
    }
698
699
    /**
700
     * Determines whether a rule has been defined at a given ID.
701
     *
702
     * @param string $id Identifier of the entry to look for.
703
     * @return bool Returns **true** if a rule has been defined or **false** otherwise.
704
     */
705 4
    public function hasRule($id) {
706 4
        $id = $this->normalizeID($id);
707 4
        return !empty($this->rules[$id]);
708
    }
709
710
    /**
711
     * Finds an entry of the container by its identifier and returns it.
712
     *
713
     * @param string $id Identifier of the entry to look for.
714
     *
715
     * @throws NotFoundException  No entry was found for this identifier.
716
     * @throws ContainerException Error while retrieving the entry.
717
     *
718
     * @return mixed Entry.
719
     */
720 57
    public function get($id) {
721 57
        return $this->getArgs($id);
722
    }
723
724
    /**
725
     * Determine the reflection information for a callback.
726
     *
727
     * @param callable $callback The callback to reflect.
728
     * @return \ReflectionFunctionAbstract Returns the reflection function for the callback.
729
     */
730 9
    private function reflectCallback(callable $callback) {
731 9
        if (is_array($callback)) {
732 2
            return new \ReflectionMethod($callback[0], $callback[1]);
733
        } else {
734 7
            return new \ReflectionFunction($callback);
735
        }
736
    }
737
}
738