swag-industries / doctrine-domain-events
v3.1.0
2024-08-05 22:52 UTC
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^1.8|^2.0
- doctrine/orm: ^2.18.3|^3.0
- symfony/event-dispatcher: ^6.4|^7.0
Requires (Dev)
- doctrine/orm: ^2.18.3
- friendsofphp/php-cs-fixer: ^3.49.0
- monolog/monolog: ^2.8.0 || ^3.2.0
- phpspec/prophecy-phpunit: ^2.0.1
- phpunit/phpunit: ^9.5 || ^10
- symfony/asset: ^6.4 || ^7.0
- symfony/debug-pack: ^1.0
- symfony/form: ^6.4 || ^7.0
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/property-access: ^6.4 || ^7.0
- symfony/serializer: ^6.4 || ^7.0
- symfony/var-dumper: ^6.4 || ^7.0
- symfony/yaml: ^6.4 || ^7.0
Conflicts
- doctrine/orm: <2.6.3
This package is auto-updated.
Last update: 2024-09-05 23:20:09 UTC
README
这个库旨在帮助您使用领域设计开发方法构建应用程序。
它与以下内容良好集成
- 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 集成的信息