8000 feature #48121 [Messenger] Do not return fallback senders when other … · symfony/symfony@fcfca3e · GitHub
[go: up one dir, main page]

Skip to content

Commit fcfca3e

Browse files
feature #48121 [Messenger] Do not return fallback senders when other senders were already found (wouterj)
This PR was merged into the 6.3 branch. Discussion ---------- [Messenger] Do not return fallback senders when other senders were already found | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | n/a See https://github.com/wouterj/sf-reproducer/tree/messenger-star-routing for a reproducer of the current situation. Currently, if you use the special `*` in the Messenger's routing configuration, this sender is always returned for all messages (also for messages explicitly configured to use another sender). According to the [documentation](https://symfony.com/doc/current/messenger.html#routing-messages-to-a-transport), the star should act as a fallback instead: > You may use `'*'` as the message class. This will act as a default routing rule for any message not matched under `routing`. This is useful to ensure no message is handled synchronously by default. Commits ------- acae5ca [Messenger] Do not return fallback senders when other senders were already found
2 parents ecf5f63 + acae5ca commit fcfca3e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ public function testItReturnsTheSenderBasedOnTransportNamesStamp()
5252
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
5353
}
5454

55+
public function testSendersMapWithFallback()
56+
{
57+
$firstSender = $this->createMock(SenderInterface::class);
58+
$secondSender = $this->createMock(SenderInterface::class);
59+
$sendersLocator = $this->createContainer([
60+
'first' => $firstSender,
61+
'second' => $secondSender,
62+
]);
63+
$locator = new SendersLocator([
64+
DummyMessage::class => ['first'],
65+
'*' => ['second'],
66+
], $sendersLocator);
67+
68+
$this->assertSame(['first' => $firstSender], iterator_to_array($locator->getSenders(new Envelope(new DummyMessage('a')))), 'Unexpected senders for configured message');
69+
$this->assertSame(['second' => $secondSender], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))), 'Unexpected senders for unconfigured message');
70+
}
71+
5572
private function createContainer(array $senders): ContainerInterface
5673
{
5774
$container = $this->createMock(ContainerInterface::class);

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

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function getSenders(Envelope $envelope): iterable
5151

5252
foreach (HandlersLocator::listTypes($envelope) as $type) {
5353
foreach ($this->sendersMap[$type] ?? [] as $senderAlias) {
54+
if (str_ends_with($type, '*') && $seen) {
55+
// the '*' acts as a fallback, if other senders already matched
56+
// with previous types, skip the senders bound to the fallback
57+
continue;
58+
}
59+
5460
if (!\in_array($senderAlias, $seen, true)) {
5561
$seen[] = $senderAlias;
5662

0 commit comments

Comments
 (0)
0