|
15 | 15 | use Doctrine\DBAL\Connection; |
16 | 16 | use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; |
17 | 17 | use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface; |
| 18 | +use Doctrine\ORM\Configuration as OrmConfiguration; |
18 | 19 | use Doctrine\ORM\EntityManager; |
19 | 20 | use Doctrine\ORM\EntityManagerInterface; |
20 | 21 | use Doctrine\ORM\Id\AbstractIdGenerator; |
|
23 | 24 | use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand; |
24 | 25 | use Doctrine\ORM\Tools\Export\ClassMetadataExporter; |
25 | 26 | use Doctrine\ORM\UnitOfWork; |
| 27 | +use Doctrine\Persistence\Reflection\RuntimeReflectionProperty; |
26 | 28 | use InvalidArgumentException; |
27 | 29 | use LogicException; |
28 | 30 | use ReflectionMethod; |
|
48 | 50 | use Symfony\Component\Messenger\MessageBusInterface; |
49 | 51 | use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; |
50 | 52 | use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; |
| 53 | +use Symfony\Component\VarExporter\LazyGhostTrait; |
51 | 54 |
|
52 | 55 | use function array_intersect_key; |
53 | 56 | use function array_keys; |
54 | 57 | use function class_exists; |
55 | 58 | use function interface_exists; |
56 | 59 | use function is_dir; |
| 60 | +use function method_exists; |
57 | 61 | use function reset; |
58 | 62 | use function sprintf; |
59 | 63 | use function str_replace; |
| 64 | +use function trait_exists; |
60 | 65 |
|
61 | 66 | /** |
62 | 67 | * DoctrineExtension is an extension for the Doctrine DBAL and ORM library. |
@@ -449,7 +454,32 @@ protected function ormLoad(array $config, ContainerBuilder $container) |
449 | 454 |
|
450 | 455 | $container->setParameter('doctrine.default_entity_manager', $config['default_entity_manager']); |
451 | 456 |
|
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']; |
453 | 483 | foreach ($options as $key) { |
454 | 484 | $container->setParameter('doctrine.orm.' . $key, $config[$key]); |
455 | 485 | } |
@@ -556,8 +586,13 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $ |
556 | 586 | 'setNamingStrategy' => new Reference($entityManager['naming_strategy']), |
557 | 587 | 'setQuoteStrategy' => new Reference($entityManager['quote_strategy']), |
558 | 588 | 'setEntityListenerResolver' => new Reference(sprintf('doctrine.orm.%s_entity_listener_resolver', $entityManager['name'])), |
| 589 | + 'setLazyGhostObjectEnabled' => '%doctrine.orm.enable_lazy_ghost_objects%', |
559 | 590 | ]; |
560 | 591 |
|
| 592 | + if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) { |
| 593 | + unset($methods['setLazyGhostObjectEnabled']); |
| 594 | + } |
| 595 | + |
561 | 596 | $listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $entityManager['name']); |
562 | 597 | $listenerDef = $container->setDefinition($listenerId, new Definition('%doctrine.orm.listeners.attach_entity_listeners.class%')); |
563 | 598 | $listenerTagParams = ['event' => 'loadClassMetadata']; |
|
0 commit comments