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: 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')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->defaultValue(method_exists(ODMConfiguration::class, 'setUseLazyGhostObject'))
->validate()
->ifTrue(static fn ($v) => $v === true && ! method_exists(ODMConfiguration::class, 'setUseLazyGhostObject'))
->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 @@
$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 @@
$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 @@
* @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 @@
$defaultDM,
$defaultDB,
$container,
$useLazyGhostObject,
);
$dms[$name] = sprintf('doctrine_mongodb.odm.%s_document_manager', $name);
}
Expand All @@ -223,7 +231,7 @@
* @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 @@
'setAutoGeneratePersistentCollectionClasses' => '%doctrine_mongodb.odm.auto_generate_persistent_collection_classes%',
];

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

Check warning on line 269 in src/DependencyInjection/DoctrineMongoDBExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/DoctrineMongoDBExtension.php#L269

Added line #L269 was not covered by tests
}

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'),
'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'),
'default_connection' => 'conn1',
'default_database' => 'default_db_name',
'default_document_manager' => 'default_dm_name',
Expand Down
Loading