From b4a909cfdb12b2631fc0454263bc6dfab026f05e Mon Sep 17 00:00:00 2001 From: Lctrs Date: Thu, 8 Aug 2019 12:01:55 +0200 Subject: [PATCH] Add a service to reset doctrine/odm identity maps --- composer.json | 5 ++- docs/bundle/config_reference.md | 1 + .../DependencyInjection/Configuration.php | 1 + .../DependencyInjection/EnqueueExtension.php | 24 +++++++++++ .../DependencyInjection/ConfigurationTest.php | 43 +++++++++++++++++++ .../EnqueueExtensionTest.php | 36 ++++++++++++++++ .../Tests/fix_composer_json.php | 1 + pkg/enqueue-bundle/composer.json | 2 + 8 files changed, 112 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c238f3148..6a115bf02 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,8 @@ "symfony/framework-bundle": "^3.4|^4", "empi89/php-amqp-stubs": "*@dev", "doctrine/doctrine-bundle": "~1.2", + "doctrine/mongodb-odm-bundle": "^3.5", + "alcaeus/mongo-php-adapter": "^1.0", "kwn/php-rdkafka-stubs": "^1.0.2", "friendsofphp/php-cs-fixer": "^2" }, @@ -112,7 +114,8 @@ "ext-rdkafka": "3.3", "ext-mongodb": "1.3", "ext-bcmath": "1", - "ext-mbstring": "1" + "ext-mbstring": "1", + "ext-mongo": "1.6.14" }, "prefer-stable": true } diff --git a/docs/bundle/config_reference.md b/docs/bundle/config_reference.md index 47f193fcc..a549ceda1 100644 --- a/docs/bundle/config_reference.md +++ b/docs/bundle/config_reference.md @@ -69,6 +69,7 @@ enqueue: extensions: doctrine_ping_connection_extension: false doctrine_clear_identity_map_extension: false + doctrine_odm_clear_identity_map_extension: false reset_services_extension: false signal_extension: true reply_extension: true diff --git a/pkg/enqueue-bundle/DependencyInjection/Configuration.php b/pkg/enqueue-bundle/DependencyInjection/Configuration.php index d0e1e1fdf..3b26f5ad4 100644 --- a/pkg/enqueue-bundle/DependencyInjection/Configuration.php +++ b/pkg/enqueue-bundle/DependencyInjection/Configuration.php @@ -47,6 +47,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->arrayNode('extensions')->addDefaultsIfNotSet()->children() ->booleanNode('doctrine_ping_connection_extension')->defaultFalse()->end() ->booleanNode('doctrine_clear_identity_map_extension')->defaultFalse()->end() + ->booleanNode('doctrine_odm_clear_identity_map_extension')->defaultFalse()->end() ->booleanNode('reset_services_extension')->defaultFalse()->end() ->booleanNode('signal_extension')->defaultValue(function_exists('pcntl_signal_dispatch'))->end() ->booleanNode('reply_extension')->defaultTrue()->end() diff --git a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php index 623be15c4..620e71925 100644 --- a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php +++ b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php @@ -137,6 +137,7 @@ public function load(array $configs, ContainerBuilder $container): void // extensions $this->loadDoctrinePingConnectionExtension($config, $container); $this->loadDoctrineClearIdentityMapExtension($config, $container); + $this->loadDoctrineOdmClearIdentityMapExtension($config, $container); $this->loadResetServicesExtension($config, $container); $this->loadSignalExtension($config, $container); $this->loadReplyExtension($config, $container); @@ -249,6 +250,29 @@ private function loadDoctrineClearIdentityMapExtension(array $config, ContainerB } } + private function loadDoctrineOdmClearIdentityMapExtension(array $config, ContainerBuilder $container): void + { + $configNames = []; + foreach ($config as $name => $modules) { + if ($modules['extensions']['doctrine_odm_clear_identity_map_extension']) { + $configNames[] = $name; + } + } + + if ([] === $configNames) { + return; + } + + $extension = $container->register('enqueue.consumption.doctrine_odm_clear_identity_map_extension', DoctrineClearIdentityMapExtension::class) + ->addArgument(new Reference('doctrine_mongodb')) + ; + + foreach ($configNames as $name) { + $extension->addTag('enqueue.consumption_extension', ['client' => $name]); + $extension->addTag('enqueue.transport.consumption_extension', ['transport' => $name]); + } + } + private function loadResetServicesExtension(array $config, ContainerBuilder $container) { $configNames = []; diff --git a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php index e3073a820..6e2b47dcf 100644 --- a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php @@ -336,6 +336,49 @@ public function testDoctrineClearIdentityMapExtensionCouldBeEnabled() ], $config); } + public function testDoctrineOdmClearIdentityMapExtensionShouldBeDisabledByDefault() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => null, + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => false, + ], + ], + ], $config); + } + + public function testDoctrineOdmClearIdentityMapExtensionCouldBeEnabled() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => true, + ], + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => true, + ], + ], + ], $config); + } + public function testResetServicesExtensionShouldBeDisabledByDefault() { $configuration = new Configuration(true); diff --git a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php index 0365feb1d..ab1eb2967 100644 --- a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php @@ -384,6 +384,42 @@ public function testShouldNotLoadDoctrineClearIdentityMapExtensionServiceIfDisab self::assertFalse($container->hasDefinition('enqueue.consumption.doctrine_clear_identity_map_extension')); } + public function testShouldLoadDoctrineOdmClearIdentityMapExtensionServiceIfEnabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => true, + ], + ], + ]], $container); + + self::assertTrue($container->hasDefinition('enqueue.consumption.doctrine_odm_clear_identity_map_extension')); + } + + public function testShouldNotLoadDoctrineOdmClearIdentityMapExtensionServiceIfDisabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => false, + ], + ], + ]], $container); + + self::assertFalse($container->hasDefinition('enqueue.consumption.doctrine_odm_clear_identity_map_extension')); + } + public function testShouldLoadResetServicesExtensionServiceIfEnabled() { $container = $this->getContainerBuilder(true); diff --git a/pkg/enqueue-bundle/Tests/fix_composer_json.php b/pkg/enqueue-bundle/Tests/fix_composer_json.php index f025f6081..6cc24aa03 100644 --- a/pkg/enqueue-bundle/Tests/fix_composer_json.php +++ b/pkg/enqueue-bundle/Tests/fix_composer_json.php @@ -5,5 +5,6 @@ $composerJson = json_decode(file_get_contents(__DIR__.'/../composer.json'), true); $composerJson['config']['platform']['ext-amqp'] = '1.9.3'; +$composerJson['config']['platform']['ext-mongo'] = '1.6.14'; file_put_contents(__DIR__.'/../composer.json', json_encode($composerJson, JSON_PRETTY_PRINT)); diff --git a/pkg/enqueue-bundle/composer.json b/pkg/enqueue-bundle/composer.json index 63620a3b5..49a637ab3 100644 --- a/pkg/enqueue-bundle/composer.json +++ b/pkg/enqueue-bundle/composer.json @@ -37,6 +37,8 @@ "enqueue/async-command": "0.9.x-dev", "php-amqplib/php-amqplib": "^2.7", "doctrine/doctrine-bundle": "~1.2", + "doctrine/mongodb-odm-bundle": "^3.5", + "alcaeus/mongo-php-adapter": "^1.0", "symfony/browser-kit": "^3.4|^4", "symfony/expression-language": "^3.4|^4" },