承接 php-architecture-kit/domain-core 相关项目开发

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

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

php-architecture-kit/domain-core

Composer 安装命令:

composer require php-architecture-kit/domain-core

包简介

Domain Core abstract classes, interfaces and categorized exceptions.

README 文档

README

Domain-Driven Design building blocks for PHP applications. Provides abstract classes, interfaces, and categorized domain exceptions mappable to HTTP status codes.

Features

  • AggregateRoot - Base class with domain event recording
  • DomainEvent - Marker interface for domain events
  • Categorized Exceptions - 7 domain exceptions mapped to HTTP codes (400, 402, 403, 409, 422, 424, 451)
  • Zero dependencies - Pure PHP, no external packages
  • PHP 7.4+ - Compatible with legacy and modern PHP

Installation

composer require php-architecture-kit/domain-core

Quick Start

Aggregate Root

use PhpArchitecture\DomainCore\AggregateRoot;
use PhpArchitecture\DomainCore\DomainEvent;

class OrderCreated implements DomainEvent
{
    public function __construct(
        public readonly string $orderId,
        public readonly array $items,
    ) {}
}

class Order extends AggregateRoot
{
    private string $id;
    private array $items;
    private string $status = 'draft';

    public static function create(string $id, array $items): self
    {
        $order = new self();
        $order->id = $id;
        $order->items = $items;
        $order->recordEvent(new OrderCreated($id, $items));

        return $order;
    }

    public function confirm(): void
    {
        if ($this->status !== 'draft') {
            throw new InvalidStateToPerformActionException('Order already confirmed');
        }
        
        $this->status = 'confirmed';
        $this->recordEvent(new OrderConfirmed($this->id));
    }
}

Using Domain Events

// Create aggregate and perform actions
$order = Order::create('order-123', ['item1', 'item2']);
$order->confirm();

// Get recorded events (without clearing)
$events = $order->getEvents(); // [OrderCreated, OrderConfirmed]

// Release events (get and clear)
$events = $order->releaseEvents(); // [OrderCreated, OrderConfirmed]
$events = $order->getEvents();     // []

Domain Exceptions

All exceptions extend \DomainException and can be mapped to HTTP status codes in your infrastructure layer.

Exception HTTP Code When to Use
InvalidInputException 400 Bad Request Input data violates business rules
PaymentStatusException 402 Payment Required Action requires specific payment status
InsufficientPrivilegeException 403 Forbidden User lacks required role/relationship
InvalidStateToPerformActionException 409 Conflict Aggregate state doesn't allow action
InvalidStateCausedException 422 Unprocessable Entity Data would cause invalid state
DependencyStateException 424 Failed Dependency Dependent aggregate unavailable/wrong state
LegalRestrictionException 451 Unavailable For Legal Reasons Legal restrictions prevent action

Exception Usage Examples

use PhpArchitecture\DomainCore\Exception\InvalidInputException;
use PhpArchitecture\DomainCore\Exception\InsufficientPrivilegeException;
use PhpArchitecture\DomainCore\Exception\InvalidStateToPerformActionException;

class Order extends AggregateRoot
{
    public function addItem(string $sku, int $quantity, string $actorId): void
    {
        // 400 - Invalid input
        if ($quantity <= 0) {
            throw new InvalidInputException('Quantity must be positive');
        }

        // 403 - Insufficient privilege
        if ($this->ownerId !== $actorId) {
            throw new InsufficientPrivilegeException('Only owner can modify order');
        }

        // 409 - Invalid state to perform action
        if ($this->status === 'shipped') {
            throw new InvalidStateToPerformActionException('Cannot modify shipped order');
        }

        $this->items[] = new OrderItem($sku, $quantity);
    }
}

HTTP Mapping (Infrastructure Layer)

// Symfony Exception Listener
use PhpArchitecture\DomainCore\Exception\InvalidInputException;
use PhpArchitecture\DomainCore\Exception\PaymentStatusException;
use PhpArchitecture\DomainCore\Exception\InsufficientPrivilegeException;
use PhpArchitecture\DomainCore\Exception\InvalidStateToPerformActionException;
use PhpArchitecture\DomainCore\Exception\InvalidStateCausedException;
use PhpArchitecture\DomainCore\Exception\DependencyStateException;
use PhpArchitecture\DomainCore\Exception\LegalRestrictionException;

class DomainExceptionListener
{
    private const HTTP_MAP = [
        InvalidInputException::class => 400,
        PaymentStatusException::class => 402,
        InsufficientPrivilegeException::class => 403,
        InvalidStateToPerformActionException::class => 409,
        InvalidStateCausedException::class => 422,
        DependencyStateException::class => 424,
        LegalRestrictionException::class => 451,
    ];

    public function onKernelException(ExceptionEvent $event): void
    {
        $exception = $event->getThrowable();
        
        foreach (self::HTTP_MAP as $class => $code) {
            if ($exception instanceof $class) {
                $event->setResponse(new JsonResponse(
                    ['error' => $exception->getMessage()],
                    $code
                ));
                return;
            }
        }
    }
}

API Reference

AggregateRoot

Method Visibility Description
getEvents(): DomainEvent[] public Returns recorded events without clearing
releaseEvents(): DomainEvent[] public Returns and clears recorded events
recordEvent(DomainEvent $event): void protected Records a domain event

DomainEvent

Marker interface - implement in your domain event classes:

interface DomainEvent {}

Exceptions

All exceptions extend \DomainException and accept standard parameters:

__construct(string $message = '', int $code = 0, ?Throwable $previous = null)

Testing

Package is tested with PHPUnit in the php-architecture-kit/workspace project.

License

MIT

php-architecture-kit/domain-core 适用场景与选型建议

php-architecture-kit/domain-core 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「ddd」 「aggregate」 「framework-agnostic」 「domain-core」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 php-architecture-kit/domain-core 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-architecture-kit/domain-core 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-architecture-kit/domain-core 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-12