* 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.
* Added factory methods to `DelayStamp`.
+* Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call
5.1.0
-----
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
if (null !== $envelope->last(DispatchAfterCurrentBusStamp::class)) {
- if (!$this->isRootDispatchCallRunning) {
- throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBusStamp::class));
+ if ($this->isRootDispatchCallRunning) {
+ $this->queue[] = new QueuedEnvelope($envelope, $stack);
+
+ return $envelope;
}
- $this->queue[] = new QueuedEnvelope($envelope, $stack);
- return $envelope;
+ $envelope = $envelope->withoutAll(DispatchAfterCurrentBusStamp::class);
}
if ($this->isRootDispatchCallRunning) {
$messageBus->dispatch($message);
}
+ public function testDispatchOutOfAnotherHandlerDispatchesAndRemoveStamp()
+ {
+ $event = new DummyEvent('First event');
+
+ $middleware = new DispatchAfterCurrentBusMiddleware();
+ $handlingMiddleware = $this->createMock(MiddlewareInterface::class);
+
+ $handlingMiddleware
+ ->method('handle')
+ ->with($this->expectHandledMessage($event))
+ ->will($this->willHandleMessage());
+
+ $eventBus = new MessageBus([
+ $middleware,
+ $handlingMiddleware,
+ ]);
+
+ $enveloppe = $eventBus->dispatch($event, [new DispatchAfterCurrentBusStamp()]);
+
+ self::assertNull($enveloppe->last(DispatchAfterCurrentBusStamp::class));
+ }
+
private function expectHandledMessage($message): Callback
{
return $this->callback(function (Envelope $envelope) use ($message) {