gpslab / cqrs
CQRS 应用程序的创建基础设施
v2.0.0
2020-01-22 08:45 UTC
Requires
- php: >=7.1.0
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpstan/phpstan-phpunit: ^0.12
- phpunit/phpunit: ^7.0|^8.2
- predis/predis: ~1.0|~1.1
- psr/container: ~1.0
- psr/log: ~1.0
- superbalist/php-pubsub-redis: 2.0.*
- symfony/dependency-injection: ~2.3|~3.0|~4.0|~5.0
- symfony/serializer: ~2.3|~3.0|~4.0|~5.0
README
CQRS
创建 CQRS 应用程序的基础设施。
安装
使用 Composer 非常简单,运行
composer require gpslab/cqrs
命令
- 简单用法
- 总线
- 处理器
- 创建处理器
- 定位器和订阅者
- 直接绑定定位器
- PSR-11 容器定位器 (PSR-11)
- Symfony 容器定位器 (Symfony 3.3 实现了 一个 PSR-11)
- 队列
- 中间件
- 负载
简单用法命令
在 CQRS 方法中,命令被设计为更改应用程序中的数据。
例如,考虑重命名文章的流程。
创建重命名命令
use GpsLab\Component\Command\Command; class RenameArticleCommand implements Command { public $article_id; public $new_name = ''; }
注意
为了简化命令的填充,可以使用 payload。
可以使用任何 callable 类型 的实现作为命令处理器。我们建议使用类的公共方法作为处理器。例如,我们使用 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); } }
现在我们注册处理器并处理命令。
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);
对于异步处理命令,您可以使用 CommandQueue
。
注意
要监控命令的执行,您可以使用 中间件。
查询
- 简单用法
- 总线
- 处理器
- 创建处理器
- 定位器和订阅者
- 直接绑定定位器
- PSR-11 容器定位器 (PSR-11)
- Symfony 容器定位器 (Symfony 3.3 实现了 一个 PSR-11)
- 中间件
- 负载
- Doctrine 规范查询
简单用法查询
在 CQRS 方法中,查询被设计为获取应用程序中的数据。
例如,考虑通过身份获取文章的流程。
创建查询
use GpsLab\Component\Query\Query; class ArticleByIdentityQuery implements Query { public $article_id; }
注意
为了简化查询的填充,可以使用 payload。
您可以使用任何 可调用类型 的实现作为查询处理器。我们建议使用类的公共方法作为处理器。例如,我们使用 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); } }
现在,我们注册处理器并处理查询。
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);
注意
要监控命令的执行,您可以使用 中间件。
许可协议
此扩展包遵循 MIT 许可协议。完整的许可协议请参阅文件:LICENSE