@@ -2413,6 +2413,62 @@ of the process. For each, the event class is the event name:
2413
2413
* :class: `Symfony\\ Component\\ Messenger\\ Event\\ WorkerRunningEvent `
2414
2414
* :class: `Symfony\\ Component\\ Messenger\\ Event\\ WorkerStoppedEvent `
2415
2415
2416
+ Additional Handler Arguments
2417
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2418
+
2419
+ It's possible to have messenger pass additional data to the message handler using the
2420
+ :class: `Symfony\\ Component\\ Messenger\\ Stamp\\ HandlerArgumentsStamp `. Add this stamp to
2421
+ the envelope in a middleware and fill it with any additional data you want to have
2422
+ available in the handler.
2423
+
2424
+ // src/Library/Messenger/AdditionalArgumentMiddleware.php
2425
+ namespace App\L ibrary\M essenger;
2426
+
2427
+ use Symfony\C omponent\M essenger\E nvelope;
2428
+ use Symfony\C omponent\M essenger\M iddleware\M iddlewareInterface;
2429
+ use Symfony\C omponent\M essenger\M iddleware\S tackInterface;
2430
+ use Symfony\C omponent\M essenger\S tamp\H andlerArgumentsStamp;
2431
+
2432
+ class AdditionalArgumentMiddleware implements MiddlewareInterface
2433
+ {
2434
+ public function handle(Envelope $envelope, StackInterface $stack): Envelope
2435
+ {
2436
+ $envelope = $envelope->with(
2437
+ new HandlerArgumentsStamp(
2438
+ [
2439
+ $this->resolveAdditionalArgument($envelope->getMessage()),
2440
+ ],
2441
+ ),
2442
+ )
2443
+
2444
+ return $stack->next()->handle($envelope, $stack);
2445
+ }
2446
+
2447
+ private function resolveAdditionalArgument(object $message): mixed
2448
+ {
2449
+ // ...
2450
+ }
2451
+ }
2452
+
2453
+ Then your handler will look like this:
2454
+
2455
+ // src/MessageHandler/SmsNotificationHandler.php
2456
+ namespace App\M essageHandler;
2457
+
2458
+ use App\M essage\S msNotification;
2459
+
2460
+ class SmsNotificationHandler
2461
+ {
2462
+ public function __invoke(SmsNotification $message, mixed $additionalArgument)
2463
+ {
2464
+ // ...
2465
+ }
2466
+ }
2467
+
2468
+ .. versionadded :: 6.2
2469
+
2470
+ The HandlerArgumentsStamp was introduced in Symfony 6.2.
2471
+
2416
2472
Multiple Buses, Command & Event Buses
2417
2473
-------------------------------------
2418
2474
0 commit comments