3 namespace League\Container;
5 use League\Container\Argument\ArgumentResolverInterface;
6 use League\Container\Argument\ArgumentResolverTrait;
7 use League\Container\Exception\NotFoundException;
9 use ReflectionFunction;
12 class ReflectionContainer implements
13 ArgumentResolverInterface,
14 ImmutableContainerInterface
16 use ArgumentResolverTrait;
17 use ImmutableContainerAwareTrait;
22 public function get($alias, array $args = [])
24 if (! $this->has($alias)) {
25 throw new NotFoundException(
26 sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $alias)
30 $reflector = new ReflectionClass($alias);
31 $construct = $reflector->getConstructor();
33 if ($construct === null) {
37 return $reflector->newInstanceArgs(
38 $this->reflectArguments($construct, $args)
45 public function has($alias)
47 return class_exists($alias);
51 * Invoke a callable via the container.
53 * @param callable $callable
57 public function call(callable $callable, array $args = [])
59 if (is_string($callable) && strpos($callable, '::') !== false) {
60 $callable = explode('::', $callable);
63 if (is_array($callable)) {
64 if (is_string($callable[0])) {
65 $callable[0] = $this->getContainer()->get($callable[0]);
68 $reflection = new ReflectionMethod($callable[0], $callable[1]);
70 if ($reflection->isStatic()) {
74 return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
77 if (is_object($callable)) {
78 $reflection = new ReflectionMethod($callable, '__invoke');
80 return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
83 $reflection = new ReflectionFunction($callable);
85 return $reflection->invokeArgs($this->reflectArguments($reflection, $args));