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

[5.7] Allow resolve with to accept type hinted params. #27077

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 11 additions & 4 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,9 +851,15 @@ protected function resolveDependencies(array $dependencies)
*/
protected function hasParameterOverride($dependency)
{
return array_key_exists(
$dependency->name, $this->getLastParameterOverride()
);
// We need to check if the user's trying to override the dependency using the
// parameter variable name first. If the parameter was not found using the
// variable name then check if they passed through a type hint instead.
return array_key_exists($dependency->name, $this->getLastParameterOverride()) ||
(
! empty($dependency->getClass()->name)
? array_key_exists($dependency->getClass()->name, $this->getLastParameterOverride())
: false
);
}

/**
Expand All @@ -864,7 +870,8 @@ protected function hasParameterOverride($dependency)
*/
protected function getParameterOverride($dependency)
{
return $this->getLastParameterOverride()[$dependency->name];
return $this->getLastParameterOverride()[$dependency->name]
?? $this->getLastParameterOverride()[$dependency->getClass()->name];
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,18 @@ public function testResolvingWithUsingAnInterface()
$this->assertEquals('laurence', $instance->something);
}

public function testResolvingWithTypeHintedParams()
{
$container = new Container;
$container->bind(IContainerContractStub::class, ContainerImplementationStub::class);

$implementation = $container->make(ContainerImplementationStubTwo::class);

$class = $container->make(ContainerDependentStub::class, [IContainerContractStub::class => $implementation]);

$this->assertSame($implementation, $class->impl);
}

public function testNestedParameterOverride()
{
$container = new Container;
Expand Down