Skip to content

Conversation

@LeoYang90
Copy link
Contributor

Hi,

In instance() function, the code set the array of instances[$abstract]. And next, the code check to determine if type has been bound before,.However in bound(), isset(this->instances[$abstract]) always be true because it is set just in last line, and $this->isAlias($abstract) always be false because it just unset($this->aliases[$abstract]):
file: /illuminate/Container/Container.php-line356

public function instance($abstract, $instance)
{
    $this->removeAbstractAlias($abstract);

    unset($this->aliases[$abstract]);        
    $this->instances[$abstract] = $instance;//this is problem

    if ($this->bound($abstract)) {
        $this->rebound($abstract);
    }
}

file: /illuminate/Container/Container.php-line151

public function bound($abstract)
{
    return isset($this->bindings[$abstract]) ||
           isset($this->instances[$abstract]) || ////this is problem
           $this->isAlias($abstract);
}

so rebound() function always fires incorrectly.
I change the position of bound() function, so it will not be affected by the code in the instance().
More detail can see issues #19194
thanks!

…und() always fires incorrectly.More detail can see issues #19194
@laurencei
Copy link
Contributor

laurencei commented May 15, 2017

@LeoYang90 - welcome to Laravel. Great pickup & first contribution!

Can we add a failing test for this change? I came up with this as one option for you:

    public function testReboundListenersOnInstancesOnlyFiresIfWasAlreadyBound()
    {
        $_SERVER['__test.rebind'] = false;

        $container = new Container;
        $container->rebinding('foo', function () {
            $_SERVER['__test.rebind'] = true;
        });
        $container->instance('foo', function () {
        });

        $this->assertFalse($_SERVER['__test.rebind']);
    }

Place around line 340 of tests\Container\ContainerTest.php

{
$this->removeAbstractAlias($abstract);

$is_bound = $this->bound($abstract);
Copy link
Contributor

Choose a reason for hiding this comment

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

Better use $isBound in camel-case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

@taylorotwell taylorotwell merged commit 778b1ff into laravel:5.4 May 15, 2017
@LeoYang90 LeoYang90 deleted the fix-bound branch May 18, 2017 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants