jozi / laravel-rabbitevents-sourcing
Integration between nuwber/rabbitevents and spatie/laravel-event-sourcing packages
Requires
- php: ^8.0
- laravel/lumen-framework: ^8.0
- nuwber/rabbitevents: ^6.0
- spatie/laravel-event-sourcing: ^5.0
This package is auto-updated.
Last update: 2025-03-01 00:28:31 UTC
README
A simple integration between nuwber/rabbitevents
and spatie/laravel-event-sourcing
.
Both are used to facilitate event sourcing and intraservice communication using RabbitMQ topic exchanges.
Installation
Using composer:
composer require jozi/laravel-rabbitevents-sourcing
Usage/Examples
All stored and published events extends the StoredRabbitEvent
class. These events will be handled for both event sourcing (spatie/laravel-event-sourcing
) and publishing to RabbitMQ (nuwber/rabbitevents
).
For this class, a string $eventKey
is explictly required for the event to be published. The $eventKey
is the same as RabbitMQ's routing key.
use Jozi\Events\StoredRabbitEvent; class AccountCreated extends StoredRabbitEvent { public $eventKey = 'account.created'; /** @var array */ public $accountAttributes; public function __construct(array $accountAttributes) { $this->accountAttributes = $accountAttributes; } public function toPublish(): array { return $this->accountAttributes; } }
After an event has been defined, you may invoke it using the publish_event
helper function. It is just a simple wrapper for invoking both event
and publish
as a one-liner.
class Account extends Model { protected $guarded = []; public static function createWithAttributes(array $attributes): Account { /* * Let's generate a uuid. */ $attributes['uuid'] = (string) Uuid::uuid4(); /* * The account will be created inside this event using the generated uuid. */ publish_event(new AccountCreated($attributes)); /* * The uuid will be used the retrieve the created account. */ return static::getByUuid($attributes['uuid']); } }