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

Skip to content

[Messenger] Add TransportNamesStamp to change the transport while dispatching a message #39306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

6.1
---
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/TransportNamesStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Stamp;

/**
* Stamp used to override the transport names specified in the Messenger routing configuration file.
*/
final class TransportNamesStamp implements StampInterface
{
/**
* @param string[] $transports Transport names to be used for the message
*/
public function __construct(private array $transports)
{
}

public function getTransportNames(): array
{
return $this->transports;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Stamp;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;

class TransportNamesStampTest extends TestCase
{
public function testGetSenders()
{
$configuredSenders = ['first_transport', 'second_transport', 'other_transport'];
$stamp = new TransportNamesStamp($configuredSenders);
$stampSenders = $stamp->getTransportNames();
$this->assertEquals(\count($configuredSenders), \count($stampSenders));

foreach ($configuredSenders as $key => $sender) {
$this->assertSame($sender, $stampSenders[$key]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
Expand All @@ -35,6 +36,22 @@ public function testItReturnsTheSenderBasedOnTheMessageClass()
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
}

public function testItReturnsTheSenderBasedOnTransportNamesStamp()
{
$mySender = $this->createMock(SenderInterface::class);
$otherSender = $this->createMock(SenderInterface::class);
$sendersLocator = $this->createContainer([
'my_sender' => $mySender,
'other_sender' => $otherSender,
]);
$locator = new SendersLocator([
DummyMessage::class => ['my_sender'],
], $sendersLocator);

$this->assertSame(['other_sender' => $otherSender], iterator_to_array($locator->getSenders(new Envelope(new DummyMessage('a'), [new TransportNamesStamp(['other_sender'])]))));
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
}

private function createContainer(array $senders): ContainerInterface
{
$container = $this->createMock(ContainerInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;

/**
* Maps a message to a list of senders.
Expand All @@ -41,20 +42,33 @@ public function __construct(array $sendersMap, ContainerInterface $sendersLocato
*/
public function getSenders(Envelope $envelope): iterable
{
if ($envelope->all(TransportNamesStamp::class)) {
foreach ($envelope->last(TransportNamesStamp::class)->getTransportNames() as $senderAlias) {
yield from $this->getSenderFromAlias($senderAlias);
}

return;
}

$seen = [];

foreach (HandlersLocator::listTypes($envelope) as $type) {
foreach ($this->sendersMap[$type] ?? [] as $senderAlias) {
if (!\in_array($senderAlias, $seen, true)) {
if (!$this->sendersLocator->has($senderAlias)) {
throw new RuntimeException(sprintf('Invalid senders configuration: sender "%s" is not in the senders locator.', $senderAlias));
}

$seen[] = $senderAlias;
$sender = $this->sendersLocator->get($senderAlias);
yield $senderAlias => $sender;

yield from $this->getSenderFromAlias($senderAlias);
}
}
}
}

private function getSenderFromAlias(string $senderAlias): iterable
{
if (!$this->sendersLocator->has($senderAlias)) {
throw new RuntimeException(sprintf('Invalid senders configuration: sender "%s" is not in the senders locator.', $senderAlias));
}

yield $senderAlias => $this->sendersLocator->get($senderAlias);
}
}