Skip to content

Commit

Permalink
Use LazyGhostObject for proxy objects (#877)
Browse files Browse the repository at this point in the history
Add use_lazy_ghost_object configuration
  • Loading branch information
GromNaN authored Jan 29, 2025
1 parent 783d281 commit 6cd47b8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
3 changes: 1 addition & 2 deletions config/mongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\Bundle\MongoDBBundle\Repository\ContainerRepositoryFactory;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Tools\ResolveTargetDocumentListener;
use ProxyManager\Proxy\GhostObjectInterface;
use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -54,7 +53,7 @@
'%doctrine_mongodb.odm.document_managers%',
'%doctrine_mongodb.odm.default_connection%',
'%doctrine_mongodb.odm.default_document_manager%',
GhostObjectInterface::class,
abstract_arg('Proxy Interface Name'),
service('service_container'),
])

Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function is_array;
use function is_string;
use function json_decode;
use function method_exists;
use function preg_match;

/**
Expand All @@ -39,6 +40,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('proxy_namespace')->defaultValue('MongoDBODMProxies')->end()
->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/odm/mongodb/Proxies')->end()
->booleanNode('enable_lazy_ghost_objects')
->defaultValue(method_exists(ODMConfiguration::class, 'setUseLazyGhostObject'))

Check failure on line 44 in src/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Call to function method_exists() with 'Doctrine\\ODM\\MongoDB\\Configuration' and 'setUseLazyGhostObje…' will always evaluate to true.
->validate()
->ifTrue(static fn ($v) => $v === true && ! method_exists(ODMConfiguration::class, 'setUseLazyGhostObject'))

Check failure on line 46 in src/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Call to function method_exists() with 'Doctrine\\ODM\\MongoDB\\Configuration' and 'setUseLazyGhostObje…' will always evaluate to true.
->thenInvalid('Lazy ghost objects require doctrine/mongodb-odm 2.10 or higher.')
->end()
->end()
->scalarNode('auto_generate_proxy_classes')
->defaultValue(ODMConfiguration::AUTOGENERATE_EVAL)
->beforeNormalization()
Expand Down
16 changes: 14 additions & 2 deletions src/DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Persistence\Proxy;
use InvalidArgumentException;
use MongoDB\Client;
use ProxyManager\Proxy\LazyLoadingInterface;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
Expand Down Expand Up @@ -106,6 +108,10 @@ public function load(array $configs, ContainerBuilder $container): void
$container->removeDefinition('doctrine_mongodb.odm.command.load_data_fixtures');
}

// Requires doctrine/mongodb-odm 2.10
$container->getDefinition('doctrine_mongodb')
->setArgument(5, $config['enable_lazy_ghost_objects'] ? Proxy::class : LazyLoadingInterface::class);

// load the connections
$this->loadConnections($config['connections'], $container);

Expand All @@ -117,6 +123,7 @@ public function load(array $configs, ContainerBuilder $container): void
$config['default_document_manager'],
$config['default_database'],
$container,
$config['enable_lazy_ghost_objects'],
);

if ($config['resolve_target_documents']) {
Expand Down Expand Up @@ -198,7 +205,7 @@ protected function overrideParameters(array $options, ContainerBuilder $containe
* @param string $defaultDB The default db name
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadDocumentManagers(array $dmConfigs, string|null $defaultDM, string $defaultDB, ContainerBuilder $container): void
protected function loadDocumentManagers(array $dmConfigs, string|null $defaultDM, string $defaultDB, ContainerBuilder $container, bool $useLazyGhostObject = false): void
{
$dms = [];
foreach ($dmConfigs as $name => $documentManager) {
Expand All @@ -208,6 +215,7 @@ protected function loadDocumentManagers(array $dmConfigs, string|null $defaultDM
$defaultDM,
$defaultDB,
$container,
$useLazyGhostObject,
);
$dms[$name] = sprintf('doctrine_mongodb.odm.%s_document_manager', $name);
}
Expand All @@ -223,7 +231,7 @@ protected function loadDocumentManagers(array $dmConfigs, string|null $defaultDM
* @param string $defaultDB The default db name
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadDocumentManager(array $documentManager, string|null $defaultDM, string $defaultDB, ContainerBuilder $container): void
protected function loadDocumentManager(array $documentManager, string|null $defaultDM, string $defaultDB, ContainerBuilder $container, bool $useLazyGhostObject = false): void
{
$connectionName = $documentManager['connection'] ?? $documentManager['name'];
$configurationId = sprintf('doctrine_mongodb.odm.%s_configuration', $documentManager['name']);
Expand Down Expand Up @@ -257,6 +265,10 @@ protected function loadDocumentManager(array $documentManager, string|null $defa
'setAutoGeneratePersistentCollectionClasses' => '%doctrine_mongodb.odm.auto_generate_persistent_collection_classes%',
];

if ($useLazyGhostObject) {
$methods['setUseLazyGhostObject'] = $useLazyGhostObject;
}

if (method_exists(ODMConfiguration::class, 'setUseTransactionalFlush')) {
$methods['setUseTransactionalFlush'] = $documentManager['use_transactional_flush'];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use function array_key_exists;
use function file_get_contents;
use function method_exists;

class ConfigurationTest extends TestCase
{
Expand All @@ -37,6 +38,7 @@ public function testDefaults(): void
'auto_generate_hydrator_classes' => false,
'auto_generate_proxy_classes' => ODMConfiguration::AUTOGENERATE_EVAL,
'auto_generate_persistent_collection_classes' => ODMConfiguration::AUTOGENERATE_NEVER,
'enable_lazy_ghost_objects' => method_exists(ODMConfiguration::class, 'setUseLazyGhostObject'),

Check failure on line 41 in tests/DependencyInjection/ConfigurationTest.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Call to function method_exists() with 'Doctrine\\ODM\\MongoDB\\Configuration' and 'setUseLazyGhostObje…' will always evaluate to true.
'default_database' => 'default',
'document_managers' => [],
'connections' => [],
Expand Down Expand Up @@ -69,6 +71,7 @@ public function testFullConfiguration(array $config): void
'auto_generate_hydrator_classes' => 1,
'auto_generate_proxy_classes' => ODMConfiguration::AUTOGENERATE_FILE_NOT_EXISTS,
'auto_generate_persistent_collection_classes' => ODMConfiguration::AUTOGENERATE_EVAL,
'enable_lazy_ghost_objects' => method_exists(ODMConfiguration::class, 'setUseLazyGhostObject'),

Check failure on line 74 in tests/DependencyInjection/ConfigurationTest.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Call to function method_exists() with 'Doctrine\\ODM\\MongoDB\\Configuration' and 'setUseLazyGhostObje…' will always evaluate to true.
'default_connection' => 'conn1',
'default_database' => 'default_db_name',
'default_document_manager' => 'default_dm_name',
Expand Down

0 comments on commit 6cd47b8

Please sign in to comment.