Skip to content

Commit 0b934bc

Browse files
committed
fix container build and add tests
1 parent 84946b7 commit 0b934bc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Illuminate/Container/Container.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ public function build($concrete)
715715
// hand back the results of the functions, which allows functions to be
716716
// used as resolvers for more fine-tuned resolution of these objects.
717717
if ($concrete instanceof Closure) {
718-
return $concrete($this, end($this->with));
718+
return $concrete($this, $this->getLastParameterOverride());
719719
}
720720

721721
$reflector = new ReflectionClass($concrete);
@@ -793,7 +793,7 @@ protected function resolveDependencies(array $dependencies)
793793
*/
794794
protected function hasParameterOverride($dependency)
795795
{
796-
return array_key_exists($dependency->name, end($this->with));
796+
return array_key_exists($dependency->name, $this->getLastParameterOverride());
797797
}
798798

799799
/**
@@ -804,7 +804,17 @@ protected function hasParameterOverride($dependency)
804804
*/
805805
protected function getParameterOverride($dependency)
806806
{
807-
return end($this->with)[$dependency->name];
807+
return $this->getLastParameterOverride()[$dependency->name];
808+
}
809+
810+
/**
811+
* Get the last parameter override.
812+
*
813+
* @return array
814+
*/
815+
protected function getLastParameterOverride()
816+
{
817+
return count($this->with) ? end($this->with) : [];
808818
}
809819

810820
/**

tests/Container/ContainerTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,19 @@ public function testSingletonBindingsNotRespectedWithMakeParameters()
838838
$this->assertEquals(['name' => 'taylor'], $container->makeWith('foo', ['name' => 'taylor']));
839839
$this->assertEquals(['name' => 'abigail'], $container->makeWith('foo', ['name' => 'abigail']));
840840
}
841+
842+
public function testCanBuildWithoutParameterStackWithNoConstructors()
843+
{
844+
$container = new Container;
845+
$this->assertInstanceOf(ContainerConcreteStub::class, $container->build(ContainerConcreteStub::class));
846+
}
847+
848+
public function testCanBuildWithoutParameterStackWithConstructors()
849+
{
850+
$container = new Container;
851+
$container->bind('Illuminate\Tests\Container\IContainerContractStub', 'Illuminate\Tests\Container\ContainerImplementationStub');
852+
$this->assertInstanceOf(ContainerDependentStub::class, $container->build(ContainerDependentStub::class));
853+
}
841854
}
842855

843856
class ContainerConcreteStub

0 commit comments

Comments
 (0)