spiral-packages/cqrs
Composer 安装命令:
composer require spiral-packages/cqrs
包简介
Lightweight message bus supporting CQRS for Spiral Framework
README 文档
README
It's a lightweight messaging facade. It allows you to define the API of your model with the help of messages.
- Command messages describe actions your model can handle.
- Query messages describe available information that can be fetched from your (read) model.
Requirements
Make sure that your server is configured with following PHP version and extensions:
- PHP 8.1+
- Spiral framework 3.0+
Installation
You can install the package via composer:
composer require spiral-packages/cqrs
After package install you need to register bootloader from the package.
protected const LOAD = [ // ... \Spiral\Cqrs\Bootloader\CqrsBootloader::class, ];
Note: if you are using
spiral-packages/discoverer, you don't need to register bootloader by yourself.
Usage
You can also register command and query handlers via attributes
Commands
Command definition
class StoreUser implements \Spiral\Cqrs\CommandInterface { public function __construct( public Uuid $uuid, public string $username, public string $password, public \DateTimeImmutable $registeredAt, ) { } }
Command handler definition
To register command handler you just need to add attribute on method that should be invoked.
class StoreUserHandler { public function __construct( private EntityManagerInterface $entityManager ) { } #[\Spiral\Cqrs\Attribute\CommandHandler] public function __invoke(StoreUser $command) { $this->entityManager->persist( new User( $command->uuid, $command->username, $command->password, $command->registeredAt ) ); $this->entityManager->run(); } }
Dispatch command
use Ramsey\Uuid\Uuid; class UserController { public function store(UserStoreRequest $request, \Spiral\Cqrs\CommandBusInterface $bus) { $bus->dispatch(new StoreUser( $uuid = Uuid::uuid4(), $request->getUsername(), $request->getPassword(), new \DateTimeImmutable() )); return $uuid; } }
Queries
Query definition
class FindAllUsers implements \Spiral\Cqrs\QueryInterface { public function __construct( public array $roles = [] ) { } }
class FindUserById implements \Spiral\Cqrs\QueryInterface { public function __construct( public Uuid $uuid ) { } }
Query handler definition
class UsersQueries { public function __construct( private UserRepository $users ) { } #[\Spiral\Cqrs\Attribute\QueryHandler] public function findAll(FindAllUsers $query): UserCollection { $scope = []; if ($query->roles !== []) { $scope['roles'] = $query->roles } return new UserCollection( $this->users->findAll($scope) ); } #[\Spiral\Cqrs\Attribute\QueryHandler] public function findById(FindUserById $query): UserResource { return new UserResource( $this->users->findByPK($query->uuid) ); } }
Dispatch queries
use Ramsey\Uuid\Uuid; class UserController { public function index(UserFilters $filters, \Spiral\Cqrs\QueryBusInterface $bus) { return $bus->ask( new FindAllUsers($filters->roles()) )->toArray(); } public function show(string $uuid, \Spiral\Cqrs\QueryBusInterface $bus) { return $bus->ask( new FindUserById(Uuid::fromString($uuid)) )->toArray(); } }
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
spiral-packages/cqrs 适用场景与选型建议
spiral-packages/cqrs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.53k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cqrs」 「command-bus」 「spiral」 「query-bus」 「spiral-packages」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spiral-packages/cqrs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spiral-packages/cqrs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spiral-packages/cqrs 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel integration for command bus.
Interfaces, Traits and Nominal Classes used by Domain Entities Implementing Domain Events
Swagger-php integration package for Spiral Framework.
Implementation of the transactional outbox pattern on top of rekalogika/domain-event
CliTube is data viewer in console
Bridge between CliTube and Spiral Framework
统计信息
- 总下载量: 19.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-13