定制 brammm/tactishun 二次开发

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

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

brammm/tactishun

Composer 安装命令:

composer require brammm/tactishun

包简介

A simple PHP 8 command bus implementation

关键字:

README 文档

README

PHP Version Latest Version on Packagist Software License Build status

A lightweight command bus implementation for PHP 8.3+ projects, designed to provide a straightforward approach to hexagonal architecture and CQRS patterns.

Features

  • Simple command bus pattern implementation
  • PSR-11 container integration
  • Extensible middleware system
  • Zero configuration required to get started
  • Full PHPStan coverage

Installation

Install via Composer:

composer require brammm/tactishun

Quickstart

use Brammm\Tactishun\CommandBus;
use Brammm\Tactishun\CommandHandler\CommandHandler;
use Brammm\Tactishun\HandledBy;

// 1. Create your command
#[HandledBy(SendWelcomeMailHandler::class)]
final readonly class SendWelcomeMail
{
    public function __construct(
        public UserId $userId,
    ) {}
}

// 2. Implement the handler
/** @implements CommandHandler<SendWelcomeMail> */
final class SendWelcomeMailHandler implements CommandHandler
{
    public function handle(object $command): void
    {
        // Fetch user from $command->userId and send welcome email
    }
}

// 3. Set up the command bus
$container = new Container(); // Any PSR-11 compatible container
$commandBus = new CommandBus($container);

// 4. Dispatch commands
$commandBus->handle(new SendWelcomeMail($user->id));

Usage

Handling multiple commands with a single command handler

It's possible to have multiple commands handled by a single command handler. Simply add the HandledBy attribute with the same handler to multiple command classes.

For convenience, you can extend from the MultipleCommandsHandler (instead of using the CommandHandler interface). It forwards the command to separate handleCommandName methods.

use Brammm\Tactishun\CommandHandler\MultipleCommandsHandler;
use Brammm\Tactishun\HandledBy;

#[HandledBy(UserCommandHandler::class)]
final readonly class RegisterUser
{
}

#[HandledBy(UserCommandHandler::class)]
final readonly class DeactivateUser
{
}

/** @extends MultipleCommandsHandler<RegisterUser|DeactivateUser> */
final class UserCommandHandler extends MultipleCommandsHandler
{
    public function handleRegisterUser(RegisterUser $command): void
    {
    }

    public function handleDeactivateUser(DeactivateUser $command): void
    {
    }
}

Extending functionality through middleware

Extend functionality with middleware that wraps command execution:

final readonly class LoggingMiddleware implements Middleware
{
    public function __construct(
        private LoggerInterface $logger
    ) {}

    public function process(object $command, CommandHandler $commandHandler): void
    {
        $commandName = get_class($command);
        $this->logger->info("Executing command: {$commandName}");

        $startTime = microtime(true);
        $commandHandler->handle($command);
        $executionTime = microtime(true) - $startTime;

        $this->logger->info("Command {$commandName} completed in {$executionTime}ms");
    }
}

// Register middleware
$commandBus->add(new LoggingMiddleware($logger));

Middleware are added First In, First Out.

Through Middleware, it is possible to provide async queued functionality. No Middleware to facilitate this are shipped with this package at the moment.

Custom CommandHandlerResolvers

By default, Tactishun uses the AttributeCommandHandlerResolver to find handlers via the HandledBy attribute. This allows the zero-configuration setup. If you'd rather not use that attribute or have a different solution in mind, you can provide a custom resolver as the second parameter:

class ClassMapResolver implements CommandHandlerResolver
{
    private array $handlerMap = [
        SendWelcomeMail::class => SendWelcomeMailHandler::class,
        ProcessPayment::class => ProcessPaymentHandler::class,
    ];

    public function resolve(object $command): string
    {
        $commandClass = get_class($command);
        return $this->handlerMap[$commandClass] ?? throw new HandlerNotFound($commandClass);
    }
}

$commandBus = new CommandBus($container, new ClassMapResolver());

Note that command handler resolvers must return a class-string for a command handler that the container implementation can then resolve to an instance of that handler.

Inspiration

This library draws inspiration from:

brammm/tactishun 适用场景与选型建议

brammm/tactishun 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 brammm/tactishun 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2025-07-22