php-ddd/domain-event
最新稳定版本:v1.0.1
Composer 安装命令:
composer require php-ddd/domain-event
包简介
Library to manage domain and event
README 文档
README
This library allows you to concentrate to what matters most in your application: the domain.
According to Eric Evan's book Domain-Driven Design, your domain should be composed of aggregate.
Each aggregate is composed by an AggregateRoot and possibly some ObjectValue.
This library focus on the AggregateRoot and provides helper to manage Event happening to your aggregate.
Usage
An example will better explain the purpose of this library:
use PhpDDD\Domain\AbstractAggregateRoot; class PurchaseOrder extends AbstractAggregateRoot { /** * @var mixed */ private $customerId; /** * Try to create a new PurchaseOrder for a client. */ public function __construct(Customer $customer) { // Tests that some business rules are followed. if (false === $customer->isActive()) { throw new MyDomainException('The customer need to be active.'); } // ... // Since every rules are validated, we can simulate the creation of the Event associated // This allows us to have less redundant code $this->apply(new PurchaseOrderCreated($customer, new DateTime())); // This is equivalent as // $this->customerId = $customer->getId(); // $this->events[] = new PurchaseOrderCreated($customer, new DateTime()); // but we are not allowing to add event directly. } /** * The apply() method will automatically call this method. * Since it's an event you should never do some tests in this method. * Try to think that an Event is something that happened in the past. * You can not modify what happened. The only thing that you can do is create another event to compensate. */ protected function applyPurchaseOrderCreated(PurchaseOrderCreated $event) { $this->customerId = $event->getCustomer()->getId(); } }
What's good with this approach is that you can then listen to every events produced by your AggregateRoot and do some additional stuff that is not really relevant to your business domain like sending an email.
use PhpDDD\Domain\Event\Listener\AbstractEventListener; class SendEmailOnPurchaseOrderCreated extends AbstractEventListener { private $mailer; public function __construct($mailer) { $this->mailer = $mailer; } /** * {@inheritDoc} */ public function getSupportedEventClassName() { return 'PurchaseOrderCreated'; // Should be the fully qualified class name of the event } /** * {@inheritDoc} */ public function handle(EventInterface $event) { $this->mailer->send('to@you.com', 'Purchase order created for customer #' . $event->getCustomer()->getId()); } }
For your project to know that an EventListener is bound to the Event, you should use the EventBus:
// first the locator $locator = new \PhpDDD\Domain\Event\Listener\Locator\EventListenerLocator(); $locator->register('PurchaseOrderCreated', new SendEmailOnPurchaseOrderCreated(/* $mailer */)); // then the EventBus $bus = new \PhpDDD\Domain\Event\Bus\EventBus($locator); // do what you need to do on your Domain $aggregateRoot = new PurchaseOrder(new Customer(1)); // Then apply EventListeners $events = $aggregateRoot->pullEvents(); // this will clear the list of event in your AggregateRoot so an Event is trigger only once // You can have more than one event at a time. foreach($events as $event) { $bus->publish($event); }
php-ddd/domain-event 适用场景与选型建议
php-ddd/domain-event 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.57k 次下载、GitHub Stars 达 43, 最近一次更新时间为 2014 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 php-ddd/domain-event 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-ddd/domain-event 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 43
- 点击次数: 16
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-30