From 622b772a878f9082fa280777beb954887cdeafa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Colombaro?= Date: Tue, 4 Jan 2022 18:15:16 +0000 Subject: [PATCH 1/2] [8.x] Allow method typed variadics dependencies --- src/Illuminate/Container/BoundMethod.php | 6 +++++- tests/Container/ContainerCallTest.php | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Container/BoundMethod.php b/src/Illuminate/Container/BoundMethod.php index 3a614720fae9..b42aeed3bfa1 100644 --- a/src/Illuminate/Container/BoundMethod.php +++ b/src/Illuminate/Container/BoundMethod.php @@ -172,7 +172,11 @@ protected static function addDependencyForCallParameter($container, $parameter, unset($parameters[$className]); } else { - $dependencies[] = $container->make($className); + if ($parameter->isVariadic()) { + $dependencies = array_merge($dependencies, $container->make($className)); + } else { + $dependencies[] = $container->make($className); + } } } elseif ($parameter->isDefaultValueAvailable()) { $dependencies[] = $parameter->getDefaultValue(); diff --git a/tests/Container/ContainerCallTest.php b/tests/Container/ContainerCallTest.php index e2a32a8955b0..694ccd511fcb 100644 --- a/tests/Container/ContainerCallTest.php +++ b/tests/Container/ContainerCallTest.php @@ -160,6 +160,29 @@ public function testCallWithDependencies() $this->assertSame('taylor', $result[1]); } + public function testCallWithVariadicDependency() + { + $stub1 = new ContainerCallConcreteStub; + $stub2 = new ContainerCallConcreteStub; + + $container = new Container; + $container->bind(ContainerCallConcreteStub::class, function () use ($stub1, $stub2) { + return [ + $stub1, + $stub2, + ]; + }); + + $result = $container->call(function (stdClass $foo, ContainerCallConcreteStub ...$bar) { + return func_get_args(); + }); + + $this->assertInstanceOf(stdClass::class, $result[0]); + $this->assertInstanceOf(ContainerCallConcreteStub::class, $result[1]); + $this->assertSame($stub1, $result[1]); + $this->assertSame($stub2, $result[2]); + } + public function testCallWithCallableObject() { $container = new Container; From e59f86d678532180516aaff3edbeb95f3e60a39d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 5 Jan 2022 09:36:04 -0600 Subject: [PATCH 2/2] check array --- src/Illuminate/Container/BoundMethod.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Container/BoundMethod.php b/src/Illuminate/Container/BoundMethod.php index b42aeed3bfa1..5c96c973e0c8 100644 --- a/src/Illuminate/Container/BoundMethod.php +++ b/src/Illuminate/Container/BoundMethod.php @@ -173,7 +173,11 @@ protected static function addDependencyForCallParameter($container, $parameter, unset($parameters[$className]); } else { if ($parameter->isVariadic()) { - $dependencies = array_merge($dependencies, $container->make($className)); + $variadicDependencies = $container->make($className); + + $dependencies = array_merge($dependencies, is_array($variadicDependencies) + ? $variadicDependencies + : [$variadicDependencies]); } else { $dependencies[] = $container->make($className); }