Skip to content

Commit

Permalink
[8.x] Allow method typed variadics dependencies (#40255)
Browse files Browse the repository at this point in the history
* [8.x] Allow method typed variadics dependencies

* check array

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
LeoColomb and taylorotwell authored Jan 5, 2022
1 parent 066cbdf commit 5321e34
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Illuminate/Container/BoundMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,15 @@ protected static function addDependencyForCallParameter($container, $parameter,

unset($parameters[$className]);
} else {
$dependencies[] = $container->make($className);
if ($parameter->isVariadic()) {
$variadicDependencies = $container->make($className);

$dependencies = array_merge($dependencies, is_array($variadicDependencies)
? $variadicDependencies
: [$variadicDependencies]);
} else {
$dependencies[] = $container->make($className);
}
}
} elseif ($parameter->isDefaultValueAvailable()) {
$dependencies[] = $parameter->getDefaultValue();
Expand Down
23 changes: 23 additions & 0 deletions tests/Container/ContainerCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5321e34

Please sign in to comment.