Skip to content

Commit

Permalink
Add option "doctrine.orm.enable_lazy_ghost_objects"
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Dec 21, 2022
1 parent 428d543 commit 5e69d5f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ private function addOrmSection(ArrayNodeDefinition $node): void
$excludedKeys = [
'default_entity_manager' => true,
'auto_generate_proxy_classes' => true,
'enable_lazy_ghost_objects' => true,
'proxy_dir' => true,
'proxy_namespace' => true,
'resolve_target_entities' => true,
Expand Down Expand Up @@ -439,6 +440,8 @@ private function addOrmSection(ArrayNodeDefinition $node): void
})
->end()
->end()
->booleanNode('enable_lazy_ghost_objects')->defaultFalse()
->end()
->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/orm/Proxies')->end()
->scalarNode('proxy_namespace')->defaultValue('Proxies')->end()
->arrayNode('controller_resolver')
Expand Down
34 changes: 33 additions & 1 deletion DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;
use Doctrine\ORM\Configuration as OrmConfiguration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Id\AbstractIdGenerator;
Expand All @@ -23,6 +24,7 @@
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use InvalidArgumentException;
use LogicException;
use ReflectionMethod;
Expand All @@ -48,6 +50,7 @@
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function array_intersect_key;
use function array_keys;
Expand All @@ -57,6 +60,7 @@
use function reset;
use function sprintf;
use function str_replace;
use function trait_exists;

/**
* DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
Expand Down Expand Up @@ -449,7 +453,30 @@ protected function ormLoad(array $config, ContainerBuilder $container)

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

$options = ['auto_generate_proxy_classes', 'proxy_dir', 'proxy_namespace'];
if ($options['enable_lazy_ghost_objects'] ?? false) {
if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) {
throw new LogicException(
'Lazy ghost objects cannot be enabled because the "doctrine/orm" library'
. ' version 2.14 or higher is not installed. Please run "composer update doctrine/orm".'
);
}

if (! trait_exists(LazyGhostTrait::class)) {
throw new LogicException(
'Lazy ghost objects cannot be enabled because the "symfony/var-exporter" library'
. ' version 6.2 or higher is not installed. Please run "composer require symfony/var-exporter:^6.2".'
);
}

if (! class_exists(RuntimeReflectionProperty::class)) {
throw new LogicException(
'Lazy ghost objects cannot be enabled because the "doctrine/persistence" library'
. ' version 3.1 or higher is not installed. Please run "composer update doctrine/persistence".'
);
}
}

$options = ['auto_generate_proxy_classes', 'enable_lazy_ghost_objects', 'proxy_dir', 'proxy_namespace'];
foreach ($options as $key) {
$container->setParameter('doctrine.orm.' . $key, $config[$key]);
}
Expand Down Expand Up @@ -550,6 +577,7 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
'setProxyDir' => '%doctrine.orm.proxy_dir%',
'setProxyNamespace' => '%doctrine.orm.proxy_namespace%',
'setAutoGenerateProxyClasses' => '%doctrine.orm.auto_generate_proxy_classes%',
'setLazyGhostObjectEnabled' => '%doctrine.orm.enable_lazy_ghost_objects%',
'setSchemaIgnoreClasses' => $entityManager['schema_ignore_classes'],
'setClassMetadataFactoryName' => $entityManager['class_metadata_factory_name'],
'setDefaultRepositoryClassName' => $entityManager['default_repository_class'],
Expand All @@ -558,6 +586,10 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
'setEntityListenerResolver' => new Reference(sprintf('doctrine.orm.%s_entity_listener_resolver', $entityManager['name'])),
];

if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) {
unset($methods['setLazyGhostObjectEnabled']);
}

$listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $entityManager['name']);
$listenerDef = $container->setDefinition($listenerId, new Definition('%doctrine.orm.listeners.attach_entity_listeners.class%'));
$listenerTagParams = ['event' => 'loadClassMetadata'];
Expand Down

0 comments on commit 5e69d5f

Please sign in to comment.