Laravel Client

The Laravel client wraps the framework-agnostic PHP client and hooks it into Laravel's service container, configuration, artisan commands and the queue system — so EventSourcerer feels like a native Laravel package.

Installation

composer require eventsourcerer/event-sourcerer-laravel

Publish the configuration file:

php artisan vendor:publish \
    --provider="Eventsourcerer\EventSourcererLaravel\Providers\EventSourcererProvider"

Run the migrations that ship with the package:

php artisan migrate

Configuration

Add the following entries to your .env:

EVENT_SOURCERER_APPLICATION_ID=your-application-id
EVENT_SOURCERER_SERVER_HOST=eventsourcerer.local
EVENT_SOURCERER_SERVER_PORT=9000
EVENT_SOURCERER_SERVER_URL=https://eventsourcerer.local

The published config/eventsourcerer.php wires those values into the client.

Writing events from artisan

php artisan eventsourcerer:write-new-event \
    {streamId} {eventName} {eventVersion} {jsonPayload}

# example
php artisan eventsourcerer:write-new-event \
    basket-42 ItemAddedToBasket 1 '{"productId":"abc","amount":1999}'

Listening for events

The package ships with a dedicated queue connection called eventsourcerer. Events are received over the socket and pushed onto the queue, so you process them with the regular Laravel worker tooling.

# start the EventSourcerer listener
php artisan eventsourcerer:listen-for-events my-worker

# process queued events as usual
php artisan queue:work eventsourcerer

Writing your own event handler

By default, every incoming event is dispatched as a NewEventJob on the eventsourcerer connection. Override this by binding a custom EventHandler:

use Eventsourcerer\EventSourcererLaravel\EventHandler;

$this->app->singleton(EventHandler::class, fn () => new class implements EventHandler {
    public function handle(): callable
    {
        return function (array $event): void {
            // route by event name, dispatch jobs, fire Laravel events, etc.
        };
    }
});

Writing events programmatically

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

public function __invoke(Client $client): void
{
    $client
        ->connect()
        ->writeNewEvent(
            StreamId::fromString('user-' . $user->id),
            EventName::fromString('UserRegistered'),
            EventVersion::fromString('1'),
            ['email' => $user->email],
        );
}