diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index d9a690deccdd..0945cb01ce52 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -343,6 +343,10 @@ public function extend($abstract, Closure $closure) $this->rebound($abstract); } else { $this->extenders[$abstract][] = $closure; + + if ($this->resolved($abstract)) { + $this->rebound($abstract); + } } } diff --git a/tests/Container/ContainerTest.php b/tests/Container/ContainerTest.php index e5cf4164632a..31ad9a8289fe 100755 --- a/tests/Container/ContainerTest.php +++ b/tests/Container/ContainerTest.php @@ -236,6 +236,50 @@ public function testExtendCanBeCalledBeforeBind() $this->assertEquals('foobar', $container->make('foo')); } + public function testExtendInstanceRebindingCallback() + { + $_SERVER['_test_rebind'] = false; + + $container = new Container; + $container->rebinding('foo', function () { + $_SERVER['_test_rebind'] = true; + }); + + $obj = new StdClass; + $container->instance('foo', $obj); + + $container->extend('foo', function ($obj, $container) { + return $obj; + }); + + $this->assertTrue($_SERVER['_test_rebind']); + } + + public function testExtendBindRebindingCallback() + { + $_SERVER['_test_rebind'] = false; + + $container = new Container; + $container->rebinding('foo', function () { + $_SERVER['_test_rebind'] = true; + }); + $container->bind('foo', function () { + $obj = new StdClass; + + return $obj; + }); + + $this->assertFalse($_SERVER['_test_rebind']); + + $container->make('foo'); + + $container->extend('foo', function ($obj, $container) { + return $obj; + }); + + $this->assertTrue($_SERVER['_test_rebind']); + } + public function testUnsetExtend() { $container = new Container;