gpslab/cqrs
Composer 安装命令:
composer require gpslab/cqrs
包简介
Infrastructure for creating CQRS applications
README 文档
README
CQRS
Infrastructure for creating CQRS applications.
Installation
Pretty simple with Composer, run:
composer require gpslab/cqrs
Command
- Simple usage
- Bus
- Handler
- Create handler
- Locator and Subscribers
- Queue
- Middleware
- Payload
Simple usage commands
Commands, in the CQRS approach, are designed to change the data in the application.
For example, consider the procedure for renaming an article.
Create a command to rename:
use GpsLab\Component\Command\Command; class RenameArticleCommand implements Command { public $article_id; public $new_name = ''; }
Note
To simplify the filling of the command, you can use payload.
You can use any implementations of callable type as a command handler. We recommend using public methods of classes as handlers. For example we use Doctrine ORM.
use GpsLab\Component\Command\Command; use Doctrine\ORM\EntityManagerInterface; class RenameArticleHandler { private $em; public function __construct(EntityManagerInterface $em) { $this->em = $em; } public function handleRenameArticle(RenameArticleCommand $command): void { // get article by id $article = $this->em->getRepository(Article::class)->find($command->article_id); $article->rename($command->new_name); } }
And now we register handler and handle command.
use GpsLab\Component\Command\Bus\HandlerLocatedCommandBus; use GpsLab\Component\Command\Handler\Locator\DirectBindingCommandHandlerLocator; // register command handler in handler locator $handler = new RenameArticleHandler($em); $locator = new DirectBindingCommandHandlerLocator(); $locator->registerHandler(RenameArticleCommand::class, [$handler, 'handleRenameArticle']); // create bus with command handler locator $bus = new HandlerLocatedCommandBus($locator); // ... // create rename article command $command = new RenameArticleCommand(); $command->article_id = $article_id; $command->new_name = $new_name; // handle command $bus->handle($command);
For the asynchronous handle a command you can use CommandQueue.
Note
To monitor the execution of commands, you can use middleware.
Query
- Simple usage
- Bus
- Handler
- Create handler
- Locator and Subscribers
- Middleware
- Payload
- Doctrine specification query
Simple usage queries
Query, in the CQRS approach, are designed to get the data in the application.
For example, consider the procedure for get an article by identity.
Create a query:
use GpsLab\Component\Query\Query; class ArticleByIdentityQuery implements Query { public $article_id; }
Note
To simplify the filling of the query, you can use payload.
You can use any implementations of callable type as a query handler. We recommend using public methods of classes as handlers. For example we use Doctrine ORM.
use GpsLab\Component\Query\Query; use Doctrine\ORM\EntityManagerInterface; class ArticleByIdentityHandler { private $em; public function __construct(EntityManagerInterface $em) { $this->em = $em; } public function handleArticleByIdentity(ArticleByIdentityQuery $query) { // get article by id return $this->em->getRepository(Article::class)->find($query->article_id); } }
And now we register handler and handle query.
use GpsLab\Component\Query\Bus\HandlerLocatedQueryBus; use GpsLab\Component\Query\Handler\Locator\DirectBindingQueryHandlerLocator; // register query handler in handler locator $handler = new ArticleByIdentityHandler($em); $locator = new DirectBindingQueryHandlerLocator(); $locator->registerHandler(ArticleByIdentityQuery::class, [$handler, 'handleArticleByIdentity']); // create bus with query handler locator $bus = new HandlerLocatedQueryBus($locator); // ... // create find article query $query = new ArticleByIdentityQuery(); $query->article_id = $article_id; // handle query $article = $bus->handle($query);
Note
To monitor the execution of commands, you can use middleware.
License
This bundle is under the MIT license. See the complete license in the file: LICENSE
gpslab/cqrs 适用场景与选型建议
gpslab/cqrs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 585 次下载、GitHub Stars 达 28, 最近一次更新时间为 2017 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「predis」 「psr-3」 「cqrs」 「infrastructure」 「PSR-11」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gpslab/cqrs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gpslab/cqrs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gpslab/cqrs 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Asynchronous version of Predis
The bundle for easy using json-rpc api on your project
The CodeIgniter Redis package
PSR-3 compatible logger with dynamic file naming and hourly rotation, powered by Monolog
Chronolog is a tool that allows you to send logs to files, sockets, mailboxes, databases, etc.
Wide logging implementation for PHP - one comprehensive, context-rich event per request
统计信息
- 总下载量: 585
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 12
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-24
