Skip to content

Commit 52a946f

Browse files
committed
[Messenger] HandlerArgumentsStamp
1 parent 74e436b commit 52a946f

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

messenger.rst

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ to retry them:
926926
927927
# see all messages in the failure transport with a default limit of 50
928928
$ php bin/console messenger:failed:show
929-
929+
930930
# see the 10 first messages
931931
$ php bin/console messenger:failed:show --max=10
932932
@@ -2263,6 +2263,62 @@ of the process. For each, the event class is the event name:
22632263
* :class:`Symfony\\Component\\Messenger\\Event\\WorkerRunningEvent`
22642264
* :class:`Symfony\\Component\\Messenger\\Event\\WorkerStoppedEvent`
22652265

2266+
Additional Handler Arguments
2267+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2268+
2269+
It's possible to have messenger pass additional data to the message handler using the
2270+
:class:`Symfony\\Component\\Messenger\\Stamp\\HandlerArgumentsStamp`. Add this stamp to
2271+
the envelope in a middleware and fill it with any additional data you want to have
2272+
available in the handler.
2273+
2274+
// src/Library/Messenger/AdditionalArgumentMiddleware.php
2275+
namespace App\Library\Messenger;
2276+
2277+
use Symfony\Component\Messenger\Envelope;
2278+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
2279+
use Symfony\Component\Messenger\Middleware\StackInterface;
2280+
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
2281+
2282+
class AdditionalArgumentMiddleware implements MiddlewareInterface
2283+
{
2284+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
2285+
{
2286+
$envelope = $envelope->with(
2287+
new HandlerArgumentsStamp(
2288+
[
2289+
$this->resolveAdditionalArgument($envelope->getMessage()),
2290+
],
2291+
),
2292+
)
2293+
2294+
return $stack->next()->handle($envelope, $stack);
2295+
}
2296+
2297+
private function resolveAdditionalArgument(object $message): mixed
2298+
{
2299+
// ...
2300+
}
2301+
}
2302+
2303+
Then your handler will look like this:
2304+
2305+
// src/MessageHandler/SmsNotificationHandler.php
2306+
namespace App\MessageHandler;
2307+
2308+
use App\Message\SmsNotification;
2309+
2310+
class SmsNotificationHandler
2311+
{
2312+
public function __invoke(SmsNotification $message, mixed $additionalArgument)
2313+
{
2314+
// ...
2315+
}
2316+
}
2317+
2318+
.. versionadded:: 6.2
2319+
2320+
The HandlerArgumentsStamp was introduced in Symfony 6.2.
2321+
22662322
Multiple Buses, Command & Event Buses
22672323
-------------------------------------
22682324

0 commit comments

Comments
 (0)