登录
首页 >  文章 >  php教程

使用 Symfony Console 进行交互式调试

来源:dev.to

时间:2024-07-20 22:18:51 229浏览 收藏

在文章实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《使用 Symfony Console 进行交互式调试》,聊聊,希望可以帮助到正在努力赚钱的你。

使用 Symfony Console 进行交互式调试

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');
    }
}

今天就这样!

今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>