For the complete documentation index, see llms.txt. This page is also available as Markdown.

Extending Message Buses (Gateways)

Extending Command, Event, and Query Buses with custom Gateways

You want webhook commands to retry 3 times and dedup on paymentId, but internal admin commands to fail fast with no retry. Both go through your CommandBus. Extending the bus interface lets you declare a WebhookCommandBus extends CommandBus, attach #[InstantRetry] and #[Deduplicated] directly to it, and inject the typed bus where you want those policies — same handlers, different policies, no runtime branching.

For better understanding, please read Interceptors section before going through this chapter.

Intercepting Gateways

Suppose we want to add custom logging, whenever any Command is executed. We know that CommandBus is a interface for sending Commands, therefore we need to hook into that Gateway.

class LoggerInterceptor
{
    #[Before(pointcut: CommandBus::class)]
    public function log(object $command, array $metadata) : void
    {
        // log Command message
    }
}

Intercepting Gateways, does not differ from intercepting Message Handlers.

Building customized Gateways

We may also want to have different types of Message Buses for given Message Type. For example we could have EventBus with audit which we would use in specific cases. Therefore we want to keep the original EventBus untouched, as for other scenarios we would simply keep using it.

To do this, we will introduce our new EventBus:

interface AuditableEventBus extends EventBus {}

That's basically enough to register our new interface. This new Gateway will be automatically registered in our DI container, so we will be able to inject it and use.

Now as this is separate interface, we can point interceptor specifically on this

Pointcut by attributes

We could of course intercept by attributes, if we would like to make audit functionality reusable

and then we pointcut based on the attribute

Asynchronous Gateways

Gateways can also be extended with asynchronous functionality on which you can read more in Asynchronous section.

Last updated

Was this helpful?