@@ -2643,6 +2643,59 @@ of the process. For each, the event class is the event name:
26432643
26442644 The ``WorkerRateLimitedEvent `` was introduced in Symfony 6.2.
26452645
2646+ Additional Handler Arguments
2647+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2648+
2649+ It's possible to have messenger pass additional data to the message handler
2650+ using the :class: `Symfony\\ Component\\ Messenger\\ Stamp\\ HandlerArgumentsStamp `.
2651+ Add this stamp to the envelope in a middleware and fill it with any additional
2652+ data you want to have available in the handler::
2653+
2654+ // src/Messenger/AdditionalArgumentMiddleware.php
2655+ namespace App\Messenger;
2656+
2657+ use Symfony\Component\Messenger\Envelope;
2658+ use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
2659+ use Symfony\Component\Messenger\Middleware\StackInterface;
2660+ use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
2661+
2662+ final class AdditionalArgumentMiddleware implements MiddlewareInterface
2663+ {
2664+ public function handle(Envelope $envelope, StackInterface $stack): Envelope
2665+ {
2666+ $envelope = $envelope->with(new HandlerArgumentsStamp([
2667+ $this->resolveAdditionalArgument($envelope->getMessage()),
2668+ ]));
2669+
2670+ return $stack->next()->handle($envelope, $stack);
2671+ }
2672+
2673+ private function resolveAdditionalArgument(object $message): mixed
2674+ {
2675+ // ...
2676+ }
2677+ }
2678+
2679+ Then your handler will look like this::
2680+
2681+ // src/MessageHandler/SmsNotificationHandler.php
2682+ namespace App\MessageHandler;
2683+
2684+ use App\Message\SmsNotification;
2685+
2686+ final class SmsNotificationHandler
2687+ {
2688+ public function __invoke(SmsNotification $message, mixed $additionalArgument)
2689+ {
2690+ // ...
2691+ }
2692+ }
2693+
2694+ .. versionadded :: 6.2
2695+
2696+ The :class: `Symfony\\ Component\\ Messenger\\ Stamp\\ HandlerArgumentsStamp `
2697+ was introduced in Symfony 6.2.
2698+
26462699Multiple Buses, Command & Event Buses
26472700-------------------------------------
26482701
0 commit comments