RoundingWell 最重要的系统之一是评估引擎,或者我们通常所说的“评估”。该系统负责处理特定于客户的配置事件侦听器,这使我们能够为每个组织提供高度独特的用户体验。不用说,这个系统对我们的应用程序非常重要,并且能够准确地知道它对于给定事件做了什么,或将要做什么至关重要。
今天,我正在努力将我们的一位客户从一些旧事件过渡到较新的事件,并且需要确保其中一个步骤能够正确处理事件。我们有很多关于评估的日志记录,我知道我需要的信息就在日志中。但拥有大量日志记录的问题是,有很多日志。另外,我只需要运行每个触发器的第一部分来验证迁移是否有效。
我心想,“如果我们能为评估引擎提供一个类似 Xdebug 的单步调试器,这不是很聪明吗?我知道 Symfony Console 拥有我需要的所有功能......”
而这正是我所做的。因为我们的应用程序是完全依赖注入的,所以我能够创建一个新类来包装 StepFactory,它负责读取配置并创建构成评估的“步骤”。
use RoundingWell\Framework\DebugVar; use RuntimeException; use Symfony\Component\Console\Style\SymfonyStyle; readonly class StepFactoryWithConsoleConfirmation implements StepFactory { public function __construct( private StepFactory $stepFactory, private SymfonyStyle $style, private bool $confirmCreatedStep = true, private bool $showCreatedStep = true, private bool $showStepDefinition = false, ) { } public function create(object $subject, StepDefinition $definition): Step { if ($this->showStepDefinition) { $debug = new DebugVar($definition->parameters); $this->style->info( message: <<<TEXT Next step is $definition->name with parameters: $debug TEXT, ); } $step = $this->stepFactory->create($subject, $definition); if ($this->showCreatedStep) { $debug = new DebugVar($step); $this->style->info( message: <<<TEXT Step $definition->name created as: $debug TEXT, ); } if ($this->confirmCreatedStep && ! $this->style->confirm(question: "Continue with evaluation?")) { throw new RuntimeException( message: "Evaluation aborted at step {$definition->name}", ); } return $step; } }
并且,通过我的控制台命令中的一些容器操作,我们有一个交互式评估调试器:
use DI\Container; use RoundingWell\Common\Command\CommandBus; use RoundingWell\Common\Command\CommandBusComposed; use RoundingWell\Common\Command\Middleware\LoggingMiddleware; use RoundingWell\Evaluation\Command\EvaluateEvent; use RoundingWell\Evaluation\Command\EvaluateEventHandler; use RoundingWell\Evaluation\Step\StepFactory; use RoundingWell\Evaluation\Step\StepFactoryWithConsoleConfirmation; use Symfony\Component\Console\Style\SymfonyStyle; readonly class EvaluationDebug { public function __construct( private Container $container, ) { } public function __invoke( SymfonyStyle $symfonyStyle, string $eventType, string $eventId, string|null $evaluationId = null, ): void { // The command bus MUST ONLY log executed commands. $commandBus = new CommandBusComposed( $this->container->get(LoggingMiddleware::class), ); // The step factory MUST be wrapped to step through the evaluation. $stepFactory = new StepFactoryWithConsoleConfirmation( stepFactory: $this->container->get(StepFactory::class), style: $symfonyStyle, ); $this->container->set(CommandBus::class, $commandBus); $this->container->set(StepFactory::class, $stepFactory); $command = new EvaluateEvent( eventClass: $eventType, eventId: $eventId, evaluationId: $evaluationId, ); $this->container->get(EvaluateEventHandler::class)->handle($command); $symfonyStyle->success('Evaluation complete'); } }
今天就这样!
以上是使用 Symfony Console 进行交互式调试的详细内容。更多信息请关注PHP中文网其他相关文章!