phpgears/aggregate

聚合基础

0.2.1 2019-10-25 22:51 UTC

This package is auto-updated.

Last update: 2024-09-17 06:56:34 UTC


README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

聚合

聚合基础

安装

Composer

composer require phpgears/aggregate

用法

需要composer自动加载文件

require './vendor/autoload.php';

聚合身份

聚合身份由gears/identity提供,前往那里了解相关信息

聚合根

聚合根应该实现Gears\Aggregate\AggregateRoot接口。为了简单起见,你可以从Gears\Aggregate\AbstractAggregateRoot扩展

use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
    public static function instantiate(Identity $identity): self
    {
        return new self($identity);
    }
}

请注意,AbstractAggregateRoot构造函数是受保护的,这强制你创建静态命名构造函数方法

实体

实体可以实现Gears\Aggregate\Entity接口。为了简单起见,你可以从Gears\Aggregate\AbstractEntity扩展

事件

在执行操作时,聚合根可以记录gears/event

use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
    public static function instantiate(Identity $identity): self
    {
        return new self($identity);
    }

    public function doSomething(): void
    {
        // do something

        $this->recordEvent(new SomethingHappened());
    }
}

这些事件可以在之后收集并发送到事件总线,例如gears/event

$customAggregate = CustomAggregate::instantiate(
    UuidIdentity::fromString('4c4316cb-b48b-44fb-a034-90d789966bac')
);
$customAggregate->doSomething();

foreach ($customAggregate->collectRecordedEvents() as $event) {
    /** @var \Gears\Event\EventBus $eventBus */
    $eventBus->dispatch($event);
}

贡献

发现了错误或有功能请求?请创建一个新问题。在提交之前先查看现有问题。

请参阅CONTRIBUTING.md文件

许可

请参阅源代码中包含的LICENSE文件,以获取许可证条款的副本。