(feat) Container: using() for temporary, self-restoring overrides - #348
Open
techmahedy wants to merge 1 commit into
Open
techmahedy wants to merge 1 commit into
techmahedy wants to merge 1 commit into
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
Container: using() for temporary, self-restoring overrides
Summary
Adds
Container::using(array $overrides, callable $callback). It replaces one or more bindings only while the callback runs, then puts every one of them back exactly as it was, even if the callback throws.Why
instance()andbind()replace a binding permanently. Anything that needs a short-lived override has to save the old binding and restore it by hand, and forgetting (or an exception skipping the restore) leaks the override. That matters most in long-running processes such as queue workers, where the leak carries into the next job or request, and in tests, where it carries into the next test.Behaviour
Each override is applied the way the container already reads a binding:
Closureusing().Closureitself, return it from a factory:fn() => $closure.Limitation
Services that were already resolved and hold a dependency keep that dependency. Only things resolved inside the callback see the override. This is documented on the method and in the docs.
Compatibility
Purely additive: one new public method on
Phaseolies\DI\Container. No existing method, signature or behaviour changed.Files
src/Phaseolies/DI/Container.php: newusing()method.tests/Application/ContainerTest.php: 7 new tests.Testing
New tests cover: override then restore, a key that did not exist before is removed (bindings and instances), restore when the callback throws, a Closure factory is called once per scope, a class-name override is built, the same singleton instance is returned after the scope, and nested scopes.
Checklist