Changing how RoutableMessageBus fallback bus works
This commit is contained in:
parent
ca6266d902
commit
42e0536d8f
@ -106,6 +106,7 @@
|
|||||||
<!-- routable message bus -->
|
<!-- routable message bus -->
|
||||||
<service id="messenger.routable_message_bus" class="Symfony\Component\Messenger\RoutableMessageBus">
|
<service id="messenger.routable_message_bus" class="Symfony\Component\Messenger\RoutableMessageBus">
|
||||||
<argument /> <!-- Message bus locator -->
|
<argument /> <!-- Message bus locator -->
|
||||||
|
<argument type="service" id="messenger.default_bus" />
|
||||||
</service>
|
</service>
|
||||||
</services>
|
</services>
|
||||||
</container>
|
</container>
|
||||||
|
@ -28,13 +28,15 @@ use Symfony\Component\Messenger\Stamp\BusNameStamp;
|
|||||||
class RoutableMessageBus implements MessageBusInterface
|
class RoutableMessageBus implements MessageBusInterface
|
||||||
{
|
{
|
||||||
private $busLocator;
|
private $busLocator;
|
||||||
|
private $fallbackBus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ContainerInterface $busLocator A locator full of MessageBusInterface objects
|
* @param ContainerInterface $busLocator A locator full of MessageBusInterface objects
|
||||||
*/
|
*/
|
||||||
public function __construct(ContainerInterface $busLocator)
|
public function __construct(ContainerInterface $busLocator, MessageBusInterface $fallbackBus = null)
|
||||||
{
|
{
|
||||||
$this->busLocator = $busLocator;
|
$this->busLocator = $busLocator;
|
||||||
|
$this->fallbackBus = $fallbackBus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dispatch($envelope, array $stamps = []): Envelope
|
public function dispatch($envelope, array $stamps = []): Envelope
|
||||||
@ -43,14 +45,28 @@ class RoutableMessageBus implements MessageBusInterface
|
|||||||
throw new InvalidArgumentException('Messages passed to RoutableMessageBus::dispatch() must be inside an Envelope');
|
throw new InvalidArgumentException('Messages passed to RoutableMessageBus::dispatch() must be inside an Envelope');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var BusNameStamp $busNameStamp */
|
return $this->getMessageBus($envelope)->dispatch($envelope, $stamps);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getMessageBus(Envelope $envelope): MessageBusInterface
|
||||||
|
{
|
||||||
|
/** @var BusNameStamp|null $busNameStamp */
|
||||||
$busNameStamp = $envelope->last(BusNameStamp::class);
|
$busNameStamp = $envelope->last(BusNameStamp::class);
|
||||||
$busName = null !== $busNameStamp ? $busNameStamp->getBusName() : MessageBusInterface::class;
|
|
||||||
|
if (null === $busNameStamp) {
|
||||||
|
if (null === $this->fallbackBus) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->fallbackBus;
|
||||||
|
}
|
||||||
|
|
||||||
|
$busName = $busNameStamp->getBusName();
|
||||||
|
|
||||||
if (!$this->busLocator->has($busName)) {
|
if (!$this->busLocator->has($busName)) {
|
||||||
throw new InvalidArgumentException(sprintf('Bus named "%s" does not exist.', $busName));
|
throw new InvalidArgumentException(sprintf('Bus named "%s" does not exist.', $busName));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
|
return $this->busLocator->get($busName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,41 +50,34 @@ class RoutableMessageBusTest extends TestCase
|
|||||||
->willReturn($envelope);
|
->willReturn($envelope);
|
||||||
|
|
||||||
$container = $this->createMock(ContainerInterface::class);
|
$container = $this->createMock(ContainerInterface::class);
|
||||||
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
|
|
||||||
->willReturn(true);
|
|
||||||
$container->expects($this->once())->method('get')->with(MessageBusInterface::class)
|
|
||||||
->willReturn($defaultBus);
|
|
||||||
|
|
||||||
$routableBus = new RoutableMessageBus($container);
|
$routableBus = new RoutableMessageBus($container, $defaultBus);
|
||||||
|
|
||||||
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
|
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItExceptionOnDefaultBusNotFound()
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', MessageBusInterface::class));
|
|
||||||
|
|
||||||
$envelope = new Envelope(new \stdClass());
|
|
||||||
|
|
||||||
$container = $this->createMock(ContainerInterface::class);
|
|
||||||
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
|
|
||||||
->willReturn(false);
|
|
||||||
|
|
||||||
$routableBus = new RoutableMessageBus($container);
|
|
||||||
$routableBus->dispatch($envelope);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItExceptionOnBusNotFound()
|
public function testItExceptionOnBusNotFound()
|
||||||
{
|
{
|
||||||
$this->expectException(InvalidArgumentException::class);
|
$this->expectException(InvalidArgumentException::class);
|
||||||
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', 'foo_bus'));
|
$this->expectExceptionMessage('Bus named "my_cool_bus" does not exist.');
|
||||||
|
|
||||||
$envelope = new Envelope(new \stdClass(), [new BusNameStamp('foo_bus')]);
|
$envelope = new Envelope(new \stdClass(), [
|
||||||
|
new BusNameStamp('my_cool_bus'),
|
||||||
|
]);
|
||||||
|
|
||||||
$container = $this->createMock(ContainerInterface::class);
|
$container = $this->createMock(ContainerInterface::class);
|
||||||
$container->expects($this->once())->method('has')->willReturn(false);
|
$routableBus = new RoutableMessageBus($container);
|
||||||
|
$routableBus->dispatch($envelope);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItExceptionOnDefaultBusNotFound()
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.');
|
||||||
|
|
||||||
|
$envelope = new Envelope(new \stdClass());
|
||||||
|
|
||||||
|
$container = $this->createMock(ContainerInterface::class);
|
||||||
$routableBus = new RoutableMessageBus($container);
|
$routableBus = new RoutableMessageBus($container);
|
||||||
$routableBus->dispatch($envelope);
|
$routableBus->dispatch($envelope);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user