Skip to content

Commit

Permalink
[FramewExtraBundle/DoctrineExtra] allow to enabled/disabled mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jan 16, 2024
1 parent d569919 commit 860e88a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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\\',
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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;
use Draw\DoctrineExtra\ORM\Command\MysqlDumpCommand;
use Draw\DoctrineExtra\ORM\EntityHandler;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Notifier\NotifierInterface;

/**
* @property DoctrineExtraIntegration $integration
Expand All @@ -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, []),
],
];
}
Expand All @@ -44,6 +48,9 @@ public static function provideTestLoad(): iterable
'orm' => [
'enabled' => true,
],
'mongodb_odm' => [
'enabled' => true,
],
],
],
[
Expand Down

0 comments on commit 860e88a

Please sign in to comment.