-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
7eb9290
to
ab1df92
Compare
stof
reviewed
Jun 23, 2022
src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.php
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php
Outdated
Show resolved
Hide resolved
0c56ebc
to
3bd9bd8
Compare
Tobion
reviewed
Jun 24, 2022
src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php
Outdated
Show resolved
Hide resolved
df1a745
to
830a069
Compare
5a18093
to
5739354
Compare
5739354
to
cb711d0
Compare
PR rebased and ready \o/ |
chalasr
approved these changes
Jul 12, 2022
cb711d0
to
58a1848
Compare
nicolas-grekas
added a commit
to symfony/orm-pack
that referenced
this pull request
Jul 12, 2022
Not needed in 6.2 after symfony/symfony#46752
fabpot
added a commit
that referenced
this pull request
Sep 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 in doctrine/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 (see https://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 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.
This PR builds on #46751. It also replaces #46458.
Instead of using ProxyManager to make lazy services actually lazy, using
LazyGhostObjectTrait
from #46751 allows doing so out of the box - aka without the need to install any optional dependencies.When a virtual proxy is required (typically when using the
proxy
tag), ProxyManager is still required (and the dep remains optional.)But for most services, using
LazyGhostObjectTrait
just works \o/