Skip to content

Commit 91b3e8f

Browse files
[Messenger] Fix code review comments
Overwrite config routing Read from parent interfaces and classes Add tests
1 parent 7651b34 commit 91b3e8f

File tree

6 files changed

+83
-30
lines changed

6 files changed

+83
-30
lines changed

src/Symfony/Component/Messenger/Attribute/Senders.php renamed to src/Symfony/Component/Messenger/Attribute/Transport.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
/**
1515
* @author Maxim Dovydenok <dovydenok.maxim@gmail.com>
1616
*/
17-
#[\Attribute(\Attribute::TARGET_CLASS)]
18-
class Senders
17+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
18+
class Transport
1919
{
20-
public array $senders;
20+
public string $name;
2121

22-
/**
23-
* @param string[] $senders
24-
*/
25-
public function __construct(string ...$senders)
22+
public function __construct(string $name)
2623
{
27-
$this->senders = $senders;
24+
$this->name = $name;
2825
}
2926
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Tests\Fixtures;
4+
5+
use Symfony\Component\Messenger\Attribute\Transport;
6+
7+
#[Transport('my_common_sender')]
8+
interface DummyMessageInterfaceWithAttribute
9+
{
10+
}

src/Symfony/Component/Messenger/Tests/Fixtures/DummyMessageWithAttribute.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Symfony\Component\Messenger\Tests\Fixtures;
44

5-
use Symfony\Component\Messenger\Attribute\Senders;
5+
use Symfony\Component\Messenger\Attribute\Transport;
66

7-
#[Senders('my_sender')]
7+
#[Transport('my_sender')]
88
class DummyMessageWithAttribute implements DummyMessageInterface
99
{
1010
private $message;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Tests\Fixtures;
4+
5+
use Symfony\Component\Messenger\Attribute\Transport;
6+
7+
#[Transport('my_sender')]
8+
class DummyMessageWithAttributeAndInterface implements DummyMessageInterfaceWithAttribute
9+
{
10+
private $message;
11+
12+
public function __construct(string $message)
13+
{
14+
$this->message = $message;
15+
}
16+
17+
public function getMessage(): string
18+
{
19+
return $this->message;
20+
}
21+
}

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Messenger\Envelope;
1717
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1818
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageWithAttribute;
19+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageWithAttributeAndInterface;
1920
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
2021
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
2122
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
@@ -30,11 +31,28 @@ public function testAttributeMapping()
3031
$sender = $this->createMock(SenderInterface::class);
3132
$sendersLocator = $this->createContainer([
3233
'my_sender' => $sender,
34+
'my_common_sender' => $sender,
35+
'my_merged_sender' => $sender,
3336
]);
3437
$locator = new SendersLocator([], $sendersLocator);
38+
$locatorWithRouting = new SendersLocator([
39+
DummyMessageWithAttribute::class => ['my_merged_sender'],
40+
], $sendersLocator);
3541

36-
$this->assertSame(['my_sender' => $sender], iterator_to_array($locator->getSenders(new Envelope(new DummyMessageWithAttribute('a')))));
3742
$this->assertSame([], iterator_to_array($locator->getSenders(new Envelope(new DummyMessage('a')))));
43+
$this->assertSame(
44+
['my_sender' => $sender],
45+
iterator_to_array($locator->getSenders(new Envelope(new DummyMessageWithAttribute('a'))))
46+
);
47+
$this->assertSame(
48+
['my_sender' => $sender, 'my_common_sender' => $sender],
49+
iterator_to_array($locator->getSenders(new Envelope(new DummyMessageWithAttributeAndInterface('a'))))
50+
);
51+
52+
$this->assertSame(
53+
['my_sender' => $sender],
54+
iterator_to_array($locatorWithRouting->getSenders(new Envelope(new DummyMessageWithAttribute('a'))))
55+
);
3856
}
3957

4058
public function testItReturnsTheSenderBasedOnTheMessageClass()

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

+26-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Messenger\Transport\Sender;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\Messenger\Attribute\Senders;
15+
use Symfony\Component\Messenger\Attribute\Transport;
1616
use Symfony\Component\Messenger\Envelope;
1717
use Symfony\Component\Messenger\Exception\RuntimeException;
1818
use Symfony\Component\Messenger\Handler\HandlersLocator;
@@ -44,16 +44,20 @@ public function getSenders(Envelope $envelope): iterable
4444
{
4545
$senderAliases = [];
4646

47-
if (\PHP_VERSION_ID >= 80000) {
48-
$senderAliases = $this->getSendersFromAttributes($envelope);
49-
}
50-
5147
foreach (HandlersLocator::listTypes($envelope) as $type) {
5248
foreach ($this->sendersMap[$type] ?? [] as $senderAlias) {
5349
$senderAliases[] = $senderAlias;
5450
}
5551
}
5652

53+
if (\PHP_VERSION_ID >= 80000) {
54+
$senderAliasesFromAttributes = $this->getSendersFromAttributes($envelope);
55+
56+
if (!empty($senderAliasesFromAttributes)) {
57+
$senderAliases = $senderAliasesFromAttributes;
58+
}
59+
}
60+
5761
$senderAliases = array_unique($senderAliases);
5862

5963
foreach ($senderAliases as $senderAlias) {
@@ -71,21 +75,24 @@ public function getSenders(Envelope $envelope): iterable
7175
*/
7276
private function getSendersFromAttributes(Envelope $envelope): array
7377
{
74-
$messageClass = \get_class($envelope->getMessage());
75-
76-
try {
77-
$reflectionClass = new \ReflectionClass($messageClass);
78-
} catch (\ReflectionException $e) {
79-
return [];
80-
}
81-
82-
$attributes = $reflectionClass->getAttributes(Senders::class);
83-
8478
$senders = [];
85-
foreach ($attributes as $attribute) {
86-
/** @var Senders $attributeInstance */
87-
$attributeInstance = $attribute->newInstance();
88-
$senders = array_merge($senders, $attributeInstance->senders);
79+
80+
foreach (HandlersLocator::listTypes($envelope) as $type) {
81+
if (class_exists($type) || interface_exists($type)) {
82+
try {
83+
$reflectionClass = new \ReflectionClass($type);
84+
} catch (\ReflectionException $e) {
85+
continue;
86+
}
87+
88+
$attributes = $reflectionClass->getAttributes(Transport::class);
89+
90+
foreach ($attributes as $attribute) {
91+
/** @var Transport $attributeInstance */
92+
$attributeInstance = $attribute->newInstance();
93+
$senders[] = $attributeInstance->name;
94+
}
95+
}
8996
}
9097

9198
return $senders;

0 commit comments

Comments
 (0)