Skip to content

Commit 7a4abf1

Browse files
committed
[Messenger] support iterable in sender locator
1 parent 0998710 commit 7a4abf1

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ public function handle($message, callable $next)
3636
return $next($message->getMessage());
3737
}
3838

39-
if (!empty($senders = $this->senderLocator->getSendersForMessage($message))) {
40-
foreach ($senders as $sender) {
41-
if (null === $sender) {
42-
continue;
43-
}
44-
45-
$sender->send($message);
39+
$hasSender = false;
40+
$forwardToSenderAndHandler = false;
41+
foreach ($this->senderLocator->getSendersForMessage($message) as $sender) {
42+
$hasSender = true;
43+
44+
if (null === $sender) {
45+
$forwardToSenderAndHandler = true;
46+
continue;
4647
}
4748

48-
if (!\in_array(null, $senders, true)) {
49-
return;
50-
}
49+
$sender->send($message);
5150
}
5251

53-
return $next($message);
52+
if (!$hasSender || $forwardToSenderAndHandler) {
53+
return $next($message);
54+
}
5455
}
5556
}

src/Symfony/Component/Messenger/Asynchronous/Routing/SenderLocator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ public function __construct(ContainerInterface $senderServiceLocator, array $mes
3030
/**
3131
* {@inheritdoc}
3232
*/
33-
public function getSendersForMessage($message): array
33+
public function getSendersForMessage($message): iterable
3434
{
3535
$senderIds = $this->messageToSenderIdsMapping[\get_class($message)] ?? $this->messageToSenderIdsMapping['*'] ?? array();
3636

37-
$senders = array();
3837
foreach ($senderIds as $senderId) {
39-
$senders[] = $this->senderServiceLocator->get($senderId);
38+
yield $this->senderServiceLocator->get($senderId);
4039
}
41-
42-
return $senders;
4340
}
4441
}

src/Symfony/Component/Messenger/Asynchronous/Routing/SenderLocatorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
interface SenderLocatorInterface
2222
{
2323
/**
24-
* Gets the sender (if applicable) for the given message object.
24+
* Gets the senders (if applicable) for the given message object.
2525
*
2626
* @param object $message
2727
*
2828
* @return SenderInterface[]
2929
*/
30-
public function getSendersForMessage($message): array;
30+
public function getSendersForMessage($message): iterable;
3131
}

src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ class InMemorySenderLocator implements SenderLocatorInterface
8888
{
8989
private $senders;
9090

91-
public function __construct(array $senders)
91+
public function __construct(iterable $senders)
9292
{
9393
$this->senders = $senders;
9494
}
9595

96-
public function getSendersForMessage($message): array
96+
public function getSendersForMessage($message): iterable
9797
{
9898
return $this->senders;
9999
}

src/Symfony/Component/Messenger/Tests/Asynchronous/Routing/SenderLocatorTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function testItReturnsTheSenderBasedOnTheMessageClass()
3232
),
3333
));
3434

35-
$this->assertEquals(array($sender), $locator->getSendersForMessage(new DummyMessage('Hello')));
36-
$this->assertEquals(array(), $locator->getSendersForMessage(new SecondMessage()));
35+
$this->assertIterableEquals(array($sender), $locator->getSendersForMessage(new DummyMessage('Hello')));
36+
$this->assertIterableEquals(array(), $locator->getSendersForMessage(new SecondMessage()));
3737
}
3838

3939
public function testItSupportsAWildcardInsteadOfTheMessageClass()
@@ -55,7 +55,19 @@ public function testItSupportsAWildcardInsteadOfTheMessageClass()
5555
),
5656
));
5757

58-
$this->assertEquals(array($sender), $locator->getSendersForMessage(new DummyMessage('Hello')));
59-
$this->assertEquals(array($apiSender), $locator->getSendersForMessage(new SecondMessage()));
58+
$this->assertIterableEquals(array($sender), $locator->getSendersForMessage(new DummyMessage('Hello')));
59+
$this->assertIterableEquals(array($apiSender), $locator->getSendersForMessage(new SecondMessage()));
60+
}
61+
62+
private function assertIterableEquals(iterable $expected, iterable $actual)
63+
{
64+
if ($actual instanceof \Traversable) {
65+
$actual = iterator_to_array($actual, false);
66+
}
67+
if ($expected instanceof \Traversable) {
68+
$expected = iterator_to_array($expected, false);
69+
}
70+
71+
$this->assertEquals($expected, $actual);
6072
}
6173
}

0 commit comments

Comments
 (0)