lmc/cqrs-bundle

CQRS库及其查询和命令扩展的symfony包

安装量: 9,174

依赖者: 0

建议者: 1

安全性: 0

星星: 1

关注者: 14

分支: 0

开放问题: 0

类型:symfony-bundle

2.1.0 2024-03-06 13:34 UTC

This package is auto-updated.

Last update: 2024-09-06 14:43:14 UTC


README

cqrs-types Latest Stable Version Tests and linting Coverage Status

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中定义profilerdebug,以仅允许在dev Symfony环境中使用

注意:如果您未启用任何扩展,则仅存在CallbackQueryHandlerCallbackSendCommandHandler,因此您可能需要注册自己的处理器。

路由

如果您启用分析器,则必须为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

Http扩展仓库

安装

composer require lmc/cqrs-http

注意:为了使Http扩展正常工作,您还需要实现PSR-7PSR-17PSR-18

配置

lmc_cqrs:
    extension:
        http: true

启用Http扩展将允许QueryFetcherCommandSender处理PSR-7请求/响应并对其进行解码。

Solr

SOLR扩展仓库

安装

composer require lmc/cqrs-solr

配置

lmc_cqrs:
    extension:
        solr: true

启用SOLR扩展将允许QueryFetcherCommandSender处理Solarium请求/结果并对其进行解码。

Solarium查询构建器

它允许您通过定义一个具有所有您想要提供的功能的实体来构建Solarium请求。有关更多信息,请参阅SOLR扩展readme

注意:您可以通过lmc_cqrs.solr.query_builder_applicator指定您自定义应用器的标签

所有预定义服务和它们的优先级列表

注意:要查看在您的应用程序中实际注册的所有服务的列表,请使用bin/console debug:cqrs(您的配置中需要设置debug: true

命令和查询的最高处理器

查询处理器

发送命令处理器

响应解码器

分析器格式化程序

其他服务