swag-industries/doctrine-domain-events

v3.1.0 2024-08-05 22:52 UTC

README

Build Status Latest Stable Version License

这个库旨在帮助您使用领域设计开发方法构建应用程序。

它与以下内容良好集成

  • Symfony >= 5.5 (为了与 >=4.4 兼容,请安装 Doctrine 领域事件的 2.2 版本)
  • ApiPlatform >= 2.1
  • Doctrine >= 2.5
  • PHP >= 7.4

但您可以使用它与任何 PHP 项目。

这里有一些幻灯片解释了我们是怎样到达这一点的。

特性

领域事件

缺点

这个库旨在允许您使用 Doctrine 模型作为领域模型。这有一些代价:您不能再手动实例化领域模型。这意味着您需要为您的领域模型使用任何使用情况的工厂。

此组件为 Symfony 序列化和 Doctrine 提供了实现。对于您自己的需求,您应使用类(如果您使用捆绑包,则使用服务)Biig\Component\Domain\Model\Instantiator\Instantiator

安装

composer require swag-industries/doctrine-domain-events

基本用法

class YourModel extends DomainModel
{
    public const CREATION = 'creation';
    public function __construct()
    {
        $this->dispatch(new DomainEvent($this), self::CREATION);
    }
}
class DomainRule implements DomainRuleInterface
{
    public function on()
    {
        return YourModel::CREATION;
    }
    
    public function execute(DomainEvent $event)
    {
        // Do Something on your model creation
    }
}

由于您的模型需要一个分发器,因此您需要每次创建新实例时都调用 setDispatcher() 方法。为了避免手动执行此操作,您可以使用库提供的 Instantiator

它不使用构造函数来添加分发器,因为在 PHP 中可以创建没有构造函数的对象。例如,这就是 Doctrine 所做的。

use Biig\Component\Domain\Model\Instantiator\Instantiator;
use Doctrine\ORM\EntityManager;

class SomeController
{
    public function index(Instantiator $instantiator, EntityManager $entityManager)
    {
        $model = $instantiator->instantiate(YourModel::class);
        $entityManager->persist($model);
        $entityManager->flush();
    }
}

与 Symfony 集成

使用捆绑包

<?php
// config/bundles.php

return [
    // ...
    Biig\Component\Domain\Integration\Symfony\DomainBundle::class => ['all' => true],
];

了解更多关于 Symfony 集成的信息

版本