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

Skip to content

Commit 0aae1b4

Browse files
committed
feature #37976 [Messenger] Don't prevent dispatch out of another bus (ogizanagi)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Messenger] Don't prevent dispatch out of another bus | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | #35814 (comment) <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | N/A Commits ------- 1e8ae43 [Messenger] Don't prevent dispatch out of another bus
2 parents 9cd3e67 + 1e8ae43 commit 0aae1b4

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Added `FlattenExceptionNormalizer` to give more information about the exception on Messenger background processes. The `FlattenExceptionNormalizer` has a higher priority than `ProblemNormalizer` and it is only used when the Messenger serialization context is set.
88
* Added factory methods to `DelayStamp`.
9+
* Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call
910

1011
5.1.0
1112
-----

src/Symfony/Component/Messenger/Middleware/DispatchAfterCurrentBusMiddleware.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ class DispatchAfterCurrentBusMiddleware implements MiddlewareInterface
4444
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4545
{
4646
if (null !== $envelope->last(DispatchAfterCurrentBusStamp::class)) {
47-
if (!$this->isRootDispatchCallRunning) {
48-
throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBusStamp::class));
47+
if ($this->isRootDispatchCallRunning) {
48+
$this->queue[] = new QueuedEnvelope($envelope, $stack);
49+
50+
return $envelope;
4951
}
50-
$this->queue[] = new QueuedEnvelope($envelope, $stack);
5152

52-
return $envelope;
53+
$envelope = $envelope->withoutAll(DispatchAfterCurrentBusStamp::class);
5354
}
5455

5556
if ($this->isRootDispatchCallRunning) {

src/Symfony/Component/Messenger/Tests/Middleware/DispatchAfterCurrentBusMiddlewareTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,28 @@ public function testHandleDelayedEventFromQueue()
256256
$messageBus->dispatch($message);
257257
}
258258

259+
public function testDispatchOutOfAnotherHandlerDispatchesAndRemoveStamp()
260+
{
261+
$event = new DummyEvent('First event');
262+
263+
$middleware = new DispatchAfterCurrentBusMiddleware();
264+
$handlingMiddleware = $this->createMock(MiddlewareInterface::class);
265+
266+
$handlingMiddleware
267+
->method('handle')
268+
->with($this->expectHandledMessage($event))
269+
->will($this->willHandleMessage());
270+
271+
$eventBus = new MessageBus([
272+
$middleware,
273+
$handlingMiddleware,
274+
]);
275+
276+
$enveloppe = $eventBus->dispatch($event, [new DispatchAfterCurrentBusStamp()]);
277+
278+
self::assertNull($enveloppe->last(DispatchAfterCurrentBusStamp::class));
279+
}
280+
259281
private function expectHandledMessage($message): Callback
260282
{
261283
return $this->callback(function (Envelope $envelope) use ($message) {

0 commit comments

Comments
 (0)