brammm/tactishun
Composer 安装命令:
composer require brammm/tactishun
包简介
A simple PHP 8 command bus implementation
关键字:
README 文档
README
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:
- Ross Tuck's work on league/tactician
- The Slim Framework middleware dispatcher pattern
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 brammm/tactishun 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
Easy command bus
rezzza/command-bus integration to Symfony
Shell command module for PHP.
Library with classes to help you do batch.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2025-07-22