New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.4] Fix contextual bindings when instances and aliases are involved #17031
Conversation
This doesn’t get reset before the test so every time `testExtendIsLazyInitialized` runs after the first time it fails.
If there is an instance in the container that is involved in contextual binding the binding would not be honored and the same instance would be always returned. If the instance was removed it would be. This fixes that.
Didn't I just fix this? Did my commit not fix this problem? I confirmed with your original logger example it was fixed. |
I ran the all the tests I wrote a while ago (same ones in the PR) to confirm the problem was fixed and these did not pass:
|
The main thing that it needs is skipping the instance short circuit in If I have a contextual binding for Basically, with the way it works now, a container instance overrides all resolution and returns that one object for every request regardless of the registered contextual bindings. Does this explanation help at all? |
Furthermore, the reason the log code works with what you provided has to do with some of the refactoring. On 5.4: https://github.com/laravel/framework/blob/master/src/Illuminate/Log/LogServiceProvider.php#L17 On 5.3 it is using instance (which this PR fixes contextual bindings for). On 5.4 it is using singleton (which your commit fixed contextual bindings for). So the problem still exists just not for the logger because the setup changed. |
@@ -53,6 +53,7 @@ class Container implements ArrayAccess, ContainerContract | |||
* @var array | |||
*/ | |||
protected $aliases = []; | |||
protected $aliasAbstracts = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add phpdoc.
@@ -351,6 +353,23 @@ public function instance($abstract, $instance) | |||
} | |||
} | |||
|
|||
private function removeAbstractAlias($searched) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this protected, and add phpdoc.
|
||
class ContainerTestContextInjectInstantiations implements IContainerContractStub | ||
{ | ||
public static $instantiations = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work properly if you use the flag on phpunit that lets you run the tests multiple times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so, why does this variable get initialized here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not work. It's a left over from before I set it in the test itself. Thanks for catching this.
OK Thanks for feedback. |
A resubmit of #15637 directed at 5.4.
Ping @taylorotwell, addressing your last comment from the PR:
Yes. It does respect it. I've added a test to ensure that only one instance of
SomeLogger
(or in the case of the testContainerTestContextInjectInstantiations
) is created and used during contextual binding if there is an instance available.I couldn't come up with a better name for
ContainerTestContextInjectInstantiations
that followed the conventions of the other classes in the test case. Feel free to change it. :)