承接 complex-heart/domain-events 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

complex-heart/domain-events

最新稳定版本:v1.0.0

Composer 安装命令:

composer require complex-heart/domain-events

包简介

Domain event toolset for building rich domain events.

README 文档

README

Tests Quality Gate Status Coverage

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:

  • IsDomainEvent Implements the Event contract with auto-generated eventId, eventName, payload, and occurredOn.

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 (OrderPlacedorder.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.

统计信息

  • 总下载量: 429
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固