Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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