diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 698fb84fc769f..c1e567cc0e436 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -2014,6 +2014,9 @@ private function addNotifierSection(ArrayNodeDefinition $rootNode, callable $ena ->arrayNode('notifier') ->info('Notifier configuration') ->{$enableIfStandalone('symfony/notifier', Notifier::class)}() + ->children() + ->scalarNode('message_bus')->defaultNull()->info('The message bus to use. Defaults to the default bus if the Messenger component is installed.')->end() + ->end() ->fixXmlConfig('chatter_transport') ->children() ->arrayNode('chatter_transports') diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 982210c5fcc4b..6092506f9b3a2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -2522,6 +2522,18 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $ $container->removeDefinition('notifier.channel.email'); } + foreach (['texter', 'chatter', 'notifier.channel.chat', 'notifier.channel.email', 'notifier.channel.sms'] as $serviceId) { + if (!$container->hasDefinition($serviceId)) { + continue; + } + + if (false === $messageBus = $config['message_bus']) { + $container->getDefinition($serviceId)->replaceArgument(1, null); + } else { + $container->getDefinition($serviceId)->replaceArgument(1, $messageBus ? new Reference($messageBus) : new Reference('messenger.default_bus', ContainerInterface::NULL_ON_INVALID_REFERENCE)); + } + } + if ($this->isInitializedConfigEnabled('messenger')) { if ($config['notification_on_failed_messages']) { $container->getDefinition('notifier.failed_message_listener')->addTag('kernel.event_subscriber'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php index fde0533140809..2f53ff03de03d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php @@ -52,15 +52,24 @@ ->tag('notifier.channel', ['channel' => 'browser']) ->set('notifier.channel.chat', ChatChannel::class) - ->args([service('chatter.transports'), service('messenger.default_bus')->ignoreOnInvalid()]) + ->args([ + service('chatter.transports'), + abstract_arg('message bus'), + ]) ->tag('notifier.channel', ['channel' => 'chat']) ->set('notifier.channel.sms', SmsChannel::class) - ->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()]) + ->args([ + service('texter.transports'), + abstract_arg('message bus'), + ]) ->tag('notifier.channel', ['channel' => 'sms']) ->set('notifier.channel.email', EmailChannel::class) - ->args([service('mailer.transports'), service('messenger.default_bus')->ignoreOnInvalid()]) + ->args([ + service('mailer.transports'), + abstract_arg('message bus'), + ]) ->tag('notifier.channel', ['channel' => 'email']) ->set('notifier.channel.push', PushChannel::class) @@ -76,7 +85,7 @@ ->set('chatter', Chatter::class) ->args([ service('chatter.transports'), - service('messenger.default_bus')->ignoreOnInvalid(), + abstract_arg('message bus'), service('event_dispatcher')->ignoreOnInvalid(), ]) @@ -96,7 +105,7 @@ ->set('texter', Texter::class) ->args([ service('texter.transports'), - service('messenger.default_bus')->ignoreOnInvalid(), + abstract_arg('message bus'), service('event_dispatcher')->ignoreOnInvalid(), ]) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 59c7bf7a8b875..f37794e3d7fe5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -655,6 +655,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor ], 'notifier' => [ 'enabled' => !class_exists(FullStack::class) && class_exists(Notifier::class), + 'message_bus' => null, 'chatter_transports' => [], 'texter_transports' => [], 'channel_policy' => [], diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/notifier_with_disabled_message_bus.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/notifier_with_disabled_message_bus.php new file mode 100644 index 0000000000000..014b54b94a5dd --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/notifier_with_disabled_message_bus.php @@ -0,0 +1,19 @@ +loadFromExtension('framework', [ + 'messenger' => [ + 'enabled' => true, + ], + 'mailer' => [ + 'dsn' => 'smtp://example.com', + ], + 'notifier' => [ + 'message_bus' => false, + 'chatter_transports' => [ + 'test' => 'null' + ], + 'texter_transports' => [ + 'test' => 'null' + ], + ], +]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/notifier_with_specific_message_bus.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/notifier_with_specific_message_bus.php new file mode 100644 index 0000000000000..75074e073ce29 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/notifier_with_specific_message_bus.php @@ -0,0 +1,19 @@ +loadFromExtension('framework', [ + 'messenger' => [ + 'enabled' => true, + ], + 'mailer' => [ + 'dsn' => 'smtp://example.com', + ], + 'notifier' => [ + 'message_bus' => 'app.another_bus', + 'chatter_transports' => [ + 'test' => 'null' + ], + 'texter_transports' => [ + 'test' => 'null' + ], + ], +]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/notifier_with_disabled_message_bus.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/notifier_with_disabled_message_bus.xml new file mode 100644 index 0000000000000..599bd23cb8f43 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/notifier_with_disabled_message_bus.xml @@ -0,0 +1,17 @@ + + + + + + + + + null + null + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/notifier_with_specific_message_bus.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/notifier_with_specific_message_bus.xml new file mode 100644 index 0000000000000..62373497056ac --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/notifier_with_specific_message_bus.xml @@ -0,0 +1,17 @@ + + + + + + + + + null + null + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/notifier_with_disabled_message_bus.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/notifier_with_disabled_message_bus.yml new file mode 100644 index 0000000000000..08b3d6ad6e759 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/notifier_with_disabled_message_bus.yml @@ -0,0 +1,11 @@ +framework: + messenger: + enabled: true + mailer: + dsn: 'smtp://example.com' + notifier: + message_bus: false + chatter_transports: + test: 'null' + texter_transports: + test: 'null' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/notifier_with_specific_message_bus.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/notifier_with_specific_message_bus.yml new file mode 100644 index 0000000000000..1851717bd9627 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/notifier_with_specific_message_bus.yml @@ -0,0 +1,11 @@ +framework: + messenger: + enabled: true + mailer: + dsn: 'smtp://example.com' + notifier: + message_bus: 'app.another_bus' + chatter_transports: + test: 'null' + texter_transports: + test: 'null' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 640a152402bea..b87a877690815 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -2173,6 +2173,28 @@ public function testHtmlSanitizerDefaultConfig() $this->assertSame('html_sanitizer', (string) $container->getAlias(HtmlSanitizerInterface::class)); } + public function testNotifierWithDisabledMessageBus() + { + $container = $this->createContainerFromFile('notifier_with_disabled_message_bus'); + + $this->assertNull($container->getDefinition('chatter')->getArgument(1)); + $this->assertNull($container->getDefinition('texter')->getArgument(1)); + $this->assertNull($container->getDefinition('notifier.channel.chat')->getArgument(1)); + $this->assertNull($container->getDefinition('notifier.channel.email')->getArgument(1)); + $this->assertNull($container->getDefinition('notifier.channel.sms')->getArgument(1)); + } + + public function testNotifierWithSpecificMessageBus() + { + $container = $this->createContainerFromFile('notifier_with_specific_message_bus'); + + $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('chatter')->getArgument(1)); + $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('texter')->getArgument(1)); + $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('notifier.channel.chat')->getArgument(1)); + $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('notifier.channel.email')->getArgument(1)); + $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('notifier.channel.sms')->getArgument(1)); + } + protected function createContainer(array $data = []) { return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([