Repository navigation
Freeze #[Immutable] services registered with a closure or instance() - #370
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Description
A service marked
#[Immutable]was only frozen when the container built it itself. If you created the object in a launcher and handed it to the container, it was never frozen, even though the docs showed exactly that pattern and promised it would be. This PR makes the container freeze these services the first time it resolves them, whichever way they were registered.The problem
The docs teach this launcher pattern:
The service never froze.
isFrozen()returnedfalseand writes like$payment->taxRate = 0.0were allowed, anywhere in the app. The same was true for$this->app->instance(PaymentService::class, $payment).The reason is that
freeze()was only called fromContainer::build(). A closure binding orinstance()never goes throughbuild(), so nothing froze the object. The only forms that worked were no registration at all,singleton(PaymentService::class)with no closure, or a closure that called$app->build(...). People following the docs got a service that looked protected and was not.Behavior change to know about
An app that registered an
#[Immutable]service through a closure orinstance()and then changed it after the first resolve will now get anImmutableViolationException. The docs always promised that this would happen, so I treat it as a bug fix, but it is worth a line in the release notes.Checklist