sbooker / domain-events
领域事件
2.0.3
2021-06-22 09:10 UTC
Requires
- php: ^7.4 || ^8.0
- ramsey/uuid: ^4.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
安装
通过 Composer 安装
composer require sbooker/domain-events
使用方法
发布事件
<?php namespace Domain; use Sbooker\DomainEvents\DomainEntity; use Sbooker\DomainEvents\DomainEvent; use Sbooker\DomainEvents\DomainEventCollector; class SomethingOccurred extends DomainEvent {} class SomeAggregateRoot implements DomainEntity { use DomainEventCollector; public function doSomethingOfDomainLogic() { // do $this->publish(new SomethingOccurred()); } }
分发事件
使用 Sbooker\Domain\Events\Publihser 实现分发事件
class SomeAggregateRoot implements DomainEntity { use DomainEventCollector; ... } $publisher = new class implements Publihser { ... } $aggregateRoot = new SomeAggregateRoot(); $aggregateRoot->dispatchEvents($publisher)
在更复杂的情况下,如果聚合根包含一个或多个领域实体,你必须覆盖 DomainEventCollector::dispatchEvents,如下所示
class Entity implements DomainEntity { ... } class SomeAggregateRoot implements DomainEntity { use DomainEventCollector { dispatchEvents as doDispatchEvents; } private Entity $entity; ... public function dispatchEvents(Publisher $publisher): void { $this->doDispatchEvents($publisher); $this->entity->dispatchEvents($publisher); } }
许可
请参阅 LICENSE 文件。