From ad43f4189d65ed4111648435f10a8dc4fd933f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Tue, 16 Jan 2024 17:08:29 -0500 Subject: [PATCH] [FrameworkExtraBundle/DoctrineExtra] allow to enable/disable mongodb --- .../Integration/DoctrineExtraIntegration.php | 37 +++++++++++++++---- .../DoctrineExtraIntegrationTest.php | 11 +++++- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/packages/framework-extra-bundle/DependencyInjection/Integration/DoctrineExtraIntegration.php b/packages/framework-extra-bundle/DependencyInjection/Integration/DoctrineExtraIntegration.php index 54a02087..be11d4fa 100644 --- a/packages/framework-extra-bundle/DependencyInjection/Integration/DoctrineExtraIntegration.php +++ b/packages/framework-extra-bundle/DependencyInjection/Integration/DoctrineExtraIntegration.php @@ -2,13 +2,14 @@ namespace Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration; +use Doctrine\ODM\MongoDB\DocumentManager; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\ManagerRegistry; use Draw\DoctrineExtra\ORM\EntityHandler; use Draw\DoctrineExtra\ORM\Query\CommentSqlWalker; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; -use Symfony\Component\Notifier\NotifierInterface; class DoctrineExtraIntegration implements IntegrationInterface { @@ -21,13 +22,8 @@ public function getConfigSectionName(): string public function load(array $config, PhpFileLoader $loader, ContainerBuilder $container): void { - $container - ->registerAliasForArgument('doctrine', ManagerRegistry::class, 'ormManagerRegistry'); - - $container - ->registerAliasForArgument('doctrine_mongodb', ManagerRegistry::class, 'odmManagerRegistry'); - $this->loadORM($config['orm'], $loader, $container); + $this->loadMongoODM($config['mongodb_odm'], $loader, $container); } private function loadORM(array $config, PhpFileLoader $loader, ContainerBuilder $container): void @@ -36,6 +32,9 @@ private function loadORM(array $config, PhpFileLoader $loader, ContainerBuilder return; } + $container + ->registerAliasForArgument('doctrine', ManagerRegistry::class, 'ormManagerRegistry'); + $this->registerClasses( $loader, $namespace = 'Draw\\DoctrineExtra\\ORM\\', @@ -55,19 +54,41 @@ private function loadORM(array $config, PhpFileLoader $loader, ContainerBuilder ); } + private function loadMongoODM(array $config, PhpFileLoader $loader, ContainerBuilder $container): void + { + if (!$this->isConfigEnabled($container, $config)) { + return; + } + + $container + ->registerAliasForArgument('doctrine_mongodb', ManagerRegistry::class, 'odmManagerRegistry'); + } + public function addConfiguration(ArrayNodeDefinition $node): void { $node ->children() ->append($this->createORMNode()) + ->append($this->createMongoODMNode()) ->end(); } + private function createMongoODMNode(): ArrayNodeDefinition + { + $node = new ArrayNodeDefinition('mongodb_odm'); + + ContainerBuilder::willBeAvailable('doctrine/mongodb-odm', DocumentManager::class, []) + ? $node->canBeDisabled() + : $node->canBeEnabled(); + + return $node; + } + private function createORMNode(): ArrayNodeDefinition { $node = new ArrayNodeDefinition('orm'); - ContainerBuilder::willBeAvailable('doctrine/orm', NotifierInterface::class, []) + ContainerBuilder::willBeAvailable('doctrine/orm', EntityManagerInterface::class, []) ? $node->canBeDisabled() : $node->canBeEnabled(); diff --git a/packages/framework-extra-bundle/Tests/DependencyInjection/Integration/DoctrineExtraIntegrationTest.php b/packages/framework-extra-bundle/Tests/DependencyInjection/Integration/DoctrineExtraIntegrationTest.php index 33ccd81c..efd73020 100644 --- a/packages/framework-extra-bundle/Tests/DependencyInjection/Integration/DoctrineExtraIntegrationTest.php +++ b/packages/framework-extra-bundle/Tests/DependencyInjection/Integration/DoctrineExtraIntegrationTest.php @@ -2,6 +2,8 @@ namespace Draw\Bundle\FrameworkExtraBundle\Tests\DependencyInjection\Integration; +use Doctrine\ODM\MongoDB\DocumentManager; +use Doctrine\ORM\EntityManagerInterface; use Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration\DoctrineExtraIntegration; use Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration\IntegrationInterface; use Draw\DoctrineExtra\ORM\Command\ImportFileCommand; @@ -9,7 +11,6 @@ use Draw\DoctrineExtra\ORM\EntityHandler; use PHPUnit\Framework\Attributes\CoversClass; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\Notifier\NotifierInterface; /** * @property DoctrineExtraIntegration $integration @@ -31,7 +32,10 @@ public function getDefaultConfiguration(): array { return [ 'orm' => [ - 'enabled' => ContainerBuilder::willBeAvailable('doctrine/orm', NotifierInterface::class, []), + 'enabled' => ContainerBuilder::willBeAvailable('doctrine/orm', EntityManagerInterface::class, []), + ], + 'mongodb_odm' => [ + 'enabled' => ContainerBuilder::willBeAvailable('doctrine/mongodb-odm', DocumentManager::class, []), ], ]; } @@ -44,6 +48,9 @@ public static function provideTestLoad(): iterable 'orm' => [ 'enabled' => true, ], + 'mongodb_odm' => [ + 'enabled' => true, + ], ], ], [