Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a84061

Browse files
feature #29166 [Messenger] Add handled & sent stamps (ogizanagi)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Add handled & sent stamps | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs/issues/10661 Based on #29159 This new feature marks sent and handled messages, so middleware can act upon these and use the handler(s) result(s). This is also the base of a next PR (#29167), introducing a query bus built on top of the message bus. I'm not sure yet about the best way to determine the handlers and senders names/descriptions to store in the stamps: - Handlers are callable. I've just reused the [console text descriptor](https://github.com/nicolas-grekas/symfony/blob/1c1818b87675d077808dbf7e05da84c2e1ddc9f8/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php#L457-L491) format for now. - ~~Sender are `SenderInterface` instances. `\get_class` is used for now, but a single message can be sent by multiple senders, including of the same class.~~ => Updated. Yielding the sender name if provided, the FQCN otherwise. ~~Instead, what about allowing to yield names from locators, and fallback on the above strategies otherwise? So we'll use transport names from the config for senders, and pre-computed compile-time handlers descriptions?~~ => Done. For handlers, computing it at compile time might not be straightforward. Let's compute it lazily from `HandledStamp::fromCallable()` --- ### From previous conversations: > What about not adding HandledStamp on `null` returned from handler IMHO, `null` still is a result. The stamps allows to identify a message as being handled regardless of the returned value, so makes sense on its own and keeping would require one less check for those wanting to consume it. > What about adding SentStamp? Makes sense to me and I think it was requested by @Nyholm before on Slack. So, included in this PR. > Should it target 4.2 or 4.3? Targeting 4.2, because of the removal of the handler result forwarding by middleware. A userland middleware could have used this result, typically a cache middleware. Which would now require extra boring code in userland. This will simplify it and allow users to create their query bus instance until 4.3. Commits ------- 2f5acf790a [Messenger] Add handled & sent stamps
2 parents e64fefb + 48e8ffe commit 9a84061

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,9 +1578,10 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
15781578
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false)) {
15791579
throw new LogicException(sprintf('Invalid Messenger routing configuration: class or interface "%s" not found.', $message));
15801580
}
1581-
$senders = array_map(function ($sender) use ($senderAliases) {
1582-
return new Reference($senderAliases[$sender] ?? $sender);
1583-
}, $messageConfiguration['senders']);
1581+
$senders = array();
1582+
foreach ($messageConfiguration['senders'] as $sender) {
1583+
$senders[$sender] = new Reference($senderAliases[$sender] ?? $sender);
1584+
}
15841585

15851586
$sendersId = 'messenger.senders.'.$message;
15861587
$container->register($sendersId, RewindableGenerator::class)

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,10 @@ public function testMessengerRouting()
569569
);
570570

571571
$this->assertSame($messageToSendAndHandleMapping, $senderLocatorDefinition->getArgument(1));
572-
$this->assertEquals(array(new Reference('messenger.transport.amqp'), new Reference('audit')), $container->getDefinition('messenger.senders.'.DummyMessage::class)->getArgument(0)[0]->getValues());
572+
$this->assertEquals(array(
573+
'amqp' => new Reference('messenger.transport.amqp'),
574+
'audit' => new Reference('audit'),
575+
), $container->getDefinition('messenger.senders.'.DummyMessage::class)->getArgument(0)[0]->getValues());
573576
}
574577

575578
/**

0 commit comments

Comments
 (0)