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

[12.x] fix: container resolution order when resolving class dependencies #53522

Merged
merged 3 commits into from
Nov 27, 2024
Merged
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
28 changes: 19 additions & 9 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,22 +1108,32 @@ protected function resolvePrimitive(ReflectionParameter $parameter)
*/
protected function resolveClass(ReflectionParameter $parameter)
{
$className = Util::getParameterClassName($parameter);

// First, we check if the dependency has been explicitly bound in the container
// and if so, we will resolve it directly from there to respect any explicit
// bindings the developer has defined rather than using any default value.
if ($this->bound($className)) {
return $this->make($className);
}

// If no binding exists, we will check if a default value has been defined for
// the parameter. If it has, we should return it to avoid overriding any of
// the developer specified default values for the constructor parameters.
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}

try {
return $parameter->isVariadic()
? $this->resolveVariadicClass($parameter)
: $this->make(Util::getParameterClassName($parameter));
: $this->make($className);
}

// If we can not resolve the class instance, we will check to see if the value
// is optional, and if it is we will return the optional parameter value as
// the value of the dependency, similarly to how we do this with scalars.
// is variadic. If it is, we will return an empty array as the value of the
// dependency similarly to how we handle scalar values in this situation.
catch (BindingResolutionException $e) {
if ($parameter->isDefaultValueAvailable()) {
array_pop($this->with);

return $parameter->getDefaultValue();
}

if ($parameter->isVariadic()) {
array_pop($this->with);

Expand Down
21 changes: 21 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ public function testResolutionOfDefaultParameters()
$this->assertSame('taylor', $instance->default);
}

public function testResolutionOfClassWithDefaultParameters()
{
$container = new Container;
$instance = $container->make(ContainerClassWithDefaultValueStub::class);
$this->assertInstanceOf(ContainerConcreteStub::class, $instance->noDefault);
$this->assertSame(null, $instance->default);

$container->bind(ContainerConcreteStub::class, fn () => new ContainerConcreteStub);
$instance = $container->make(ContainerClassWithDefaultValueStub::class);
$this->assertInstanceOf(ContainerConcreteStub::class, $instance->default);
}

public function testBound()
{
$container = new Container;
Expand Down Expand Up @@ -765,6 +777,15 @@ public function __construct(ContainerConcreteStub $stub, $default = 'taylor')
}
}

class ContainerClassWithDefaultValueStub
{
public function __construct(
public ?ContainerConcreteStub $noDefault,
public ?ContainerConcreteStub $default = null,
) {
}
}

class ContainerMixedPrimitiveStub
{
public $first;
Expand Down