Skip to content

[Proposal] Make dependency injection work with default values #80

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

Closed
wants to merge 4 commits 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
17 changes: 12 additions & 5 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,20 @@ protected function getDependencies($parameters)
$dependency = $parameter->getClass();

// If the class is null, it means the dependency is a string or some other
// primitive type which we can not resolve since it is not a class and
// we'll just bomb out with an error since we have no-where to go.
// primitive type, which we can not resolve since it is not a class, if there
// is a default value we will use it otherwise we'll just bomb out with an
// error since we have no-where to go.
if (is_null($dependency))
{
$message = "Unresolvable dependency resolving [$parameter].";
if ( ! $parameter->isDefaultValueAvailable())
{
$message = "Unresolvable dependency resolving [$parameter].";

throw new BindingResolutionException($message);
throw new BindingResolutionException($message);
}

$dependencies[] = $parameter->getDefaultValue();
continue;
}

$dependencies[] = $this->make($dependency->name);
Expand Down Expand Up @@ -435,4 +442,4 @@ public function offsetUnset($key)
unset($this->bindings[$key]);
}

}
}
24 changes: 23 additions & 1 deletion tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function testNestedDependencyResolution()
$this->assertTrue($class->inner->impl instanceof ContainerImplementationStub);
}

public function testNestedDependencyResolutionWithDefaultValue()
{
$container = new Container;
$container->bind('IContainerContractStub', 'ContainerImplementationStub');
$class = $container->make('ContainerNestedDependentWithDefaultValueStub');
$this->assertTrue($class->inner instanceof ContainerDependentStub);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about assertInstanceOf()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just copying Taylors tests

$this->assertTrue($class->inner->impl instanceof ContainerImplementationStub);
$this->assertNull($class->innerArray);
$this->assertEquals($class->innerString, 'string');
}

public function testContainerIsPassedToResolvers()
{
Expand Down Expand Up @@ -206,4 +216,16 @@ public function __construct(ContainerDependentStub $inner)
{
$this->inner = $inner;
}
}
}

class ContainerNestedDependentWithDefaultValueStub {
public $inner;
public $innerArray;
public $innerString;
public function __construct(ContainerDependentStub $inner, array $innerArray = null, $innerString = 'string')
{
$this->inner = $inner;
$this->innerArray = $innerArray;
$this->innerString = $innerString;
}
}