Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04d6d8d

Browse files
committed
feature #34648 [Mailer] Allow to configure or disable the message bus to use (ogizanagi)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Mailer] Allow to configure or disable the message bus to use | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | #34633 <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | todo A new `mailer.message_bus` option allowing to choose the message bus to use instead of using the default one. Also allows to set it to `false` so no message bus is used and the transport will be called directly. Commits ------- 42fd0cf [Mailer] Allow to configure or disable the message bus to use
2 parents 270247a + 42fd0cf commit 04d6d8d

13 files changed

+77
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
88
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.
9+
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
910

1011
5.0.0
1112
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
14751475
->end()
14761476
->fixXmlConfig('transport')
14771477
->children()
1478+
->scalarNode('message_bus')->defaultNull()->info('The message bus to use. Defaults to the default bus if the Messenger component is installed.')->end()
14781479
->scalarNode('dsn')->defaultNull()->end()
14791480
->arrayNode('transports')
14801481
->useAttributeAsKey('name')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,13 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
18841884
$container->getDefinition('mailer.transports')->setArgument(0, $transports);
18851885
$container->getDefinition('mailer.default_transport')->setArgument(0, current($transports));
18861886

1887+
$mailer = $container->getDefinition('mailer.mailer');
1888+
if (false === $messageBus = $config['message_bus']) {
1889+
$mailer->replaceArgument(1, null);
1890+
} else {
1891+
$mailer->replaceArgument(1, $messageBus ? new Reference($messageBus) : new Reference('messenger.default_bus', ContainerInterface::NULL_ON_INVALID_REFERENCE));
1892+
}
1893+
18871894
$classToServices = [
18881895
SesTransportFactory::class => 'mailer.transport_factory.amazon',
18891896
GmailTransportFactory::class => 'mailer.transport_factory.gmail',

src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<services>
88
<service id="mailer.mailer" class="Symfony\Component\Mailer\Mailer">
99
<argument type="service" id="mailer.transports" />
10-
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
10+
<argument /> <!-- message bus-->
1111
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
1212
</service>
1313
<service id="mailer" alias="mailer.mailer" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@
549549
<xsd:element name="envelope" type="mailer_envelope" minOccurs="0" maxOccurs="1" />
550550
</xsd:sequence>
551551
<xsd:attribute name="dsn" type="xsd:string" />
552+
<xsd:attribute name="message-bus" type="xsd:string" />
552553
</xsd:complexType>
553554

554555
<xsd:complexType name="mailer_envelope">

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
490490
'dsn' => null,
491491
'transports' => [],
492492
'enabled' => !class_exists(FullStack::class) && class_exists(Mailer::class),
493+
'message_bus' => null,
493494
],
494495
'notifier' => [
495496
'enabled' => !class_exists(FullStack::class) && class_exists(Notifier::class),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'mailer' => [
5+
'dsn' => 'smtp://example.com',
6+
'message_bus' => false,
7+
],
8+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'mailer' => [
5+
'dsn' => 'smtp://example.com',
6+
'message_bus' => 'app.another_bus',
7+
],
8+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:mailer dsn="smtp://example.com" message-bus="false">
11+
</framework:mailer>
12+
</framework:config>
13+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:mailer dsn="smtp://example.com" message-bus="app.another_bus">
11+
</framework:mailer>
12+
</framework:config>
13+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
mailer:
3+
dsn: 'smtp://example.com'
4+
message_bus: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
mailer:
3+
dsn: 'smtp://example.com'
4+
message_bus: app.another_bus

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,21 @@ public function testMailer(): void
13671367
$l = $container->getDefinition('mailer.envelope_listener');
13681368
$this->assertSame('[email protected]', $l->getArgument(0));
13691369
$this->assertSame(['[email protected]', '[email protected]'], $l->getArgument(1));
1370+
$this->assertEquals(new Reference('messenger.default_bus', ContainerInterface::NULL_ON_INVALID_REFERENCE), $container->getDefinition('mailer.mailer')->getArgument(1));
1371+
}
1372+
1373+
public function testMailerWithDisabledMessageBus(): void
1374+
{
1375+
$container = $this->createContainerFromFile('mailer_with_disabled_message_bus');
1376+
1377+
$this->assertNull($container->getDefinition('mailer.mailer')->getArgument(1));
1378+
}
1379+
1380+
public function testMailerWithSpecificMessageBus(): void
1381+
{
1382+
$container = $this->createContainerFromFile('mailer_with_specific_message_bus');
1383+
1384+
$this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('mailer.mailer')->getArgument(1));
13701385
}
13711386

13721387
protected function createContainer(array $data = [])

0 commit comments

Comments
 (0)