Symfony 8.2 New Published on

The shop follows the whole import, with PropagatedStampInterface

A stamp implementing PropagatedStampInterface is copied onto every message sent during handling, and the handler gets it as an argument: no more context to pass from message to message.

Topics covered: Messenger

New in Symfony 8.2 (in development). When a handler sends a follow-up message, that message leaves with an empty envelope. A piece of context, like a shop or trace id, therefore ends up as a property of every message in the flow, only to pass it along to the next one.

PropagatedStampInterface

A stamp implementing the new interface is copied onto every message sent while handling the message that carries it. And a handler can now get a stamp (or the whole Envelope) as an argument, after the message:

class TenantStamp implements PropagatedStampInterface
{
    public function __construct(
        public readonly string $id,
    ) {
    }
}

$bus->dispatch(new ImportCatalog($path), [new TenantStamp($tenant->id)]);

#[AsMessageHandler]
class ImportRowHandler
{
    public function __invoke(ImportRow $row, TenantStamp $tenant): void
    {
        $this->catalogs->for($tenant->id)->add($row);
    }
}

The same version adds the identity_stamps option: each message then carries its own id (MessageIdStamp), the id of the message whose handling caused it (CausationStamp) and the id of the whole flow (CorrelationStamp). Handlers can get these as arguments too, for instance for logs.

Your task

A catalog import leaves with a BoutiqueStamp. Its handler sends one AjouterProduit per product, without dealing with the shop, so the AjouterProduit handler doesn't know where to put the product: everything ends up in an "unknown" catalog.

  1. Make the BoutiqueStamp follow the messages sent during the import.
  2. Get it as an argument in AjouterProduitHandler, and put the product in the right shop.

The messages and the import handler don't change: the shop stays out of the messages.

Where the feature comes from: the original pull request.