jozi/laravel-rabbitevents-sourcing
Composer 安装命令:
composer require jozi/laravel-rabbitevents-sourcing
包简介
Integration between nuwber/rabbitevents and spatie/laravel-event-sourcing packages
README 文档
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']); } }
Acknowledgements
License
统计信息
- 总下载量: 56
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-12