Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] fix: allows injection using multiple interfaces with the same concrete implementation #53275

Draft
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/Illuminate/Routing/ResolvesRouteDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,30 @@ public function resolveMethodDependencies(array $parameters, ReflectionFunctionA

$skippableValue = new stdClass;

$resolvedInterfaces = [];

foreach ($reflector->getParameters() as $key => $parameter) {
$instance = $this->transformDependency($parameter, $parameters, $skippableValue);
$className = Reflector::getParameterClassName($parameter);

// If the parameter has a type-hinted class, we will check to see if it is already in
// the list of parameters. If it is we will just skip it as it is probably a model
// binding and we do not want to mess with those; otherwise, we resolve it here.
if ($className && (! $this->alreadyInParameters($className, $parameters) || ! $this->alreadyInResolvedInterfaces($className, $resolvedInterfaces))) {
$instance = $this->transformDependency($parameter, $className);

if (interface_exists($className) && !class_exists($className) && $instance !== $skippableValue) {
$resolvedInterfaces[] = $className;
}
} else {
$instance = $skippableValue;
}

if ($instance !== $skippableValue) {
$instanceCount++;

$this->spliceIntoParameters($parameters, $key, $instance);
} elseif (! isset($values[$key - $instanceCount]) &&
$parameter->isDefaultValueAvailable()) {
$parameter->isDefaultValueAvailable()) {
$this->spliceIntoParameters($parameters, $key, $parameter->getDefaultValue());
}

Expand All @@ -73,26 +88,17 @@ public function resolveMethodDependencies(array $parameters, ReflectionFunctionA
* @param object $skippableValue
* @return mixed
*/
protected function transformDependency(ReflectionParameter $parameter, $parameters, $skippableValue)
protected function transformDependency(ReflectionParameter $parameter, $className)
{
$className = Reflector::getParameterClassName($parameter);

if ($attribute = Util::getContextualAttributeFromDependency($parameter)) {
return $this->container->resolveFromAttribute($attribute);
}

// If the parameter has a type-hinted class, we will check to see if it is already in
// the list of parameters. If it is we will just skip it as it is probably a model
// binding and we do not want to mess with those; otherwise, we resolve it here.
if ($className && ! $this->alreadyInParameters($className, $parameters)) {
$isEnum = (new ReflectionClass($className))->isEnum();

return $parameter->isDefaultValueAvailable()
? ($isEnum ? $parameter->getDefaultValue() : null)
: $this->container->make($className);
}
$isEnum = (new ReflectionClass($className))->isEnum();

return $skippableValue;
return $parameter->isDefaultValueAvailable()
? ($isEnum ? $parameter->getDefaultValue() : null)
: $this->container->make($className);
}

/**
Expand All @@ -107,6 +113,11 @@ protected function alreadyInParameters($class, array $parameters)
return ! is_null(Arr::first($parameters, fn ($value) => $value instanceof $class));
}

protected function alreadyInResolvedInterfaces($class, array $resolvedInterfaces)
{
return in_array($class, $resolvedInterfaces);
}

/**
* Splice the given value into the parameter list.
*
Expand Down
Loading