Skip to content

Commit

Permalink
Solve a few bugs regarding container and aliases.
Browse files Browse the repository at this point in the history
Previous, binding an extension or resolving callback to an alias would
not trigger the callback when the alias was built. Secondly, contextual
binding did not work with aliases. Both of these have been solved.
  • Loading branch information
taylorotwell committed Dec 28, 2016
1 parent c36874d commit c99098f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Container implements ArrayAccess, ContainerContract
*/
public function when($concrete)
{
return new ContextualBindingBuilder($this, $concrete);
return new ContextualBindingBuilder($this, $this->getAlias($concrete));
}

/**
Expand Down Expand Up @@ -278,7 +278,7 @@ public function callMethodBinding($method, $instance)
*/
public function addContextualBinding($concrete, $abstract, $implementation)
{
$this->contextual[$concrete][$abstract] = $implementation;
$this->contextual[$concrete][$this->getAlias($abstract)] = $implementation;
}

/**
Expand Down Expand Up @@ -319,6 +319,8 @@ public function singleton($abstract, $concrete = null)
*/
public function extend($abstract, Closure $closure)
{
$abstract = $this->getAlias($abstract);

if (isset($this->instances[$abstract])) {
$this->instances[$abstract] = $closure($this->instances[$abstract], $this);

Expand Down Expand Up @@ -411,7 +413,7 @@ public function alias($abstract, $alias)
*/
public function rebinding($abstract, Closure $callback)
{
$this->reboundCallbacks[$abstract][] = $callback;
$this->reboundCallbacks[$abstract = $this->getAlias($abstract)][] = $callback;

if ($this->bound($abstract)) {
return $this->make($abstract);
Expand Down Expand Up @@ -766,6 +768,10 @@ protected function unresolvablePrimitive(ReflectionParameter $parameter)
*/
public function resolving($abstract, Closure $callback = null)
{
if (is_string($abstract)) {
$abstract = $this->getAlias($abstract);
}

if (is_null($callback) && $abstract instanceof Closure) {
$this->globalResolvingCallbacks[] = $abstract;
} else {
Expand All @@ -782,6 +788,10 @@ public function resolving($abstract, Closure $callback = null)
*/
public function afterResolving($abstract, Closure $callback = null)
{
if (is_string($abstract)) {
$abstract = $this->getAlias($abstract);
}

if ($abstract instanceof Closure && is_null($callback)) {
$this->globalAfterResolvingCallbacks[] = $abstract;
} else {
Expand Down Expand Up @@ -898,6 +908,8 @@ public function getAlias($abstract)
*/
protected function getExtenders($abstract)
{
$abstract = $this->getAlias($abstract);

if (isset($this->extenders[$abstract])) {
return $this->extenders[$abstract];
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,54 @@ public function testContainerGetFactory()
$factory = $container->factory('name');
$this->assertEquals($container->make('name'), $factory());
}

public function testExtensionWorksOnAliasedBindings()
{
$container = new Container;
$container->singleton('something', function () {
return 'some value';
});
$container->alias('something', 'something-alias');
$container->extend('something-alias', function ($value) {
return $value.' extended';
});

$this->assertEquals('some value extended', $container->make('something'));
}

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

$container->bind('IContainerContractStub', 'ContainerImplementationStub');
$container->alias('IContainerContractStub', 'interface-stub');

$container->alias('ContainerImplementationStub', 'stub-1');

$container->when('ContainerTestContextInjectOne')->needs('interface-stub')->give('stub-1');
$container->when('ContainerTestContextInjectTwo')->needs('interface-stub')->give('ContainerImplementationStubTwo');

$one = $container->make('ContainerTestContextInjectOne');
$two = $container->make('ContainerTestContextInjectTwo');

$this->assertInstanceOf('ContainerImplementationStub', $one->impl);
$this->assertInstanceOf('ContainerImplementationStubTwo', $two->impl);
}

public function testResolvingCallbacksShouldBeFiredWhenCalledWithAliases()
{
$container = new Container;
$container->alias('StdClass', 'std');
$container->resolving('std', function ($object) {
return $object->name = 'taylor';
});
$container->bind('foo', function () {
return new StdClass;
});
$instance = $container->make('foo');

$this->assertEquals('taylor', $instance->name);
}
}

class ContainerConcreteStub
Expand Down

0 comments on commit c99098f

Please sign in to comment.