Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / league / container / src / ReflectionContainer.php
1 <?php
2
3 namespace League\Container;
4
5 use League\Container\Argument\ArgumentResolverInterface;
6 use League\Container\Argument\ArgumentResolverTrait;
7 use League\Container\Exception\NotFoundException;
8 use ReflectionClass;
9 use ReflectionFunction;
10 use ReflectionMethod;
11
12 class ReflectionContainer implements
13     ArgumentResolverInterface,
14     ImmutableContainerInterface
15 {
16     use ArgumentResolverTrait;
17     use ImmutableContainerAwareTrait;
18
19     /**
20      * {@inheritdoc}
21      */
22     public function get($alias, array $args = [])
23     {
24         if (! $this->has($alias)) {
25             throw new NotFoundException(
26                 sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $alias)
27             );
28         }
29
30         $reflector = new ReflectionClass($alias);
31         $construct = $reflector->getConstructor();
32
33         if ($construct === null) {
34             return new $alias;
35         }
36
37         return $reflector->newInstanceArgs(
38             $this->reflectArguments($construct, $args)
39         );
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function has($alias)
46     {
47         return class_exists($alias);
48     }
49
50     /**
51      * Invoke a callable via the container.
52      *
53      * @param  callable $callable
54      * @param  array    $args
55      * @return mixed
56      */
57     public function call(callable $callable, array $args = [])
58     {
59         if (is_string($callable) && strpos($callable, '::') !== false) {
60             $callable = explode('::', $callable);
61         }
62
63         if (is_array($callable)) {
64             if (is_string($callable[0])) {
65                 $callable[0] = $this->getContainer()->get($callable[0]);
66             }
67
68             $reflection = new ReflectionMethod($callable[0], $callable[1]);
69
70             if ($reflection->isStatic()) {
71                 $callable[0] = null;
72             }
73
74             return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
75         }
76
77         if (is_object($callable)) {
78             $reflection = new ReflectionMethod($callable, '__invoke');
79
80             return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
81         }
82
83         $reflection = new ReflectionFunction($callable);
84
85         return $reflection->invokeArgs($this->reflectArguments($reflection, $args));
86     }
87 }