From 49549b73dcf0b0141c0338410a4ed7f1b24dd5fc Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 24 May 2018 15:16:56 +0200 Subject: [PATCH] Allow passing object instances regardless of the parameter name to method injection (#24234) --- src/Illuminate/Container/BoundMethod.php | 4 ++++ tests/Container/ContainerTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Illuminate/Container/BoundMethod.php b/src/Illuminate/Container/BoundMethod.php index 0dfd6fdc5bd0..69a812f72909 100644 --- a/src/Illuminate/Container/BoundMethod.php +++ b/src/Illuminate/Container/BoundMethod.php @@ -152,6 +152,10 @@ protected static function addDependencyForCallParameter($container, $parameter, $dependencies[] = $parameters[$parameter->name]; unset($parameters[$parameter->name]); + } elseif ($parameter->getClass() && array_key_exists($parameter->getClass()->name, $parameters)) { + $dependencies[] = $parameters[$parameter->getClass()->name]; + + unset($parameters[$parameter->getClass()->name]); } elseif ($parameter->getClass()) { $dependencies[] = $container->make($parameter->getClass()->name); } elseif ($parameter->isDefaultValueAvailable()) { diff --git a/tests/Container/ContainerTest.php b/tests/Container/ContainerTest.php index 352e756f8d24..18c5d0be3e90 100755 --- a/tests/Container/ContainerTest.php +++ b/tests/Container/ContainerTest.php @@ -490,6 +490,14 @@ public function testCallWithDependencies() $this->assertInstanceOf('stdClass', $result[0]); $this->assertEquals('taylor', $result[1]); + $stub = new ContainerConcreteStub; + $result = $container->call(function (stdClass $foo, ContainerConcreteStub $bar) { + return func_get_args(); + }, [ContainerConcreteStub::class => $stub]); + + $this->assertInstanceOf('stdClass', $result[0]); + $this->assertSame($stub, $result[1]); + /* * Wrap a function... */