Leverage LazyGhostTrait when possible #10187
Merged
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 replaces Doctrine Common's proxies by VarExporter's
LazyGhostTrait
.It requires running
composer require symfony/var-exporter:^6.2@dev doctrine/persistence:^3.1@dev
for now until stable versions of both packages are released (will happen in one month for var-exporter.)As you can see from the changes on the documentation, the benefit is quite clear: this removes many limitations of the current proxy implementation.
From what I've understood through many interactions around
doctrine/common
, deprecating everything in theDoctrine\Common\Proxy
namespace is highly desired and this PR provides a critical step in this direction. Thanks to the design design + maintenance policies ofLazyGhostTrait
, this should also make it way simpler to support newer versions of PHP in the future (this is already the case for PHP 8.1 which is fully supported by the trait.).The target version on ORM is 2.14. This should help move away from Common's proxies quite quickly, and will allow deprecating them ASAP. In turn, Common's Proxies wouldn't need to add support for newer versions of PHP (which is non trivial, as highlighted by the work still happening to support PHP 8.1.), making maintenance easier for the Doctrine project. Targeting ORM v3 wouldn't allow this.
Another reason to target ORM v2 is that the change is mostly backward compatible, so it doesn't need to target v3. In my experience, this can help adoption of ORM v3 when it will be released, by reducing the number of dependencies that one would have to update in the process of upgrading.
Still, enabling the new proxies is opt-in for a few reasons:
Doctrine\ORM\Proxy\Proxy
interface anymore so ppl need to replace theirinstanceof
checks to targetDoctrine\Persistence\Proxy
instead (which is implemented by both new and old proxy implems.)__wakeup()
is not called anymore when initializing entities that implement it (this should be transparent if ppl implemented it as advised in the doc - aka not doing anything when the identifier is not yet known.)One needs to call
Configuration::setLazyGhostObjectEnabled(true)
to turn on the new proxies.For cross-ref, this takes some inspiration+changes from #6719.
As I see for now, the steps after merging this could be to:
Doctrine\Common\Proxy
Configuration::setLazyGhostObjectEnabled()
a no-op in ORM v3 (throwing when called withfalse
as argument)Configuration::setLazyGhostObjectEnabled()
but not before ORM v3.1 to ease the transition (we cannot remove the method in v3.0 since we cannot first deprecate the method in v2).For inspiration for other projects, the plan on Symfony should be to add a new config option to DoctrineBundle to allow opting-in, to deprecate not setting it to
true
, and to update the default recipe to make new projects opt-in by default.