Skip to content

Commit 778b1ff

Browse files
LeoYang90taylorotwell
authored andcommitted
Fix the position of bound() in Container::instance() (#19207)
* fix:change the position of bound() in instance() to prevent that rebound() always fires incorrectly.More detail can see issues #19194 * failing test for change * fix:rename the variable
1 parent dbc1368 commit 778b1ff

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Illuminate/Container/Container.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,16 @@ public function instance($abstract, $instance)
357357
{
358358
$this->removeAbstractAlias($abstract);
359359

360+
$isBound = $this->bound($abstract);
361+
360362
unset($this->aliases[$abstract]);
361363

362364
// We'll check to determine if this type has been bound before, and if it has
363365
// we will fire the rebound callbacks registered with the container and it
364366
// can be updated with consuming classes that have gotten resolved here.
365367
$this->instances[$abstract] = $instance;
366368

367-
if ($this->bound($abstract)) {
369+
if ($isBound) {
368370
$this->rebound($abstract);
369371
}
370372
}

tests/Container/ContainerTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,20 @@ public function testReboundListenersOnInstances()
337337
$this->assertTrue($_SERVER['__test.rebind']);
338338
}
339339

340+
public function testReboundListenersOnInstancesOnlyFiresIfWasAlreadyBound()
341+
{
342+
$_SERVER['__test.rebind'] = false;
343+
344+
$container = new Container;
345+
$container->rebinding('foo', function () {
346+
$_SERVER['__test.rebind'] = true;
347+
});
348+
$container->instance('foo', function () {
349+
});
350+
351+
$this->assertFalse($_SERVER['__test.rebind']);
352+
}
353+
340354
/**
341355
* @expectedException \Illuminate\Contracts\Container\BindingResolutionException
342356
* @expectedExceptionMessage Unresolvable dependency resolving [Parameter #0 [ <required> $first ]] in class Illuminate\Tests\Container\ContainerMixedPrimitiveStub

0 commit comments

Comments
 (0)