Symfony Client

The Symfony client ships as a bundle. It registers the EventSourcerer client into the container and exposes a custom Messenger transport, so consuming events is as simple as binding a message handler.

Installation

composer require eventsourcerer/symfony-client

Enable the bundle (if Flex hasn't done it for you) in config/bundles.php:

return [
    // ...
    EventSourcerer\ClientBundle\EventSourcererBundle::class => ['all' => true],
];

Configuration

Set your environment variables in .env:

EVENT_SOURCERER_APPLICATION_ID=your-application-id
EVENT_SOURCERER_HOST=eventsourcerer.local
EVENT_SOURCERER_PORT=9000
EVENT_SOURCERER_URL=https://eventsourcerer.local

The Messenger transport

Add an EventSourcerer-backed transport in config/packages/messenger.yaml:

framework:
    messenger:
        transports:
            events_from_sourcerer:
                dsn: 'es://default'

        routing:
            'EventSourcerer\ClientBundle\NewMessage': events_from_sourcerer

The transport's DSN must start with es://. Incoming events from EventSourcerer are wrapped as NewMessage instances and dispatched through Messenger.

Consuming events

use EventSourcerer\ClientBundle\NewMessage;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final class HandleNewEvent
{
    public function __invoke(NewMessage $message): void
    {
        $event = $message->event;
        // route by $event['name'] / $event['version']
    }
}

Run a worker just like any other Messenger transport:

bin/console messenger:consume events_from_sourcerer -vv

Writing events

use PearTreeWeb\EventSourcerer\Client\Infrastructure\Client;
use PearTreeWebLtd\EventSourcererMessageUtilities\Model\{StreamId, EventName, EventVersion};

public function __construct(private Client $client) {}

public function placeOrder(Order $order): void
{
    $this->client
        ->connect()
        ->writeNewEvent(
            StreamId::fromString('order-' . $order->id),
            EventName::fromString('OrderPlaced'),
            EventVersion::fromString('1'),
            ['total' => $order->total, 'currency' => $order->currency],
        );
}

Console command

The bundle also registers a command to write events for quick experiments:

bin/console eventsourcerer:write-new-event \
    order-42 OrderPlaced 1 '{"total":1999,"currency":"GBP"}'