Skip to content

Commit 917d72c

Browse files
committed
Changing how RoutableMessageBus fallback bus works
1 parent ca6266d commit 917d72c

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<!-- routable message bus -->
107107
<service id="messenger.routable_message_bus" class="Symfony\Component\Messenger\RoutableMessageBus">
108108
<argument /> <!-- Message bus locator -->
109+
<argument type="service" id="messenger.default_bus" />
109110
</service>
110111
</services>
111112
</container>

src/Symfony/Component/Messenger/RoutableMessageBus.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
class RoutableMessageBus implements MessageBusInterface
2929
{
3030
private $busLocator;
31+
private $fallbackBus;
3132

3233
/**
3334
* @param ContainerInterface $busLocator A locator full of MessageBusInterface objects
3435
*/
35-
public function __construct(ContainerInterface $busLocator)
36+
public function __construct(ContainerInterface $busLocator, MessageBusInterface $fallbackBus = null)
3637
{
3738
$this->busLocator = $busLocator;
39+
$this->fallbackBus = $fallbackBus;
3840
}
3941

4042
public function dispatch($envelope, array $stamps = []): Envelope
@@ -43,14 +45,28 @@ public function dispatch($envelope, array $stamps = []): Envelope
4345
throw new InvalidArgumentException('Messages passed to RoutableMessageBus::dispatch() must be inside an Envelope');
4446
}
4547

46-
/** @var BusNameStamp $busNameStamp */
48+
return $this->getMessageBus($envelope)->dispatch($envelope, $stamps);
49+
}
50+
51+
private function getMessageBus(Envelope $envelope): MessageBusInterface
52+
{
53+
/** @var BusNameStamp|null $busNameStamp */
4754
$busNameStamp = $envelope->last(BusNameStamp::class);
48-
$busName = null !== $busNameStamp ? $busNameStamp->getBusName() : MessageBusInterface::class;
55+
56+
if (null === $busNameStamp) {
57+
if (null === $this->fallbackBus) {
58+
throw new InvalidArgumentException(sprintf('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.'));
59+
}
60+
61+
return $this->fallbackBus;
62+
}
63+
64+
$busName = $busNameStamp->getBusName();
4965

5066
if (!$this->busLocator->has($busName)) {
5167
throw new InvalidArgumentException(sprintf('Bus named "%s" does not exist.', $busName));
5268
}
5369

54-
return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
70+
return $this->busLocator->get($busName);
5571
}
5672
}

src/Symfony/Component/Messenger/Tests/RoutableMessageBusTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,34 @@ public function testItRoutesToDefaultBus()
5050
->willReturn($envelope);
5151

5252
$container = $this->createMock(ContainerInterface::class);
53-
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
54-
->willReturn(true);
55-
$container->expects($this->once())->method('get')->with(MessageBusInterface::class)
56-
->willReturn($defaultBus);
5753

58-
$routableBus = new RoutableMessageBus($container);
54+
$routableBus = new RoutableMessageBus($container, $defaultBus);
5955

6056
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
6157
}
6258

63-
public function testItExceptionOnDefaultBusNotFound()
59+
public function testItExceptionOnBusNotFound()
6460
{
6561
$this->expectException(InvalidArgumentException::class);
66-
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', MessageBusInterface::class));
62+
$this->expectExceptionMessage('Bus named "my_cool_bus" does not exist.');
6763

68-
$envelope = new Envelope(new \stdClass());
64+
$envelope = new Envelope(new \stdClass(), [
65+
new BusNameStamp('my_cool_bus')
66+
]);
6967

7068
$container = $this->createMock(ContainerInterface::class);
71-
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
72-
->willReturn(false);
73-
7469
$routableBus = new RoutableMessageBus($container);
7570
$routableBus->dispatch($envelope);
7671
}
7772

78-
public function testItExceptionOnBusNotFound()
73+
public function testItExceptionOnDefaultBusNotFound()
7974
{
8075
$this->expectException(InvalidArgumentException::class);
81-
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', 'foo_bus'));
76+
$this->expectExceptionMessage('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.');
8277

83-
$envelope = new Envelope(new \stdClass(), [new BusNameStamp('foo_bus')]);
78+
$envelope = new Envelope(new \stdClass());
8479

8580
$container = $this->createMock(ContainerInterface::class);
86-
$container->expects($this->once())->method('has')->willReturn(false);
87-
8881
$routableBus = new RoutableMessageBus($container);
8982
$routableBus->dispatch($envelope);
9083
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy