Skip to content

Commit

Permalink
Fix conditional binding for nesting dependencies with primitives
Browse files Browse the repository at this point in the history
It fixes this bug: laravel#29019
  • Loading branch information
Organizm238 committed Jul 15, 2019
1 parent dfbbaf3 commit 9bf0215
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ protected function resolveClass(ReflectionParameter $parameter)
// the value of the dependency, similarly to how we do this with scalars.
catch (BindingResolutionException $e) {
if ($parameter->isOptional()) {
array_pop($this->buildStack);
return $parameter->getDefaultValue();
}

Expand Down
65 changes: 65 additions & 0 deletions tests/Container/ContextualBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,29 @@ public function testContextualBindingWorksWithAliasedTargets()
$this->assertInstanceOf(ContainerContextImplementationStub::class, $one->impl);
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $two->impl);
}


public function testContextualBindingWorksForNestedPrimitiveResolvingForMultipleClassesInjection()
{
$container = new Container;

$container->when(ContainerTestContextInjectTwoInstances::class)->needs(ContainerTestContextPrimitiveTwo::class)->give(function() {
return new ContainerTestContextPrimitiveTwo('test');
});

$resolvedInstance = $container->make(ContainerTestContextInjectTwoInstances::class);
$this->assertInstanceOf(
ContainerTestContextWithNestedOptionalDependencyStubWithPrimitive::class,
$resolvedInstance->implOne
);
$this->assertNull($resolvedInstance->implOne->inner);

$this->assertInstanceOf(
ContainerTestContextPrimitiveTwo::class,
$resolvedInstance->implTwo
);
$this->assertEquals($resolvedInstance->implTwo->primitive, 'test');
}
}

interface IContainerContextContractStub
Expand Down Expand Up @@ -265,3 +288,45 @@ public function __construct(IContainerContextContractStub $impl)
$this->impl = $impl;
}
}

class ContainerTestContextInjectTwoInstances
{
public $implOne;
public $implTwo;

public function __construct(ContainerTestContextWithNestedOptionalDependencyStubWithPrimitive $implOne, ContainerTestContextPrimitiveTwo $implTwo)
{
$this->implOne = $implOne;
$this->implTwo = $implTwo;
}
}

class ContainerTestContextWithNestedOptionalDependencyStubWithPrimitive
{
public $inner;

public function __construct(ContainerTestContextPrimitiveOne $inner = null)
{
$this->inner = $inner;
}
}

class ContainerTestContextPrimitiveOne
{
public $primitive;

public function __construct(string $primitive)
{
$this->primitive = $primitive;
}
}

class ContainerTestContextPrimitiveTwo
{
public $primitive;

public function __construct(string $primitive)
{
$this->primitive = $primitive;
}
}

0 comments on commit 9bf0215

Please sign in to comment.