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

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

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
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
64 changes: 5 additions & 59 deletions src/Illuminate/Routing/ResolvesRouteDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Illuminate\Routing;

use Illuminate\Container\Util;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use ReflectionClass;
use ReflectionException;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use ReflectionParameter;
Expand Down Expand Up @@ -49,24 +47,15 @@ public function resolveMethodDependencies(array $parameters, ReflectionFunctionA

$skippableValue = new stdClass;

$resolvedInterfaces = [];

foreach ($reflector->getParameters() as $key => $parameter) {
$className = Reflector::getParameterClassName($parameter);

$instance = $this->transformDependency($parameter, $parameters, $className, $skippableValue, $resolvedInterfaces);

if ($instance !== $skippableValue &&
! $this->alreadyInResolvedInterfaces($className, $resolvedInterfaces)) {
$resolvedInterfaces[] = $className;
}
$instance = $this->transformDependency($parameter, $parameters, $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 @@ -79,27 +68,18 @@ public function resolveMethodDependencies(array $parameters, ReflectionFunctionA
/**
* Attempt to transform the given parameter into a class instance.
*
* @param ReflectionParameter $parameter
* @param \ReflectionParameter $parameter
* @param array $parameters
* @param string $className
* @param object $skippableValue
* @param $resolvedInterfaces
* @return mixed
*
* @throws BindingResolutionException
* @throws ReflectionException
*/
protected function transformDependency(ReflectionParameter $parameter, $parameters, $className, object $skippableValue, $resolvedInterfaces)
protected function transformDependency(ReflectionParameter $parameter, $parameters, $skippableValue)
{
if ($attribute = Util::getContextualAttributeFromDependency($parameter)) {
return $this->container->resolveFromAttribute($attribute);
}

if ($this->isSimilarConcreteToExistingParameterButDifferentInterface(
$className, $parameters, $resolvedInterfaces
)) {
return $this->container->make($className);
}
$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
Expand All @@ -115,24 +95,6 @@ protected function transformDependency(ReflectionParameter $parameter, $paramete
return $skippableValue;
}

/**
* Determines if an instance of the given class is already in the parameters, but the route is type-hinting another interface that hasn't yet been resolved.
*
* @param string $className
* @param array $parameters
* @param array $resolvedInterfaces
* @return bool
*/
protected function isSimilarConcreteToExistingParameterButDifferentInterface($className, array $parameters, array $resolvedInterfaces)
{
// See: https://github.com/laravel/framework/pull/53275
return $className &&
$this->alreadyInParameters($className, $parameters) &&
interface_exists($className) &&
! $this->alreadyInResolvedInterfaces($className, $resolvedInterfaces) &&
(new ReflectionClass($className))->isInterface();
}

/**
* Determine if an object of the given class is in a list of parameters.
*
Expand All @@ -145,22 +107,6 @@ protected function alreadyInParameters($class, array $parameters)
return ! is_null(Arr::first($parameters, fn ($value) => $value instanceof $class));
}

/**
* Determine if the given class name is already in the list of resolved interfaces.
*
* @param string|null $class
* @param array $resolvedInterfaces
* @return bool
*/
protected function alreadyInResolvedInterfaces($class, array $resolvedInterfaces)
{
if (! is_null($class)) {
return in_array($class, $resolvedInterfaces);
}

return false;
}

/**
* Splice the given value into the parameter list.
*
Expand Down
52 changes: 0 additions & 52 deletions tests/Routing/RouteDependencyInjectionTest.php

This file was deleted.

Loading