From cf4d4459ae6a4335122df2ad932b58901f8ee3cf Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Fri, 11 May 2018 14:22:21 +0200 Subject: [PATCH] [Messenger] Wire the transaction middleware factory when component is enabled --- .../Compiler/MessengerPass.php | 25 ++++++++ DependencyInjection/DoctrineExtension.php | 12 ++++ DoctrineBundle.php | 2 + Resources/config/messenger.xml | 20 +++++++ .../Compiler/MessengerPassTest.php | 58 +++++++++++++++++++ .../DoctrineExtensionTest.php | 21 +++++++ composer.json | 6 +- 7 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 DependencyInjection/Compiler/MessengerPass.php create mode 100644 Resources/config/messenger.xml create mode 100644 Tests/DependencyInjection/Compiler/MessengerPassTest.php diff --git a/DependencyInjection/Compiler/MessengerPass.php b/DependencyInjection/Compiler/MessengerPass.php new file mode 100644 index 000000000..1d51e0f66 --- /dev/null +++ b/DependencyInjection/Compiler/MessengerPass.php @@ -0,0 +1,25 @@ +hasAlias('message_bus') || !is_subclass_of($container->findDefinition('message_bus')->getClass(), MessageBusInterface::class)) { + $container->removeDefinition('doctrine.orm.messenger.middleware_factory.transaction'); + $container->removeDefinition('messenger.middleware.doctrine_transaction_middleware'); + } + } +} diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index b9eaee737..f20d00877 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Version; use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension; use Symfony\Bridge\Doctrine\Form\Type\DoctrineType; +use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -20,6 +21,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Messenger\MessageBus; /** * DoctrineExtension is an extension for the Doctrine DBAL and ORM library. @@ -388,6 +390,16 @@ protected function ormLoad(array $config, ContainerBuilder $container) ->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG); } + // If the Messenger component is installed and the doctrine transaction middleware factory is available, wire it: + if (class_exists(MessageBus::class) && class_exists(DoctrineTransactionMiddlewareFactory::class)) { + $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader->load('messenger.xml'); + + $container->getDefinition('messenger.middleware.doctrine_transaction_middleware') + ->replaceArgument(0, $config['default_entity_manager']) + ; + } + /* * Compatibility for Symfony 3.2 and lower: gives the service a default argument. * When DoctrineBundle requires 3.3 or higher, this can be moved to an anonymous diff --git a/DoctrineBundle.php b/DoctrineBundle.php index 637d6ed97..5c0fe757a 100644 --- a/DoctrineBundle.php +++ b/DoctrineBundle.php @@ -3,6 +3,7 @@ namespace Doctrine\Bundle\DoctrineBundle; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass; +use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MessengerPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; use Doctrine\Common\Util\ClassUtils; use Doctrine\ORM\Proxy\Autoloader; @@ -38,6 +39,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new DoctrineValidationPass('orm')); $container->addCompilerPass(new EntityListenerPass()); $container->addCompilerPass(new ServiceRepositoryCompilerPass()); + $container->addCompilerPass(new MessengerPass()); } /** diff --git a/Resources/config/messenger.xml b/Resources/config/messenger.xml new file mode 100644 index 000000000..c44d38599 --- /dev/null +++ b/Resources/config/messenger.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + diff --git a/Tests/DependencyInjection/Compiler/MessengerPassTest.php b/Tests/DependencyInjection/Compiler/MessengerPassTest.php new file mode 100644 index 000000000..a39d3bdec --- /dev/null +++ b/Tests/DependencyInjection/Compiler/MessengerPassTest.php @@ -0,0 +1,58 @@ +load('messenger.xml'); + + $pass->process($container); + + $this->assertFalse($container->hasDefinition('doctrine.orm.messenger.middleware_factory.transaction')); + $this->assertFalse($container->hasDefinition('messenger.middleware.doctrine_transaction_middleware')); + } + + public function testRemoveDefinitionsWhenHasAliasButNotMessengerComponent() + { + $pass = new MessengerPass(); + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config')); + $loader->load('messenger.xml'); + + $container->register('some_other_bus', \stdClass::class); + $container->setAlias('message_bus', 'some_other_bus'); + + $pass->process($container); + + $this->assertFalse($container->hasDefinition('doctrine.orm.messenger.middleware_factory.transaction')); + $this->assertFalse($container->hasDefinition('messenger.middleware.doctrine_transaction_middleware')); + } + + public function testDoesNotRemoveDefinitionsWhenMessengerComponentIsEnabled() + { + $pass = new MessengerPass(); + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config')); + $loader->load('messenger.xml'); + + $container->register('messenger.bus.default', MessageBus::class); + $container->setAlias('message_bus', 'messenger.bus.default'); + + $pass->process($container); + + $this->assertTrue($container->hasDefinition('doctrine.orm.messenger.middleware_factory.transaction')); + $this->assertTrue($container->hasDefinition('messenger.middleware.doctrine_transaction_middleware')); + } +} diff --git a/Tests/DependencyInjection/DoctrineExtensionTest.php b/Tests/DependencyInjection/DoctrineExtensionTest.php index a3e1c7dbe..3803a8c2c 100644 --- a/Tests/DependencyInjection/DoctrineExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineExtensionTest.php @@ -676,6 +676,27 @@ public function testAnnotationsBundleMappingDetectionWithVendorNamespace() $this->assertEquals('Fixtures\Bundles\Vendor\AnnotationsBundle\Entity', $calls[0][1][1]); } + public function testMessengerIntegration() + { + $container = $this->getContainer(); + $extension = new DoctrineExtension(); + + $config = BundleConfigurationBuilder::createBuilder() + ->addBaseConnection() + ->addEntityManager([ + 'default_entity_manager' => 'default', + 'entity_managers' => [ + 'default' => [], + ], + ]) + ->build(); + $extension->load([$config], $container); + + $this->assertNotNull($container->getDefinition('doctrine.orm.messenger.middleware_factory.transaction')); + $this->assertNotNull($middlewarePrototype = $container->getDefinition('messenger.middleware.doctrine_transaction_middleware')); + $this->assertSame('default', $middlewarePrototype->getArgument(0)); + } + public function testCacheConfiguration() { $container = $this->getContainer(); diff --git a/composer.json b/composer.json index e036fa6a5..0ca83e232 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "twig/twig": "~1.26|~2.0", "satooshi/php-coveralls": "^1.0", "phpunit/phpunit": "^4.8.36|^5.7|^6.4", - "symfony/web-profiler-bundle": "~2.7|~3.0|~4.0" + "symfony/web-profiler-bundle": "~2.7|~3.0|~4.0", + "symfony/messenger": "~4.1-beta2" }, "conflict": { "symfony/http-foundation": "<2.6" @@ -61,5 +62,6 @@ "branch-alias": { "dev-master": "1.9.x-dev" } - } + }, + "minimum-stability": "dev" }