Skip to content

Commit 3da709d

Browse files
committed
feature #17174 [Messenger] HandlerArgumentsStamp (enumag)
This PR was submitted for the 6.2 branch but it was merged into the 6.3 branch instead. Discussion ---------- [Messenger] HandlerArgumentsStamp Refs * symfony/symfony#45418 Commits ------- 756a0ca [Messenger] HandlerArgumentsStamp
2 parents cd4a29d + 756a0ca commit 3da709d

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
@@ -2513,6 +2513,59 @@ of the process. For each, the event class is the event name:
25132513

25142514
The ``WorkerRateLimitedEvent`` was introduced in Symfony 6.2.
25152515

2516+
Additional Handler Arguments
2517+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2518+
2519+
It's possible to have messenger pass additional data to the message handler
2520+
using the :class:`Symfony\\Component\\Messenger\\Stamp\\HandlerArgumentsStamp`.
2521+
Add this stamp to the envelope in a middleware and fill it with any additional
2522+
data you want to have available in the handler::
2523+
2524+
// src/Messenger/AdditionalArgumentMiddleware.php
2525+
namespace App\Messenger;
2526+
2527+
use Symfony\Component\Messenger\Envelope;
2528+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
2529+
use Symfony\Component\Messenger\Middleware\StackInterface;
2530+
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
2531+
2532+
final class AdditionalArgumentMiddleware implements MiddlewareInterface
2533+
{
2534+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
2535+
{
2536+
$envelope = $envelope->with(new HandlerArgumentsStamp([
2537+
$this->resolveAdditionalArgument($envelope->getMessage()),
2538+
]));
2539+
2540+
return $stack->next()->handle($envelope, $stack);
2541+
}
2542+
2543+
private function resolveAdditionalArgument(object $message): mixed
2544+
{
2545+
// ...
2546+
}
2547+
}
2548+
2549+
Then your handler will look like this::
2550+
2551+
// src/MessageHandler/SmsNotificationHandler.php
2552+
namespace App\MessageHandler;
2553+
2554+
use App\Message\SmsNotification;
2555+
2556+
class SmsNotificationHandler
2557+
{
2558+
public function __invoke(SmsNotification $message, mixed $additionalArgument)
2559+
{
2560+
// ...
2561+
}
2562+
}
2563+
2564+
.. versionadded:: 6.2
2565+
2566+
The :class:`Symfony\\Component\\Messenger\\Stamp\\HandlerArgumentsStamp`
2567+
was introduced in Symfony 6.2.
2568+
25162569
Multiple Buses, Command & Event Buses
25172570
-------------------------------------
25182571

0 commit comments

Comments
 (0)