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