Skip to content

Commit e528ca9

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: Minor [Messenger] HandlerArgumentsStamp
2 parents 285541c + 8f966c0 commit e528ca9

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

messenger.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
26462699
Multiple Buses, Command & Event Buses
26472700
-------------------------------------
26482701

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy