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 4e235c8
Show file tree
Hide file tree
Showing 2 changed files with 39 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
37 changes: 36 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;

This comment has been minimized.

Copy link
@segli

segli Jan 12, 2023

Hi @nicolas-grekas
RuntimeReflectionProperty was added by you in doctrine/persistence 3.1.0. That would remove doctrine/persistence:^2.2 as dependency, correct?

"doctrine/persistence": "^2.2 || ^3",

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Jan 12, 2023

Author Member

Not really, the code is conditionally using the class, see class_exists(RuntimeReflectionProperty::class) below.

use InvalidArgumentException;
use LogicException;
use ReflectionMethod;
Expand All @@ -48,15 +50,18 @@
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;
use function class_exists;
use function interface_exists;
use function is_dir;
use function method_exists;
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 +454,32 @@ 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 ($config['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".'
);
}

// available in Symfony 6.2 and higher
/** @psalm-suppress UndefinedClass */
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 @@ -556,8 +586,13 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
'setNamingStrategy' => new Reference($entityManager['naming_strategy']),
'setQuoteStrategy' => new Reference($entityManager['quote_strategy']),
'setEntityListenerResolver' => new Reference(sprintf('doctrine.orm.%s_entity_listener_resolver', $entityManager['name'])),
'setLazyGhostObjectEnabled' => '%doctrine.orm.enable_lazy_ghost_objects%',
];

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 4e235c8

Please sign in to comment.