Messaging Gateway
Message Gateway PHP
The Messaging Gateway encapsulates messaging-specific code (The code required to send or receive a Message) and separates it from the rest of the application code.
It takes Application specific data and convert it Message which then is sent via Message channel. This hide Messaging specific code, from user's code.
Command/Query/Event Buses are implementations of Messaging Gateways.
Ecotone aims for eliminating Framework related code from Business related code, that's why Gateway can defined as interface in user's code base. Ecotone is responsible for generating implementation for any interface.

Implementing own Gateway

1
namespace Product;
2
3
use Ecotone\Messaging\Annotation\Gateway;
4
5
//1
6
interface ProductGateway
7
{
8
#[Gateway("buyProduct")] // 2
9
public function order(string $productName) : void;
10
}
Copied!
  1. 1.
    By default gateway will be available under interface name in DI, in that case Product\ProductGateway. If you want to register it under different name for example "productGateway", then pass it to annotation #[ClassReference("productGateway")]
Symfony / Laravel
Lite
If you are using Symfony Integration, then it will be auto registered in Dependency Container with possibility to auto-wire the gateway.
As Command/Query/Event buses are Gateways, you can auto-wire them. They can be injected into Controller and called directly.
In Lite Configuration you can retrieve it using messaging system configuration.
1
$productGateway = $messagingSystem->getGatewayByName(ProductGateway::class);
Copied!
2. Gateway enables method to be used as Messaging Gateway. You may have multiple Gateways defined within interface.

Building your own Bus

Using combination of Messaging Gateway and Router we can build our own Buses. Messaging Gateway will be responsible for building Message and sending to the Router. And Router will be then based on need route it by payload / headers or whatever is needed.

Invoking Handler directly

In some cases you may want to invoke Query/Command Handler directly, not via Bus. In that case you may define Message Gateway that routes to given Handler.
Aggregate:
1
class OrderService {
2
#[CommandHandler("placeOrder")]
3
public function makeOrder(string $order)
4
{
5
// make order
6
}
7
}
Copied!
Gateway:
1
interface OrderGateway {
2
#[Gateway("placeOrder")]
3
public function placeOrder(string $order): void;
4
}
Copied!

Invoking Aggregate's Handler directly

You may expose Aggregate's Command and Query Handlers through interface.
Aggregate:
1
#[Aggregate]
2
class Order {
3
#[CommandHandler("cancelOrder")]
4
public function makeOrder(CancelOrder $command)
5
{
6
// do something
7
}
8
}
Copied!
Gateway:
1
interface OrderGateway {
2
#[Gateway("cancelOrder")]
3
public function placeOrder(#[AggregateIdentifier] string $orderId, #[Payload] CancelOrder $command)): void;
4
}
Copied!

Gateway reply

1
#[Gateway("getPrice")]
2
public function getPrice(string $productName) : int;
Copied!
Gateway may return values, but as you probably remember, everything is connected via Message Channels. So how does we get the reply? During Message building, gateway adds header replyChannel which contains automatically created Channel. During Endpoint's method invocation, if any value was returned it will be sent via reply Channel. This way gateway may receive the reply and return it.

Gateway reply Content Type

If you have registered Converter for specific Media Type, then you can tell Ecotone to convert result of any Gateway to specific format. This is especially useful, when we are dealing with QueryBus, when we want to return the result to the caller of the request. In order to do this, we need to make use of Metadataand replyContentType header.
Untitled
1
{
2
public function __construct(private QueryBus $queryBus)
3
{
4
$this->queryBus = $queryBus;
5
}
6
7
public function getTicketStatusAction(Request $request) : Response
8
{
9
return new Response(
10
$this->queryBus->sendWithMetadata(
11
new GetTicketStatusQuery($request->get("ticketId")),
12
["replyContentType" => "application/json"]
13
);
14
)
15
}
16
}
Copied!

Parameters to Message Conversion

In order to build Message, Parameter Converters are introduced. You may configure them manually or let Ecotone make use of default parameter converters.
1
#[Gateway("orders")]
2
public function placeOrder(#[Payload] Order $order, #[Header("executorId")] string $executorId);
Copied!
Parameter converter types:
  • This will convert string passed under $content parameter to message payload
1
public function sendMail(#[Payload] string $content) : void;
Copied!
  • This convert $content to message's payload and will add to headers under "receiverEmail" key value of $toEmail
1
public function sendMail(#[Payload] string $content, #[Header()] string $toEmail) : void;
Copied!