1515use Doctrine \DBAL \Connection ;
1616use Doctrine \DBAL \Connections \PrimaryReadReplicaConnection ;
1717use Doctrine \DBAL \Driver \Middleware as MiddlewareInterface ;
18+ use Doctrine \ORM \Configuration as OrmConfiguration ;
1819use Doctrine \ORM \EntityManager ;
1920use Doctrine \ORM \EntityManagerInterface ;
2021use Doctrine \ORM \Id \AbstractIdGenerator ;
2324use Doctrine \ORM \Tools \Console \Command \EnsureProductionSettingsCommand ;
2425use Doctrine \ORM \Tools \Export \ClassMetadataExporter ;
2526use Doctrine \ORM \UnitOfWork ;
27+ use Doctrine \Persistence \Reflection \RuntimeReflectionProperty ;
2628use InvalidArgumentException ;
2729use LogicException ;
2830use ReflectionMethod ;
4850use Symfony \Component \Messenger \MessageBusInterface ;
4951use Symfony \Component \PropertyInfo \PropertyInfoExtractorInterface ;
5052use Symfony \Component \Validator \Mapping \Loader \LoaderInterface ;
53+ use Symfony \Component \VarExporter \LazyGhostTrait ;
5154
5255use function array_intersect_key ;
5356use function array_keys ;
5760use function reset ;
5861use function sprintf ;
5962use function str_replace ;
63+ use function trait_exists ;
6064
6165/**
6266 * DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
@@ -449,7 +453,30 @@ protected function ormLoad(array $config, ContainerBuilder $container)
449453
450454 $ container ->setParameter ('doctrine.default_entity_manager ' , $ config ['default_entity_manager ' ]);
451455
452- $ options = ['auto_generate_proxy_classes ' , 'proxy_dir ' , 'proxy_namespace ' ];
456+ if ($ options ['enable_lazy_ghost_objects ' ] ?? false ) {
457+ if (! method_exists (OrmConfiguration::class, 'setLazyGhostObjectEnabled ' )) {
458+ throw new LogicException (
459+ 'Lazy ghost objects cannot be enabled because the "doctrine/orm" library '
460+ . ' version 2.14 or higher is not installed. Please run "composer update doctrine/orm". '
461+ );
462+ }
463+
464+ if (! trait_exists (LazyGhostTrait::class)) {
465+ throw new LogicException (
466+ 'Lazy ghost objects cannot be enabled because the "symfony/var-exporter" library '
467+ . ' version 6.2 or higher is not installed. Please run "composer require symfony/var-exporter:^6.2". '
468+ );
469+ }
470+
471+ if (! class_exists (RuntimeReflectionProperty::class)) {
472+ throw new LogicException (
473+ 'Lazy ghost objects cannot be enabled because the "doctrine/persistence" library '
474+ . ' version 3.1 or higher is not installed. Please run "composer update doctrine/persistence". '
475+ );
476+ }
477+ }
478+
479+ $ options = ['auto_generate_proxy_classes ' , 'enable_lazy_ghost_objects ' , 'proxy_dir ' , 'proxy_namespace ' ];
453480 foreach ($ options as $ key ) {
454481 $ container ->setParameter ('doctrine.orm. ' . $ key , $ config [$ key ]);
455482 }
@@ -550,6 +577,7 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
550577 'setProxyDir ' => '%doctrine.orm.proxy_dir% ' ,
551578 'setProxyNamespace ' => '%doctrine.orm.proxy_namespace% ' ,
552579 'setAutoGenerateProxyClasses ' => '%doctrine.orm.auto_generate_proxy_classes% ' ,
580+ 'setLazyGhostObjectEnabled ' => '%doctrine.orm.enable_lazy_ghost_objects% ' ,
553581 'setSchemaIgnoreClasses ' => $ entityManager ['schema_ignore_classes ' ],
554582 'setClassMetadataFactoryName ' => $ entityManager ['class_metadata_factory_name ' ],
555583 'setDefaultRepositoryClassName ' => $ entityManager ['default_repository_class ' ],
@@ -558,6 +586,10 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
558586 'setEntityListenerResolver ' => new Reference (sprintf ('doctrine.orm.%s_entity_listener_resolver ' , $ entityManager ['name ' ])),
559587 ];
560588
589+ if (! method_exists (OrmConfiguration::class, 'setLazyGhostObjectEnabled ' )) {
590+ unset($ methods ['setLazyGhostObjectEnabled ' ]);
591+ }
592+
561593 $ listenerId = sprintf ('doctrine.orm.%s_listeners.attach_entity_listeners ' , $ entityManager ['name ' ]);
562594 $ listenerDef = $ container ->setDefinition ($ listenerId , new Definition ('%doctrine.orm.listeners.attach_entity_listeners.class% ' ));
563595 $ listenerTagParams = ['event ' => 'loadClassMetadata ' ];
0 commit comments