8000 [Messenger] allow specifying the message topic when calling dispatch() · symfony/symfony@2bb18a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bb18a7

Browse files
[Messenger] allow specifying the message topic when calling dispatch()
1 parent d5771cc commit 2bb18a7

30 files changed

+238
-218
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,19 @@ protected function isCsrfTokenValid(string $id, ?string $token): bool
391391
/**
392392
* Dispatches a message to the bus.
393393
*
394-
* @param object $message The message to dispatch
394+
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
395+
* @param string|null $topic The topic that senders and/or handlers can subscribe to to get the message;
396+
* if not provided, the topic is the class of the message
395397
*
396398
* @final
397399
*/
398-
protected function dispatchMessage($message): Envelope
400+
protected function dispatchMessage($message, string $topic = null): Envelope
399401
{
400402
if (!$this->container->has('message_bus')) {
401403
throw new \LogicException('The message bus is not enabled in your application. Try running "composer require symfony/messenger".');
402404
}
403405

404-
return $this->container->get('message_bus')->dispatch($message);
406+
return $this->container->get('message_bus')->dispatch($message, $topic);
405407
}
406408

407409
/**

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
15211521

15221522
$defaultMiddleware = array(
15231523
'before' => array(array('id' => 'logging')),
1524-
'after' => array(array('id' => 'route_messages'), array('id' => 'call_message_handler')),
1524+
'after' => array(array('id' => 'send_message'), array('id' => 'handle_message')),
15251525
);
15261526
foreach ($config['buses'] as $busId => $bus) {
15271527
$middleware = $bus['middleware'];
@@ -1572,31 +1572,27 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
15721572
$senderAliases[$name] = $transportId;
15731573
}
15741574

1575-
$messageToSenderIdMapping = array();
1576-
$messageToSendAndHandleMapping = array();
1577-
foreach ($config['routing'] as $message => $messageConfiguration) {
1578-
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false)) {
1579-
throw new LogicException(sprintf('Messenger routing configuration contains a mistake: message "%s" does not exist. It needs to match an existing class or interface.', $message));
1580-
}
1581-
1582-
if (1 < \count($messageConfiguration['senders'])) {
1575+
$topicToSenderIdMapping = array();
1576+
$topicToSendAndHandleMapping = array();
1577+
foreach ($config['routing'] as $topic => $topicConfiguration) {
1578+
if (1 < \count($topicConfiguration['senders'])) {
15831579
$senders = array_map(function ($sender) use ($senderAliases) {
15841580
return new Reference($senderAliases[$sender] ?? $sender);
1585-
}, $messageConfiguration['senders']);
1581+
}, $topicConfiguration['senders']);
15861582
10000 $chainSenderDefinition = new Definition(ChainSender::class, array($senders));
15871583
$chainSenderDefinition->addTag('messenger.sender');
1588-
$chainSenderId = '.messenger.chain_sender.'.$message;
1584+
$chainSenderId = '.messenger.chain_sender.'.$topic;
15891585
$container->setDefinition($chainSenderId, $chainSenderDefinition);
1590-
$messageToSenderIdMapping[$message] = $chainSenderId;
1586+
$topicToSenderIdMapping[$topic] = $chainSenderId;
15911587
} else {
1592-
$messageToSenderIdMapping[$message] = $messageConfiguration['senders'][0];
1588+
$topicToSenderIdMapping[$topic] = $topicConfiguration['senders'][0];
15931589
}
15941590

1595-
$messageToSendAndHandleMapping[$message] = $messageConfiguration['send_and_handle'];
1591+
$topicToSendAndHandleMapping[$topic] = $topicConfiguration['send_and_handle'];
15961592
}
15971593

1598-
$container->getDefinition('messenger.asynchronous.routing.sender_locator')->replaceArgument(1, $messageToSenderIdMapping);
1599-
$container->getDefinition('messenger.middleware.route_messages')->replaceArgument(1, $messageToSendAndHandleMapping);
1594+
$container->getDefinition('messenger.asynchronous.routing.sender_locator')->replaceArgument(1, $topicToSenderIdMapping);
1595+
$container->getDefinition('messenger.middleware.send_message')->replaceArgument(1, $topicToSendAndHandleMapping);
16001596
}
16011597

16021598
private function registerCacheConfiguration(array $config, ContainerBuilder $container)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<argument type="service" id="messenger.sender_locator" />
1313
<argument type="collection" /> <!-- Message to sender ID mapping -->
1414
</service>
15-
<service id="messenger.middleware.route_messages" class="Symfony\Component\Messenger\Middleware\SendMessageMiddleware">
15+
<service id="messenger.middleware.send_message" class="Symfony\Component\Messenger\Middleware\SendMessageMiddleware">
1616
<argument type="service" id="messenger.asynchronous.routing.sender_locator" />
1717
<argument type="collection" /> <!-- Message to send and handle mapping -->
1818
</service>
@@ -25,7 +25,7 @@
2525
</service>
2626

2727
<!-- Middleware -->
28-
<service id="messenger.middleware.call_message_handler" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
28+
<service id="messenger.middleware.handle_message" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
2929
<argument /> <!-- Bus handler resolver -->
3030
</service>
3131

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_multiple_buses.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
'messenger.bus.queries' => array(
1414
'default_middleware' => false,
1515
'middleware' => array(
16-
'route_messages',
17-
'call_message_handler',
16+
'send_message',
17+
'handle_message',
1818
),
1919
),
2020
),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_multiple_buses.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</framework:middleware>
1919
</framework:bus>
2020
<framework:bus name="messenger.bus.queries" default-middleware="false">
21-
<framework:middleware id="route_messages" />
22-
<framework:middleware id="call_message_handler" />
21+
<framework:middleware id="send_message" />
22+
<framework:middleware id="handle_message" />
2323
</framework:bus>
2424
</framework:messenger>
2525
</framework:config>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_multiple_buses.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ framework:
99
messenger.bus.queries:
1010
default_middleware: false
1111
middleware:
12-
- "route_messages"
13-
- "call_message_handler"
12+
- "send_message"
13+
- "handle_message"

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public function testMessengerRouting()
564564
{
565565
$container = $this->createContainerFromFile('messenger_routing');
566566
$senderLocatorDefinition = $container->getDefinition('messenger.asynchronous.routing.sender_locator');
567-
$sendMessageMiddlewareDefinition = $container->getDefinition('messenger.middleware.route_messages');
567+
$sendMessageMiddlewareDefinition = $container->getDefinition('messenger.middleware.send_message');
568568

569569
$messageToSenderIdsMapping = array(
570570
DummyMessage::class => '.messenger.chain_sender.'.DummyMessage::class,
@@ -619,22 +619,22 @@ public function testMessengerWithMultipleBuses()
619619
$this->assertSame(array(), $container->getDefinition('messenger.bus.commands')->getArgument(0));
620620
$this->assertEquals(array(
621621
array('id' => 'logging'),
622-
array('id' => 'route_messages'),
623-
array('id' => 'call_message_handler'),
622+
array('id' => 'send_message'),
623+
array('id' => 'handle_message'),
624624
), $container->getParameter('messenger.bus.commands.middleware'));
625625
$this->assertTrue($container->has('messenger.bus.events'));
626626
$this->assertSame(array(), $container->getDefinition('messenger.bus.events')->getArgument(0));
627627
$this->assertEquals(array(
628628
array('id' => 'logging'),
629629
array('id' => 'with_factory', 'arguments' => array('foo', true, array('bar' => 'baz'))),
630-
array('id' => 'route_messages'),
631-
array('id' => 'call_message_handler'),
630+
array('id' => 'send_message'),
631+
array('id' => 'handle_message'),
632632
), $container->getParameter('messenger.bus.events.middleware'));
633633
$this->assertTrue($container->has('messenger.bus.queries'));
634634
$this->assertSame(array(), $container->getDefinition('messenger.bus.queries')->getArgument(0));
635635
$this->assertEquals(array(
636-
array('id' => 'route_messages', 'arguments' => array()),
637-
array('id' => 'call_message_handler', 'arguments' => array()),
636+
array('id' => 'send_message', 'arguments' => array()),
637+
array('id' => 'handle_message', 'arguments' => array()),
638638
), $container->getParameter('messenger.bus.queries.middleware'));
639639

640640
$this->assertTrue($container->hasAlias('message_bus'));

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* The component is not experimental anymore
88
* All the changes below are BC BREAKS
99
* `MessageBusInterface::dispatch()`, `MiddlewareInterface::handle()` and `SenderInterface::send()` return `Envelope`
10+
* `MessageBusInterface::dispatch()` now takes a second `string $topic = null` argument
1011
* `MiddlewareInterface::handle()` now require an `Envelope` as first argument and a `StackInterface` as second
1112
* `EnvelopeAwareInterface` has been removed
1213
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
@@ -21,16 +22,14 @@ CHANGELOG
2122
as first constructor argument
2223
* The `EncoderInterface` and `DecoderInterface` have been replaced by a unified `Symfony\Component\Messenger\Transport\Serialization\SerializerInterface`.
2324
* The locator passed to `ContainerHandlerLocator` should not prefix its keys by "handler." anymore
24-
* The `AbstractHandlerLocator::getHandler()` method uses `?callable` as return type
2525
* Renamed `EnvelopeItemInterface` to `StampInterface`
2626
* `Envelope`'s constructor and `with()` method now accept `StampInterface` objects as variadic parameters
2727
* Renamed and moved `ReceivedMessage`, `ValidationConfiguration` and `SerializerConfiguration` in the `Stamp` namespace
2828
* Removed the `WrapIntoReceivedMessage`
29-
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
3029
* `MessengerDataCollector::getMessages()` returns an iterable, not just an array anymore
3130
* `AbstractHandlerLocator` is now internal
32-
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(Envelope $envelope): ?callable` and shouldn't throw when no handlers are found
33-
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
31+
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(string $topic): ?callable` and shouldn't throw when no handlers are found
32+
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(string $topic): ?SenderInterface`
3433
* Classes in the `Middleware\Enhancers` sub-namespace have been moved to the `Middleware` one
3534
* Classes in the `Asynchronous\Routing` sub-namespace have been moved to the `Transport\Sender\Locator` sub-namespace
3635
* The `Asynchronous/Middleware/SendMessageMiddleware` class has been moved to the `Middleware` namespace

0 commit comments

Comments
 (0)
0