sbooker/domain-events

2.0.3 2021-06-22 09:10 UTC

This package is auto-updated.

Last update: 2024-09-06 21:01:25 UTC


README

Latest Version Software License PHP Version Total Downloads Build Status codecov

安装

通过 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 文件。