Skip to content

[Messenger] Introduce #[AsMessage] attribute for message routing #57507

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 1 commit into from
Jun 28, 2024
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
29 changes: 29 additions & 0 deletions src/Symfony/Component/Messenger/Attribute/AsMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

namespace Symfony\Component\Messenger\Attribute;

/**
* Attribute for configuring message routing.
*
* @author Pierre Rineau pierre.rineau@processus.org>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsMessage
{
public function __construct(
/**
* Name of the transports to which the message should be routed.
*/
public null|string|array $transport = null,
) {
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.2
---

* Add `#[AsMessage]` attribute with `$transport` parameter for message routing
* Add `--format` option to the `messenger:stats` command

7.1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Component\Messenger\Tests\Fixtures;

use Symfony\Component\Messenger\Attribute\AsMessage;

#[AsMessage(transport: ['first_sender', 'second_sender'])]
class DummyMessageWithAttribute implements DummyMessageInterface
{
public function __construct(
private string $message,
) {
}

public function getMessage(): string
{
return $this->message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageWithAttribute;
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
Expand Down Expand Up @@ -53,6 +55,56 @@ public function testItReturnsTheSenderBasedOnTransportNamesStamp()
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
}

public function testItReturnsTheSenderBasedOnAsMessageAttribute()
{
$firstSender = $this->createMock(SenderInterface::class);
$secondSender = $this->createMock(SenderInterface::class);
$otherSender = $this->createMock(SenderInterface::class);
$sendersLocator = $this->createContainer([
'first_sender' => $firstSender,
'second_sender' => $secondSender,
'other_sender' => $otherSender,
]);
$locator = new SendersLocator([], $sendersLocator);

$this->assertSame(['first_sender' => $firstSender, 'second_sender' => $secondSender], iterator_to_array($locator->getSenders(new Envelope(new DummyMessageWithAttribute('a')))));
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new SecondMessage()))));
}

public function testAsMessageAttributeIsOverridenByTransportNamesStamp()
{
$firstSender = $this->createMock(SenderInterface::class);
$secondSender = $this->createMock(SenderInterface::class);
$otherSender = $this->createMock(SenderInterface::class);
$sendersLocator = $this->createContainer([
'first_sender' => $firstSender,
'second_sender' => $secondSender,
'other_sender' => $otherSender,
]);
$locator = new SendersLocator([], $sendersLocator);

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

public function testAsMessageAttributeIsOverridenByUserConfiguration()
{
$firstSender = $this->createMock(SenderInterface::class);
$secondSender = $this->createMock(SenderInterface::class);
$otherSender = $this->createMock(SenderInterface::class);
$sendersLocator = $this->createContainer([
'first_sender' => $firstSender,
'second_sender' => $secondSender,
'other_sender' => $otherSender,
]);
$locator = new SendersLocator([
DummyMessageInterface::class => ['other_sender'],
], $sendersLocator);

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

public function testSendersMapWithFallback()
{
$firstSender = $this->createMock(SenderInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Transport\Sender;

use Psr\Container\ContainerInterface;
use Symfony\Component\Messenger\Attribute\AsMessage;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\Handler\HandlersLocator;
Expand Down Expand Up @@ -45,6 +46,7 @@ public function getSenders(Envelope $envelope): iterable
}

$seen = [];
$found = false;

foreach (HandlersLocator::listTypes($envelope) as $type) {
if (str_ends_with($type, '*') && $seen) {
Expand All @@ -58,9 +60,37 @@ public function getSenders(Envelope $envelope): iterable
$seen[] = $senderAlias;

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

// Let the configuration-driven map upper override message attributes,
// this allows environment-specific configuration overriding hardcoded
// transport name.
if ($found) {
return;
}

foreach ($this->getTransportNamesFromAttribute($envelope) as $senderAlias) {
yield from $this->getSenderFromAlias($senderAlias);
}
}

private function getTransportNamesFromAttribute(Envelope $envelope): array
{
$transports = [];
$message = $envelope->getMessage();

foreach ((new \ReflectionClass($message))->getAttributes(AsMessage::class, \ReflectionAttribute::IS_INSTANCEOF) as $refAttr) {
$asMessage = $refAttr->newInstance();

if ($asMessage->transport) {
$transports = \array_merge($transports, (array) $asMessage->transport);
}
}

return $transports;
}

private function getSenderFromAlias(string $senderAlias): iterable
Expand Down
Loading