Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit756a0ca

Browse files
enumagOskarStark
authored andcommitted
[Messenger] HandlerArgumentsStamp
1 parentcd4a29d commit756a0ca

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)

[8]ページ先頭

©2009-2025 Movatter.jp