lmc/ cqrs-handler
包含查询和命令处理程序的库
Requires
- php: ^8.2
- ext-json: *
- ext-mbstring: *
- lmc/cqrs-types: ^3.2
- psr/cache: ^2.0 || ^3.0
- psr/http-message: ^1.0.1 || ^2.0
- ramsey/collection: ^1.2.2 || ^2.0
- ramsey/uuid: ^4.2.3
- symfony/stopwatch: ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.5
- lmc/coding-standard: ^3.3
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.4
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^11.0.4
- symfony/cache: ^5.2 || ^6.0 || ^7.0
README
此库包含CQRS/Types的基础实现。
目录
安装
composer require lmc/cqrs-handler
查询获取器
查询获取器接口的基础实现(参见Types/QueryFetcherInterface)。
它负责:
- 根据查询请求类型找到查询处理器
- 处理所有查询功能
- 缓存
- 需要一个
Psr\Cache\CacheItemPoolInterface
实例
- 需要一个
- 性能分析
- 需要一个
Lmc\Cqrs\Handler\ProfilerBag
实例
- 需要一个
- 缓存
- 解码查询处理器的响应
用法
如果你没有使用CQRS/Bundle,你需要自己设置查询获取器。
最小初始化
$queryFetcher = new QueryFetcher( // Cache false, // disabled cache null, // no cache pool -> no caching // Profiling null // no profiler bag -> no profiling );
具有所有功能的完整初始化。
$profilerBag = new ProfilerBag(); $queryFetcher = new QueryFetcher( // Cache true, // is cache enabled $cache, // instance of Psr\Cache\CacheItemPoolInterface // Profiling $profilerBag, // collection of profiled information // Custom handlers // NOTE: there is multiple ways of defining handler [ [new TopMostHandler(), PrioritizedItem::PRIORITY_HIGHEST], // Array definition of priority new OtherHandler(), // Used with default priority of 50 new PrioritizedItem(new FallbackHandler(), PrioritizedItem::PRIORITY_LOWEST) // PrioritizedItem value object definition ], // Custom response decoders // NOTE: there is multiple ways of defining response decoders [ [new TopMostDecoder(), PrioritizedItem::PRIORITY_HIGHEST], // Array definition of priority new OtherDecoder(), // Used with default priority of 50 new PrioritizedItem(new FallbackDecoder(), PrioritizedItem::PRIORITY_LOWEST) // PrioritizedItem value object definition ] );
您可以通过add
方法添加处理器和解码器。
$this->queryFetcher->addHandler(new MyQueryHandler(), PrioritizedItem::PRIORITY_MEDIUM); $this->queryFetcher->addDecoder(new MyQueryResponseDecoder(), PrioritizedItem::PRIORITY_HIGH);
获取查询
您可以对响应做任何想做的事情,我们将结果持久化到数据库中,例如记录错误。
// 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()); }
查询处理器
它负责处理特定的查询请求,并将结果传递到OnSuccess
回调中。更多信息。
GetCachedHandler
此处理器在将CacheItemPoolInterface
实例传递给QueryFetcher
时自动创建,并以优先级80
添加到处理器中。
它支持实现CacheableInterface
的查询,其中cacheTime > 0
。第二个条件允许您通过仅缓存时间值来避免具有CacheableInterface
的查询的缓存。还有一个名为CacheTime::noCache()
的命名构造函数来明确表示。
它通过从缓存中检索结果(如果缓存中有项目且命中(参见PSR-6以获取更多信息)来处理查询。
CallbackQueryHandler
此处理器支持请求类型为"callable"
、"Closure"
或"callback"
(均表示callable
请求)的查询。
它简单地调用创建的请求作为一个函数,并将结果返回到OnSuccess
回调。
查询
查询是一个获取数据而不更改任何内容的请求。更多信息。
CachedDataQuery
这是为具有CacheableInterface
的查询提供的预定义实现。
它对于您想要使用缓存来存储结果的内部应用程序查询很有用。您也可以扩展它并添加更多功能。
$query = new CallbackQueryHandler( fn () => $this->repository->fetchData(), new CacheKey('my-data-key'), CacheTime::oneHour() );
ProfiledCachedDataQuery
这是为具有CacheableInterface
和ProfileableInterface
的查询提供的预定义实现。
它对于您想要使用缓存来存储结果并对其进行性能分析的内部应用程序查询很有用。您也可以扩展它并添加更多功能。
$query = new ProfiledCallbackQueryHandler( fn () => $this->repository->fetchData(), new CacheKey('my-data-key'), CacheTime::oneHour(), 'my-profiler-key', ['additional' => 'data'] // optional );
命令发送器
命令发送器接口的基础实现(参见Types/CommandSenderInterface)。
它负责:
- 根据命令请求类型找到发送命令处理器
- 处理所有命令功能
- 性能分析
- 需要一个
Lmc\Cqrs\Handler\ProfilerBag
实例
- 需要一个
- 性能分析
- 解码发送命令处理器的响应
用法
如果您没有使用CQRS/Bundle,您需要自己设置命令发送器。
最小初始化
$commandSender = new CommandSender( // Profiling null // no profiler bag -> no profiling );
具有所有功能的完整初始化。
$profilerBag = new ProfilerBag(); $commandSender = new CommandSender( // Profiling $profilerBag, // collection of profiled information // Custom handlers // NOTE: there is multiple ways of defining handler [ [new TopMostHandler(), PrioritizedItem::PRIORITY_HIGHEST], // Array definition of priority new OtherHandler(), // Used with default priority of 50 new PrioritizedItem(new FallbackHandler(), PrioritizedItem::PRIORITY_LOWEST) // PrioritizedItem value object definition ], // Custom response decoders // NOTE: there is multiple ways of defining response decoders [ [new TopMostDecoder(), PrioritizedItem::PRIORITY_HIGHEST], // Array definition of priority new OtherDecoder(), // Used with default priority of 50 new PrioritizedItem(new FallbackDecoder(), PrioritizedItem::PRIORITY_LOWEST) // PrioritizedItem value object definition ] );
您可以通过add
方法添加处理器和解码器。
$this->commandSender->addHandler(new MyCommandHandler(), PrioritizedItem::PRIORITY_MEDIUM); $this->commandSender->addDecoder(new MyCommandResponseDecoder(), PrioritizedItem::PRIORITY_HIGH);
发送命令
您可以对响应做任何想做的事情,我们将结果持久化到数据库中,例如记录错误。
// 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()); }
发送命令处理器
它负责处理特定的命令请求,并将结果传递给OnSuccess
回调。更多详情请查看此处。
CallbackSendCommandHandler
此处理程序支持请求类型为"callable"
、"Closure"
或"callback"
(它们都代表callable
请求)的命令。
它简单地调用创建的请求作为一个函数,并将结果返回到OnSuccess
回调。
命令
命令是更改数据并可能返回结果数据的请求。更多详情请查看此处
ProfiledDataCommand
这是为具有ProfileableInterface
的命令提供的预定义实现。
它非常适合需要对其进行分析的应用内命令。您还可以扩展它并添加更多功能。
$command = new ProfiledDataCommand( fn () => $this->repository->fetchData(), new CacheKey('my-data-key'), CacheTime::oneHour(), 'my-profiler-key', ['additional' => 'data'] // optional );
ProfilerBag
服务,它收集当前请求中的所有分析信息。如果您将其传递给QueryFetcher
或CommandSender
,它们将分析实现ProfileableInterface
的查询/命令并将其添加到ProfilerBag
中。
这些信息由CqrsDataCollector
使用,它在Symfony分析器(在CQRS/Bundle中使用)中显示。
详细程度
分析包还可以包含有关分析详细程度的信息。
级别
- NORMAL =
空值
(默认值) - VERBOSE =
'verbose'
- DEBUG =
'debug'
在高详细程度级别下,可能还会向ProfilerItem
添加其他数据。
您可以通过以下方式设置它:
$profilerBag->setVerbosity(ProfilerBag::VERBOSITY_VERBOSE);