lmc / cqrs-bundle
CQRS库及其查询和命令扩展的symfony包
Requires
- php: ^8.2
- ext-json: *
- ext-mbstring: *
- lmc/cqrs-handler: ^2.2
- lmc/cqrs-types: ^3.2
- symfony/config: ^5.2 || ^6.0 || ^7.0
- symfony/console: ^5.2 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.2 || ^6.0 || ^7.0
- symfony/error-handler: ^5.2 || ^6.0 || ^7.0
- symfony/framework-bundle: ^5.2 || ^6.0 || ^7.0
- symfony/http-foundation: ^5.2 || ^6.0 || ^7.0
- symfony/http-kernel: ^5.2 || ^6.0 || ^7.0
- twig/twig: ^2.0 || ^3.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.5
- lmc/coding-standard: ^3.3
- lmc/cqrs-http: ^3.1
- lmc/cqrs-solr: ^3.1
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.6
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^11.0.4
- symfony/yaml: ^5.2 || ^6.0 || ^7.0
Suggests
- lmc/cqrs-http: Provides http handler and base types for queries and commands.
- lmc/cqrs-solr: Provides solr handler and base types for queries and commands.
README
CQRS库和扩展的symfony包。它通过配置注册服务、数据收集器等。
目录
安装
composer require lmc/cqrs-bundle
配置
lmc_cqrs: profiler: false # Whether to enable profiler and allow profiling queries and commands [default false] debug: false # Whether to enable debug the CQRS by a console command [default false] cache: enabled: false # Whether to use cache for Queries [default false (true, if you define cache_provider)] cache_provider: '@my.cache.provider' # Service implementing a CacheItemPoolInterface. Required when cache is enabled [default null] extension: http: false # Whether should http extension be active (requires a lmc/cqrs-http dependency) [default false] solr: false # Whether should solr extension be active (requires a lmc/cqrs-solr dependency) [default false]
分析器扩展配置
lmc_cqrs: profiler: enabled: false # Whether to enable profiler and allow profiling queries and commands [default false] verbosity: '' # Verbosity level (verbose or debug) for a profiler bag - empty string is a default for normal
提示:
- 建议将
profiler: '%kernel.debug%'
设置为配置,以便仅在真正使用时进行分析(并注册所有服务以进行分析) - 您可以在
dev/lmc_cqrs.yaml
中定义profiler
和debug
,以仅允许在dev
Symfony环境中使用
注意:如果您未启用任何扩展,则仅存在CallbackQueryHandler
和CallbackSendCommandHandler
,因此您可能需要注册自己的处理器。
路由
如果您启用分析器,则必须为CQRS包注册路由。
# config/routes.yaml lmc_cqrs_bundle_routes: resource: "@LmcCqrsBundle/Resources/config/routes.yaml"
标签
如果您的类实现了接口并将其注册到Symfony容器中作为服务,则标签将自动注册
lmc_cqrs.query_handler
(QueryHandlerInterface
)lmc_cqrs.send_command_handler
(SendCommandHandlerInterface
)lmc_cqrs.profiler_formatter
(ProfilerFormatterInterface
)lmc_cqrs.response_decoder
(ResponseDecoderInterface
)
具有优先级
services: My\CustomQueryHandler: tags: - { name: 'lmc_cqrs.query_handler', priority: 80 }
注意:默认优先级为50
,预定义的处理程序、分析器等没有优先级高于90
(请参阅下文中的完整列表)
服务
根据配置注册所有必要的服务(例如,如果您设置了http: true
,它将自动注册所有HTTP处理器等)
大多数服务都通过别名和类名进行注册,因此它将可用于自动装配。所有接口都自动配置为具有标签(请参阅上面的标签部分)。
处理器
有两个主要服务,对于库至关重要。它们各自都有接口来表示,建议通过接口使用它们。
1. 查询检索接口
- 实现
Lmc\Cqrs\Handler\QueryFetcher
- 别名:
@lmc_cqrs.query_fetcher
- 它将为您的查询找到一个处理器,处理它,解码响应并将结果缓存(如果启用了缓存)
- 提供功能
- 缓存
- 需要
- 缓存提供者(在配置中设置) - 服务实现
Psr\Cache\CacheItemPoolInterface
- 查询实现
Lmc\Cqrs\Types\Feature\CacheableInterface
- 缓存提供者(在配置中设置) - 服务实现
- 它允许缓存解码的结果,并从缓存中再次加载它
- 需要
- 分析
- 需要
- 启用的分析器(在配置中)
- 查询实现
Lmc\Cqrs\Types\Feature\ProfileableInterface
- 它分析查询、其执行时间、响应、应用的处理程序和解码器,并在Symfony分析器中显示信息
- 需要
- 缓存
检索查询
您可以使用响应做任何想做的事情,我们将持久化结果到数据库,例如或记录错误。
// with continuation $this->queryFetcher->fetch( $query, fn ($response) => $this->repository->save($response), fn (\Throwable $error) => $this->logger->critical($error->getMassage()) ); // with return try { $response = $this->queryFetcher->fetchAndReturn($query); $this->repository->save($response); } catch (\Throwable $error) { $this->logger->critical($error->getMessage()); }
2. 命令发送接口
- 实现
Lmc\Cqrs\Handler\CommandSender
- 别名:
@lmc_cqrs.command_sender
- 它将为您的命令找到一个处理器,处理它,并解码响应
- 提供功能
- 分析
- 需要
- 启用的分析器(在配置中)
- 命令实现
Lmc\Cqrs\Types\Feature\ProfileableInterface
- 它分析了一个命令,其执行时间,响应,应用处理器和解码器,并在Symfony分析器中显示信息
- 需要
- 分析
发送一个命令
您可以使用响应做任何想做的事情,我们将持久化结果到数据库,例如或记录错误。
// with continuation $this->commandSender->send( $command, fn ($response) => $this->repository->save($response), fn (\Throwable $error) => $this->logger->critical($error->getMassage()) ); // with return try { $response = $this->commandSender->sendAndReturn($query); $this->repository->save($response); } catch (\Throwable $error) { $this->logger->critical($error->getMessage()); }
注意:CQRS库中没有日志功能,如果您需要,您必须自己实现。
分析器包
存在一个分析器包
服务,它收集了当前请求中所有的分析器信息。其中的信息由CqrsDataCollector
使用,它在Symfony分析器中显示这些信息。
需要在配置中设置profiler: true
。
您可以通过以下方式访问分析器包:
@lmc_cqrs.profiler_bag
(别名)Lmc\Cqrs\Handler\ProfilerBag
(自动装配)- 或通过程序访问
CqrsDataCollector
(请参阅此处)
扩展
我们提供了一些常见的命令和查询的基本扩展
Http
安装
composer require lmc/cqrs-http
注意:为了使Http扩展正常工作,您还需要实现PSR-7、PSR-17和PSR-18。
配置
lmc_cqrs: extension: http: true
启用Http扩展将允许QueryFetcher
和CommandSender
处理PSR-7请求/响应并对其进行解码。
Solr
安装
composer require lmc/cqrs-solr
配置
lmc_cqrs: extension: solr: true
启用SOLR扩展将允许QueryFetcher
和CommandSender
处理Solarium请求/结果并对其进行解码。
Solarium查询构建器
它允许您通过定义一个具有所有您想要提供的功能的实体来构建Solarium请求。有关更多信息,请参阅SOLR扩展readme。
注意:您可以通过lmc_cqrs.solr.query_builder_applicator
指定您自定义应用器的标签
所有预定义服务和它们的优先级列表
注意:要查看在您的应用程序中实际注册的所有服务的列表,请使用bin/console debug:cqrs
(您的配置中需要设置debug: true
)