Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] Use lazy-loading ghost object proxies out of the box#46752
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
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
7eb9290 toab1df92Comparesrc/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
0c56ebc to3bd9bd8Comparesrc/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
df1a745 to830a069Compare5a18093 to5739354Compare5739354 tocb711d0ComparePR rebased and ready \o/ |
chalasr approved these changesJul 12, 2022
cb711d0 to58a1848Comparenicolas-grekas added a commit to symfony/orm-pack that referenced this pull requestJul 12, 2022
Not needed in 6.2 aftersymfony/symfony#46752
fabpot added a commit that referenced this pull requestSep 3, 2022
…ng virtual proxies for non-ghostable lazy services (nicolas-grekas)This PR was merged into the 6.2 branch.Discussion----------[DependencyInjection][VarExporter] Generate lazy-loading virtual proxies for non-ghostable lazy services| Q | A| ------------- | ---| Branch? | 6.2| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets | -| License | MIT| Doc PR | -Since#46752 and#46751, we are able to make services lazy out of the box, except when 1. a service relies on an internal class 2. a service has the `proxy` tag or 3. a service's class is abstract (and the service uses a factory). In these situations, proxy-manager-bridge was required. This was an acceptable trade-off because this would be quite uncommon. But while working on Doctrine, I realized that we cannot use ghost objects when a factory is used. I described this for Doctrine indoctrine/orm#9896 but the situation can happen with any services constructed by a factory. This means we'd need proxy-manager-bridge anytime a factory is used on a lazy service. What was uncommon becomes quite common and the trade-off is not acceptable anymore. Thus this PR.This PR adds a `LazyProxyTrait` and a `ProxyHelper` to build lazy loading virtual proxies at will. It then wires this new capability into the container. As a result proxy-manager-bridge is not needed anymore. We can deprecate it in another PR.While the diff is quite big, `LazyProxyTrait` has many similarities with `LazyGhostTrait` and both traits can be diffed to see where their behavior varies.Excerpt from the [README](https://github.com/nicolas-grekas/symfony/blob/ve-inheritance-proxy/src/Symfony/Component/VarExporter/README.md) for the record:>The component provides two lazy loading patterns: ghost objects and virtual proxies (seehttps://martinfowler.com/eaaCatalog/lazyLoad.html for reference.)>>Ghost objects work only on concrete and non-internal classes. In the generic case, they are not compatible with using factories in their initializer.>>Virtual proxies work on concrete, abstract or internal classes. They provide an API that looks like the actual objects and forward calls to them. They can cause identity problems because proxies might not be seen as equivalents to the actual objects.>>Because of this identity problem, ghost objects should be preferred when possible. Exceptions thrown by the ProxyHelper class can help decide when it can be used or not.>>Ghost objects and virtual proxies both provide implementations for the LazyObjectInterface which allows resetting them to their initial state or to forcibly initialize them when needed. Note that resetting a ghost object skips its read-only properties. You should use a virtual proxy to reset read-only properties.Commits-------4862139 [DependencyInjection][VarExporter] Generate lazy proxies for non-ghostable lazy services out of the box
Merged
Sign up for freeto 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.
Uh oh!
There was an error while loading.Please reload this page.
This PR builds on#46751. It also replaces#46458.
Instead of using ProxyManager to make lazy services actually lazy, using
LazyGhostObjectTraitfrom#46751 allows doing soout of the box - aka without the need to install any optional dependencies.When a virtual proxy is required (typically when usingthe
proxytag), ProxyManager is still required (and the dep remains optional.)But for most services, using
LazyGhostObjectTraitjust works \o/