Skip to content

Commit 5e69d5f

Browse files
Add option "doctrine.orm.enable_lazy_ghost_objects"
1 parent 428d543 commit 5e69d5f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ private function addOrmSection(ArrayNodeDefinition $node): void
384384
$excludedKeys = [
385385
'default_entity_manager' => true,
386386
'auto_generate_proxy_classes' => true,
387+
'enable_lazy_ghost_objects' => true,
387388
'proxy_dir' => true,
388389
'proxy_namespace' => true,
389390
'resolve_target_entities' => true,
@@ -439,6 +440,8 @@ private function addOrmSection(ArrayNodeDefinition $node): void
439440
})
440441
->end()
441442
->end()
443+
->booleanNode('enable_lazy_ghost_objects')->defaultFalse()
444+
->end()
442445
->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/orm/Proxies')->end()
443446
->scalarNode('proxy_namespace')->defaultValue('Proxies')->end()
444447
->arrayNode('controller_resolver')

DependencyInjection/DoctrineExtension.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Connection;
1616
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
1717
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;
18+
use Doctrine\ORM\Configuration as OrmConfiguration;
1819
use Doctrine\ORM\EntityManager;
1920
use Doctrine\ORM\EntityManagerInterface;
2021
use Doctrine\ORM\Id\AbstractIdGenerator;
@@ -23,6 +24,7 @@
2324
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
2425
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
2526
use Doctrine\ORM\UnitOfWork;
27+
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
2628
use InvalidArgumentException;
2729
use LogicException;
2830
use ReflectionMethod;
@@ -48,6 +50,7 @@
4850
use Symfony\Component\Messenger\MessageBusInterface;
4951
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
5052
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
53+
use Symfony\Component\VarExporter\LazyGhostTrait;
5154

5255
use function array_intersect_key;
5356
use function array_keys;
@@ -57,6 +60,7 @@
5760
use function reset;
5861
use function sprintf;
5962
use 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

Comments
 (0)