定制 tetthys/domain-kernel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tetthys/domain-kernel

Composer 安装命令:

composer require tetthys/domain-kernel

包简介

Domain exception and error reason kernel for Tetthys ecosystem.

README 文档

README

This guide shows the absolute minimum needed to use:

  • FailureReason enum
  • ServiceFailureException
  • ServiceFailable + abortWith()
  • A simple service (ItemService)

No framework. No repository layer. No external dependencies.

1. Directory Layout

src/
  Exceptions/
    ItemFailureReason.php
    ItemFailure.php
  Service/
    ItemService.php
example.php

That’s all you need.

2. File: src/Exceptions/ItemFailureReason.php

<?php

declare(strict_types=1);

namespace App\Exceptions;

use Tetthys\DomainKernel\ServiceFailureReason;

/**
 * Failure reasons for ItemService.
 */
enum ItemFailureReason: string implements ServiceFailureReason
{
    case NEGATIVE_PRICE = 'negative_price';

    public function code(): string
    {
        return $this->value;
    }

    public function message(): string
    {
        return match ($this) {
            self::NEGATIVE_PRICE => 'Price cannot be negative.',
        };
    }
}

3. File: src/Exceptions/ItemFailure.php

<?php

declare(strict_types=1);

namespace App\Exceptions;

use Tetthys\DomainKernel\ServiceFailureException;

/**
 * Single failure type for ItemService.
 */
final class ItemFailure extends ServiceFailureException
{
}

4. File: src/Service/ItemService.php

<?php

declare(strict_types=1);

namespace App\Service;

use App\Exceptions\ItemFailure;
use App\Exceptions\ItemFailureReason;
use Tetthys\DomainKernel\ServiceFailable;
use Tetthys\DomainKernel.ServiceFailureException;
use Tetthys\DomainKernel.ServiceFailureReason;

/**
 * Minimal pure PHP service using abortWith().
 */
final class ItemService
{
    use ServiceFailable;

    /**
     * Required by ServiceFailable.
     */
    protected function newServiceFailure(
        ServiceFailureReason $reason,
        array $context = [],
    ): ServiceFailureException {
        /** @var ItemFailureReason $reason */
        return new ItemFailure($reason, $context);
    }

    public function calculatePrice(int $price): int
    {
        if ($price < 0) {
            $this->abortWith(
                ItemFailureReason::NEGATIVE_PRICE,
                ['given_price' => $price],
            );
        }

        return $price * 2; // some logic
    }
}

5. File: example.php

<?php

declare(strict_types=1);

use App\Service\ItemService;
use Tetthys\DomainKernel\ServiceFailureException;

require __DIR__ . '/vendor/autoload.php';

$service = new ItemService();

try {
    // This will fail because the price is negative.
    $value = $service->calculatePrice(-10);
    echo "Result: {$value}\n";
} catch (ServiceFailureException $e) {
    echo "Failure occurred!\n";
    echo "Reason:  {$e->reason->code()}\n";
    echo "Message: {$e->reason->message()}\n";
    echo "Context: " . json_encode($e->context(), JSON_PRETTY_PRINT) . "\n";
}

6. Running the Example

php example.php

Output:

Failure occurred!
Reason:  negative_price
Message: Price cannot be negative.
Context: {
    "reason": "negative_price",
    "given_price": -10
}

7. What You Actually Need

Only four things:

✔ A failure reason enum

✔ A service-specific failure exception

✔ A service class using ServiceFailable + abortWith()

✔ A try/catch around your service call

This is the smallest possible real-world usage example of tetthys/domain-kernel and can be directly applied to any other domain (OrderService, AuthService, etc.).

tetthys/domain-kernel 适用场景与选型建议

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

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

围绕 tetthys/domain-kernel 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-12-11