Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1a4a5d5

Browse files
committed
feature#39306 [Messenger] AddTransportNamesStamp to change the transport while dispatching a message (asilelik, fabpot)
This PR was merged into the 6.2 branch.Discussion----------[Messenger] Add `TransportNamesStamp` to change the transport while dispatching a message| Q | A| ------------- | ---| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets |Fix#38200,Fix#39306| License | MIT| Doc PR | symfony/symfony-docs#... (Will be added after PR discussion about implementation)This feature will allow developers to choose transport layer while dispatching a message. Normally, we define transport layer in configuration. As a result, developer doesn't have any power to modify it after the deployment. This PR aims to allow overriding already defined transport layer for a message and rerouting it to different transport per dispatch request.If newly added stamp is not used, default configuration will be used for choosing transport.Since I've added a new stamp, there shouldn't be any BC change.Commits-------f526af0 [Messenger] Rename some classesb501e67 Add functionality for choosing transport while dispatching message
2 parentse1435f6 +f526af0 commit1a4a5d5

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

‎src/Symfony/Component/Messenger/CHANGELOG.md‎

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

77
* Add new`messenger:stats` command that return a list of transports with their "to be processed" message count
8+
* Add`TransportNamesStamp` to change the transport while dispatching a message
89

910
6.1
1011
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Stamp;
13+
14+
/**
15+
* Stamp used to override the transport names specified in the Messenger routing configuration file.
16+
*/
17+
finalclass TransportNamesStampimplements StampInterface
18+
{
19+
/**
20+
* @param string[] $transports Transport names to be used for the message
21+
*/
22+
publicfunction__construct(privatearray$transports)
23+
{
24+
}
25+
26+
publicfunctiongetTransportNames():array
27+
{
28+
return$this->transports;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Tests\Stamp;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Messenger\Stamp\TransportNamesStamp;
16+
17+
class TransportNamesStampTestextends TestCase
18+
{
19+
publicfunctiontestGetSenders()
20+
{
21+
$configuredSenders = ['first_transport','second_transport','other_transport'];
22+
$stamp =newTransportNamesStamp($configuredSenders);
23+
$stampSenders =$stamp->getTransportNames();
24+
$this->assertEquals(\count($configuredSenders),\count($stampSenders));
25+
26+
foreach ($configuredSendersas$key =>$sender) {
27+
$this->assertSame($sender,$stampSenders[$key]);
28+
}
29+
}
30+
}

‎src/Symfony/Component/Messenger/Tests/Transport/Sender/SendersLocatorTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
usePHPUnit\Framework\TestCase;
1515
usePsr\Container\ContainerInterface;
1616
useSymfony\Component\Messenger\Envelope;
17+
useSymfony\Component\Messenger\Stamp\TransportNamesStamp;
1718
useSymfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1819
useSymfony\Component\Messenger\Tests\Fixtures\SecondMessage;
1920
useSymfony\Component\Messenger\Transport\Sender\SenderInterface;
@@ -35,6 +36,22 @@ public function testItReturnsTheSenderBasedOnTheMessageClass()
3536
$this->assertSame([],iterator_to_array($locator->getSenders(newEnvelope(newSecondMessage()))));
3637
}
3738

39+
publicfunctiontestItReturnsTheSenderBasedOnTransportNamesStamp()
40+
{
41+
$mySender =$this->createMock(SenderInterface::class);
42+
$otherSender =$this->createMock(SenderInterface::class);
43+
$sendersLocator =$this->createContainer([
44+
'my_sender' =>$mySender,
45+
'other_sender' =>$otherSender,
46+
]);
47+
$locator =newSendersLocator([
48+
DummyMessage::class => ['my_sender'],
49+
],$sendersLocator);
50+
51+
$this->assertSame(['other_sender' =>$otherSender],iterator_to_array($locator->getSenders(newEnvelope(newDummyMessage('a'), [newTransportNamesStamp(['other_sender'])]))));
52+
$this->assertSame([],iterator_to_array($locator->getSenders(newEnvelope(newSecondMessage()))));
53+
}
54+
3855
privatefunctioncreateContainer(array$senders):ContainerInterface
3956
{
4057
$container =$this->createMock(ContainerInterface::class);

‎src/Symfony/Component/Messenger/Transport/Sender/SendersLocator.php‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Component\Messenger\Envelope;
1616
useSymfony\Component\Messenger\Exception\RuntimeException;
1717
useSymfony\Component\Messenger\Handler\HandlersLocator;
18+
useSymfony\Component\Messenger\Stamp\TransportNamesStamp;
1819

1920
/**
2021
* Maps a message to a list of senders.
@@ -41,20 +42,33 @@ public function __construct(array $sendersMap, ContainerInterface $sendersLocato
4142
*/
4243
publicfunctiongetSenders(Envelope$envelope):iterable
4344
{
45+
if ($envelope->all(TransportNamesStamp::class)) {
46+
foreach ($envelope->last(TransportNamesStamp::class)->getTransportNames()as$senderAlias) {
47+
yieldfrom$this->getSenderFromAlias($senderAlias);
48+
}
49+
50+
return;
51+
}
52+
4453
$seen = [];
4554

4655
foreach (HandlersLocator::listTypes($envelope)as$type) {
4756
foreach ($this->sendersMap[$type] ?? []as$senderAlias) {
4857
if (!\in_array($senderAlias,$seen,true)) {
49-
if (!$this->sendersLocator->has($senderAlias)) {
50-
thrownewRuntimeException(sprintf('Invalid senders configuration: sender "%s" is not in the senders locator.',$senderAlias));
51-
}
52-
5358
$seen[] =$senderAlias;
54-
$sender =$this->sendersLocator->get($senderAlias);
55-
yield$senderAlias =>$sender;
59+
60+
yieldfrom$this->getSenderFromAlias($senderAlias);
5661
}
5762
}
5863
}
5964
}
65+
66+
privatefunctiongetSenderFromAlias(string$senderAlias):iterable
67+
{
68+
if (!$this->sendersLocator->has($senderAlias)) {
69+
thrownewRuntimeException(sprintf('Invalid senders configuration: sender "%s" is not in the senders locator.',$senderAlias));
70+
}
71+
72+
yield$senderAlias =>$this->sendersLocator->get($senderAlias);
73+
}
6074
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp