diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 90e06780d64..74d23c38197 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -874,9 +874,6 @@ protected function resolveDependencies(array $dependencies) ? $this->resolvePrimitive($dependency) : $this->resolveClass($dependency); - // If this dependency is variadic the result will be an array but they will - // need to be unrolled so they look like additional results instead of a - // single result representing an array of items. if ($dependency->isVariadic()) { $results = array_merge($results, $result); } else { @@ -953,28 +950,9 @@ protected function resolvePrimitive(ReflectionParameter $parameter) protected function resolveClass(ReflectionParameter $parameter) { try { - // The default case is handled early and quickly so more complicated - // checks that apply only for variadic calls only run in cases - // where the parameter is actually variadic. - if (! $parameter->isVariadic()) { - return $this->make($parameter->getClass()->name); - } - - $abstract = $this->getAlias($parameter->getClass()->name); - $concrete = $this->getContextualConcrete($abstract); - - // If the concrete for a variadic is not an array we can treat it - // like other parameters and will assume making the parameter - // will result in an array. - if (! is_array($concrete)) { - return $this->make($parameter->getClass()->name); - } - - // If the concrete for a variadic is an array, we call resolve on - // each value in the concrete array. - return array_map(function ($abstract) { - return $this->resolve($abstract); - }, $concrete); + return $parameter->isVariadic() + ? $this->resolveVariadicClass($parameter) + : $this->make($parameter->getClass()->name); } // If we can not resolve the class instance, we will check to see if the value @@ -985,11 +963,6 @@ protected function resolveClass(ReflectionParameter $parameter) return $parameter->getDefaultValue(); } - // The "default value" for variadic can never be null but PHP does not - // treat these parameters as having a default value. By returning an - // empty array for variadic values we can better emulate PHP - // runtime which will always result in an empty array if - // no values are present. if ($parameter->isVariadic()) { return []; } @@ -998,6 +971,25 @@ protected function resolveClass(ReflectionParameter $parameter) } } + /** + * Resolve a class based variadic dependency from the container. + * + * @param \ReflectionParameter $parameter + * @return mixed + */ + protected function resolveVariadicClass(ReflectionParameter $parameter) + { + $abstract = $this->getAlias($parameter->getClass()->name); + + if (! is_array($concrete = $this->getContextualConcrete($abstract))) { + return $this->make($parameter->getClass()->name); + } + + return array_map(function ($abstract) { + return $this->resolve($abstract); + }, $concrete); + } + /** * Throw an exception that the concrete is not instantiable. *