8000 [Messenger] remove interfaces/parent classes-based message subcription in favor of topic-based subscription by nicolas-grekas · Pull Request #28993 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] remove interfaces/parent classes-based message subcription in favor of topic-based subscription #28993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,19 @@ protected function isCsrfTokenValid(string $id, ?string $token): bool
/**
* Dispatches a message to the bus.
*
* @param object $message The message to dispatch
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
* @param string|null $topic The topic that senders and/or handlers can subscribe to to get the message;
* if not provided, the topic is the class of the message
*
* @final
*/
protected function dispatchMessage($message): Envelope
protected function dispatchMessage($message, string $topic = null): Envelope
{
if (!$this->container->has('message_bus')) {
throw new \LogicException('The message bus is not enabled in your application. Try running "composer require symfony/messenger".');
}

return $this->container->get('message_bus')->dispatch($message);
return $this->container->get('message_bus')->dispatch($message, $topic);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder

$defaultMiddleware = array(
'before' => array(array('id' => 'logging')),
'after' => array(array('id' => 'route_messages'), array('id' => 'call_message_handler')),
'after' => array(array('id' => 'send_message'), array('id' => 'handle_message')),
);
foreach ($config['buses'] as $busId => $bus) {
$middleware = $bus['middleware'];
Expand Down Expand Up @@ -1572,31 +1572,27 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
$senderAliases[$name] = $transportId;
}

$messageToSenderIdMapping = array();
$messageToSendAndHandleMapping = array();
foreach ($config['routing'] as $message => $messageConfiguration) {
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false)) {
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));
}

if (1 < \count($messageConfiguration['senders'])) {
$topicToSenderIdMapping = array();
$topicsToSendAndHandle = array();
foreach ($config['routing'] as $topic => $topicConfiguration) {
if (1 < \count($topicConfiguration['senders'])) {
$senders = array_map(function ($sender) use ($senderAliases) {
return new Reference($senderAliases[$sender] ?? $sender);
}, $messageConfiguration['senders']);
}, $topicConfiguration['senders']);
$chainSenderDefinition = new Definition(ChainSender::class, array($senders));
$chainSenderDefinition->addTag('messenger.sender');
$chainSenderId = '.messenger.chain_sender.'.$message;
$chainSenderId = '.messenger.chain_sender.'.$topic;
$container->setDefinition($chainSenderId, $chainSenderDefinition);
$messageToSenderIdMapping[$message] = $chainSenderId;
$topicToSenderIdMapping[$topic] = $chainSenderId;
} else {
$messageToSenderIdMapping[$message] = $messageConfiguration['senders'][0];
$topicToSenderIdMapping[$topic] = $topicConfiguration['senders'][0];
}

$messageToSendAndHandleMapping[$message] = $messageConfiguration['send_and_handle'];
$topicsToSendAndHandle[$topic] = $topicConfiguration['send_and_handle'];
}

$container->getDefinition('messenger.asynchronous.routing.sender_locator')->replaceArgument(1, $messageToSenderIdMapping);
$container->getDefinition('messenger.middleware.route_messages')->replaceArgument(1, $messageToSendAndHandleMapping);
$container->getDefinition('messenger.asynchronous.routing.sender_locator')->replaceArgument(1, $topicToSenderIdMapping);
$container->getDefinition('messenger.middleware.send_message')->replaceArgument(1, $topicsToSendAndHandle);
}

