Skip to content

Commit

Permalink
Revert "[11.x] fix: allows injection using multiple interfaces with t…
Browse files Browse the repository at this point in the history
…he same …" (#53955)

This reverts commit a306193.
  • Loading branch information
taylorotwell authored Dec 17, 2024
1 parent f349abb commit 17c0416
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 111 deletions.
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.

0 comments on commit 17c0416

Please sign in to comment.