Skip to content

Commit 79fb012

Browse files
authored
Merge pull request #1568 from nicolas-grekas/lazy-👻
Add option "doctrine.orm.enable_lazy_ghost_objects"
2 parents 428d543 + 4e235c8 commit 79fb012

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-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: 36 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,15 +50,18 @@
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;
5457
use function class_exists;
5558
use function interface_exists;
5659
use function is_dir;
60+
use function method_exists;
5761
use function reset;
5862
use function sprintf;
5963
use function str_replace;
64+
use function trait_exists;
6065

6166
/**
6267
* DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
@@ -449,7 +454,32 @@ protected function ormLoad(array $config, ContainerBuilder $container)
449454

450455
$container->setParameter('doctrine.default_entity_manager', $config['default_entity_manager']);
451456

452-
$options = ['auto_generate_proxy_classes', 'proxy_dir', 'proxy_namespace'];
457+
if ($config['enable_lazy_ghost_objects'] ?? false) {
458+
if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) {
459+
throw new LogicException(
460+
'Lazy ghost objects cannot be enabled because the "doctrine/orm" library'
461+
. ' version 2.14 or higher is not installed. Please run "composer update doctrine/orm".'
462+
);
463+
}
464+
465+
// available in Symfony 6.2 and higher
466+
/** @psalm-suppress UndefinedClass */
467+
if (! trait_exists(LazyGhostTrait::class)) {
468+
throw new LogicException(
469+
'Lazy ghost objects cannot be enabled because the "symfony/var-exporter" library'
470+
. ' version 6.2 or higher is not installed. Please run "composer require symfony/var-exporter:^6.2".'
471+
);
472+
}
473+
474+
if (! class_exists(RuntimeReflectionProperty::class)) {
475+
throw new LogicException(
476+
'Lazy ghost objects cannot be enabled because the "doctrine/persistence" library'
477+
. ' version 3.1 or higher is not installed. Please run "composer update doctrine/persistence".'
478+
);
479+
}
480+
}
481+
482+
$options = ['auto_generate_proxy_classes', 'enable_lazy_ghost_objects', 'proxy_dir', 'proxy_namespace'];
453483
foreach ($options as $key) {
454484
$container->setParameter('doctrine.orm.' . $key, $config[$key]);
455485
}
@@ -556,8 +586,13 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
556586
'setNamingStrategy' => new Reference($entityManager['naming_strategy']),
557587
'setQuoteStrategy' => new Reference($entityManager['quote_strategy']),
558588
'setEntityListenerResolver' => new Reference(sprintf('doctrine.orm.%s_entity_listener_resolver', $entityManager['name'])),
589+
'setLazyGhostObjectEnabled' => '%doctrine.orm.enable_lazy_ghost_objects%',
559590
];
560591

592+
if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) {
593+
unset($methods['setLazyGhostObjectEnabled']);
594+
}
595+
561596
$listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $entityManager['name']);
562597
$listenerDef = $container->setDefinition($listenerId, new Definition('%doctrine.orm.listeners.attach_entity_listeners.class%'));
563598
$listenerTagParams = ['event' => 'loadClassMetadata'];

0 commit comments

Comments
 (0)