private function registerCacheConfiguration(array $config, ContainerBuilder $container)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<argument type="service" id="messenger.sender_locator" />
<argument type="collection" /> <!-- Message to sender ID mapping -->
</service>
<service id="messenger.middleware.route_messages" class="Symfony\Component\Messenger\Middleware\SendMessageMiddleware">
<service id="messenger.middleware.send_message" class="Symfony\Component\Messenger\Middleware\SendMessageMiddleware">
<argument type="service" id="messenger.asynchronous.routing.sender_locator" />
<argument type="collection" /> <!-- Message to send and handle mapping -->
</service>
Expand All @@ -25,7 +25,7 @@
</service>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
'messenger.bus.queries' => array(
'default_middleware' => false,
'middleware' => array(
'route_messages',
'call_message_handler',
'send_message',
'handle_message',
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</framework:middleware>
</framework:bus>
<framework:bus name="messenger.bus.queries" default-middleware="false">
<framework:middleware id="route_messages" />
<framework:middleware id="call_message_handler" />
<framework:middleware id="send_message" />
<framework:middleware id="handle_message" />
</framework:bus>
</framework:messenger>
</framework:config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ framework:
messenger.bus.queries:
default_middleware: false
middleware:
- "route_messages"
- "call_message_handler"
- "send_message"
- "handle_message"
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public function testMessengerRouting()
{
$container = $this->createContainerFromFile('messenger_routing');
$senderLocatorDefinition = $container->getDefinition('messenger.asynchronous.routing.sender_locator');
$sendMessageMiddlewareDefinition = $container->getDefinition('messenger.middleware.route_messages');
$sendMessageMiddlewareDefinition = $container->getDefinition('messenger.middleware.send_message');

$messageToSenderIdsMapping = array(
DummyMessage::class => '.messenger.chain_sender.'.DummyMessage::class,
Expand Down Expand Up @@ -619,22 +619,22 @@ public function testMessengerWithMultipleBuses()
$this->assertSame(array(), $container->getDefinition('messenger.bus.commands')->getArgument(0));
$this->assertEquals(array(
array('id' => 'logging'),
array('id' => 'route_messages'),
array('id' => 'call_message_handler'),
array('id' => 'send_message'),
array('id' => 'handle_message'),
), $container->getParameter('messenger.bus.commands.middleware'));
$this->assertTrue($container->has('messenger.bus.events'));
$this->assertSame(array(), $container->getDefinition('messenger.bus.events')->getArgument(0));
$this->assertEquals(array(
array('id' => 'logging'),
array('id' => 'with_factory', 'arguments' => array('foo', true, array('bar' => 'baz'))),
array('id' => 'route_messages'),
array('id' => 'call_message_handler'),
array('id' => 'send_message'),
array('id' => 'handle_message'),
), $container->getParameter('messenger.bus.events.middleware'));
$this->assertTrue($container->has('messenger.bus.queries'));
$this->assertSame(array(), $container->getDefinition('messenger.bus.queries')->getArgument(0));
$this->assertEquals(array(
array('id' => 'route_messages', 'arguments' => array()),
array('id' => 'call_message_handler', 'arguments' => array()),
array('id' => 'send_message', 'arguments' => array()),
array('id' => 'handle_message', 'arguments' => array()),
), $container->getParameter('messenger.bus.queries.middleware'));

$this->assertTrue($container->hasAlias('message_bus'));
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ CHANGELOG

* The component is not experimental anymore
* All the changes below are BC BREAKS
* subscribing to messages based on their interfaces or parent classes has been removed in favor of topic-based subscription
* `MessageBusInterface::dispatch()`, `MiddlewareInterface::handle()` and `SenderInterface::send()` return `Envelope`
* `MessageBusInterface::dispatch()` now takes a second `string $topic = null` argument
* `MiddlewareInterface::handle()` now require an `Envelope` as first argument and a `StackInterface` as second
* `EnvelopeAwareInterface` has been removed
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
Expand All @@ -21,16 +23,14 @@ CHANGELOG
as first constructor argument
* The `EncoderInterface` and `DecoderInterface` have been replaced by a unified `Symfony\Component\Messenger\Transport\Serialization\SerializerInterface`.
* The locator passed to `ContainerHandlerLocator` should not prefix its keys by "handler." anymore
* The `AbstractHandlerLocator::getHandler()` method uses `?callable` as return type
* Renamed `EnvelopeItemInterface` to `StampInterface`
* `Envelope`'s constructor and `with()` method now accept `StampInterface` objects as variadic parameters
* Renamed and moved `ReceivedMessage`, `ValidationConfiguration` and `SerializerConfiguration` in the `Stamp` namespace
* Removed the `WrapIntoReceivedMessage`
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
* `MessengerDataCollector::getMessages()` returns an iterable, not just an array anymore
* `AbstractHandlerLocator` is now internal
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(Envelope $envelope): ?callable` and shouldn't throw when no handlers are found
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
* The `AbstractHandlerLocator` and `AbstractSenderLocator` classes have been removed
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(string $topic): ?callable` and shouldn't throw when no handlers are found
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(string $topic): ?SenderInterface`
* Classes in the `Middleware\Enhancers` sub-namespace have been moved to the `Middleware` one
* Classes in the `Asynchronous\Routing` sub-namespace have been moved to the `Transport\Sender\Locator` sub-namespace
* The `Asynchronous/Middleware/SendMessageMiddleware` class has been moved to the `Middleware` namespace
Expand Down
Loading
0