Skip to content

Commit 38c6076

Browse files
LeoYang90taylorotwell
authored andcommitted
[5.4] Container: Extend() can not fire the rebinding callback (#19288)
* fix:Extend() can not fire the rebinding callback issue #19241 * StyleCI * StyleCI * StyleCI
1 parent 10e15da commit 38c6076

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Illuminate/Container/Container.php

+4
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ public function extend($abstract, Closure $closure)
343343
$this->rebound($abstract);
344344
} else {
345345
$this->extenders[$abstract][] = $closure;
346+
347+
if ($this->resolved($abstract)) {
348+
$this->rebound($abstract);
349+
}
346350
}
347351
}
348352

tests/Container/ContainerTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,50 @@ public function testExtendCanBeCalledBeforeBind()
236236
$this->assertEquals('foobar', $container->make('foo'));
237237
}
238238

239+
public function testExtendInstanceRebindingCallback()
240+
{
241+
$_SERVER['_test_rebind'] = false;
242+
243+
$container = new Container;
244+
$container->rebinding('foo', function () {
245+
$_SERVER['_test_rebind'] = true;
246+
});
247+
248+
$obj = new StdClass;
249+
$container->instance('foo', $obj);
250+
251+
$container->extend('foo', function ($obj, $container) {
252+
return $obj;
253+
});
254+
255+
$this->assertTrue($_SERVER['_test_rebind']);
256+
}
257+
258+
public function testExtendBindRebindingCallback()
259+
{
260+
$_SERVER['_test_rebind'] = false;
261+
262+
$container = new Container;
263+
$container->rebinding('foo', function () {
264+
$_SERVER['_test_rebind'] = true;
265+
});
266+
$container->bind('foo', function () {
267+
$obj = new StdClass;
268+
269+
return $obj;
270+
});
271+
272+
$this->assertFalse($_SERVER['_test_rebind']);
273+
274+
$container->make('foo');
275+
276+
$container->extend('foo', function ($obj, $container) {
277+
return $obj;
278+
});
279+
280+
$this->assertTrue($_SERVER['_test_rebind']);
281+
}
282+
239283
public function testUnsetExtend()
240284
{
241285
$container = new Container;

0 commit comments

Comments
 (0)