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.4] [WIP] Fix container #19178

Merged
merged 2 commits into from
May 13, 2017
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
6 changes: 4 additions & 2 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ public function bind($abstract, $concrete = null, $shared = false)
protected function getClosure($abstract, $concrete)
{
return function ($container, $parameters = []) use ($abstract, $concrete) {
$method = ($abstract == $concrete) ? 'build' : 'make';
if ($abstract == $concrete) {
return $container->build($concrete);
}

return $container->$method($concrete, $parameters);
return $container->makeWith($concrete, $parameters);
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,26 @@ public function registerDeferredProvider($provider, $service = null)
}
}

/**
* Resolve the given type from the container.
*
* (Overriding Container::makeWith)
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function makeWith($abstract, array $parameters)
{
$abstract = $this->getAlias($abstract);

if (isset($this->deferredServices[$abstract])) {
$this->loadDeferredProvider($abstract);
}

return parent::makeWith($abstract, $parameters);
}

/**
* Resolve the given type from the container.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,14 @@ public function testResolvingWithArrayOfParameters()
$this->assertEquals([1, 2, 3], $container->makeWith('foo', [1, 2, 3]));
}

public function testResolvingWithUsingAnInterface()
{
$container = new Container;
$container->bind(IContainerContractStub::class, ContainerInjectVariableStubWithInterfaceImplementation::class);
$instance = $container->makeWith(IContainerContractStub::class, ['something' => 'laurence']);
$this->assertEquals('laurence', $instance->something);
}

public function testNestedParameterOverride()
{
$container = new Container;
Expand Down Expand Up @@ -864,6 +872,7 @@ interface IContainerContractStub
class ContainerImplementationStub implements IContainerContractStub
{
}

class ContainerImplementationStubTwo implements IContainerContractStub
{
}
Expand Down Expand Up @@ -990,6 +999,16 @@ public function __construct(ContainerConcreteStub $concrete, $something)
}
}

class ContainerInjectVariableStubWithInterfaceImplementation implements IContainerContractStub
{
public $something;

public function __construct(ContainerConcreteStub $concrete, $something)
{
$this->something = $something;
}
}

function containerTestInject(ContainerConcreteStub $stub, $default = 'taylor')
{
return func_get_args();
Expand Down