Skip to content

Commit

Permalink
cleaning and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 20, 2020
1 parent 45519f2 commit 1dd6db3
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 [];
}
Expand All @@ -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.
*
Expand Down

0 comments on commit 1dd6db3

Please sign in to comment.