complex-heart/domain-events
Composer 安装命令:
composer require complex-heart/domain-events
包简介
Domain event toolset for building rich domain events.
README 文档
README
Building Rich Domain Events
Complex Heart Domain Events provides a trait-based implementation of the Event contract from
php-contracts. It auto-generates event metadata so you can focus on
defining what happened in your domain without boilerplate.
The available trait:
IsDomainEventImplements theEventcontract with auto-generatedeventId,eventName,payload, andoccurredOn.
Key Features
- Auto-Generated Event ID: Each event instance gets a unique UUID v4 identifier
- Convention-Based Event Name: Derives a dotted event name from the class name (
OrderPlaced→order.placed) - Automatic Payload: Builds the payload from public properties via reflection
- ISO-8601 Timestamps: Records when the event occurred
- Factory Method: Uses
new()/make()static factories consistent with the ComplexHeart ecosystem - Framework Agnostic: Pure PHP, no framework dependencies — works with Laravel, Symfony, or standalone
- PHPStan Level 8: Complete static analysis support
Installation
composer require complex-heart/domain-events
Usage
Define your domain events as simple final classes with public readonly properties:
use ComplexHeart\Domain\Contracts\Events\Event; use ComplexHeart\Domain\Events\IsDomainEvent; final class OrderPlaced implements Event { use IsDomainEvent; public function __construct( public readonly string $orderId, public readonly string $customerId, public readonly float $total, ) { } }
Create events using the static factory:
$event = OrderPlaced::new( orderId: 'order-123', customerId: 'customer-456', total: 99.95, ); $event->eventId(); // "a1b2c3d4-e5f6-..." (auto-generated UUID) $event->eventName(); // "order.placed" $event->payload(); // ['orderId' => 'order-123', 'customerId' => 'customer-456', 'total' => 99.95] $event->occurredOn(); // "2026-03-15T10:30:00+00:00" // Direct property access also works $event->orderId; // "order-123"
Integration with Aggregates
Domain events are designed to work with aggregates that use the HasDomainEvents trait from
php-domain-model:
use ComplexHeart\Domain\Contracts\Model\Aggregate; use ComplexHeart\Domain\Model\IsAggregate; class Order extends Model implements Aggregate { use IsAggregate; public static function place(string $id, string $customerId, float $total): static { $order = static::new(id: $id, customerId: $customerId, total: $total); $order->registerDomainEvent( OrderPlaced::new(orderId: $id, customerId: $customerId, total: $total) ); return $order; } }
Then publish events through an EventBus implementation in your use case:
final readonly class PlaceOrder { public function __construct( private OrderRepository $orders, private EventBus $eventBus, ) { } public function __invoke(string $id, string $customerId, float $total): Order { $order = Order::place($id, $customerId, $total); $this->orders->store($order); $order->publishDomainEvents($this->eventBus); return $order; } }
For more information and usage examples, please check the wiki.
complex-heart/domain-events 适用场景与选型建议
complex-heart/domain-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 469 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 complex-heart/domain-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 complex-heart/domain-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 469
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15