elaberino/symfony-style-verbose

向 SymfonyStyle 添加方法,仅在设置 verbosity 标志时创建输出

1.2.1 2022-11-04 21:40 UTC

This package is auto-updated.

Last update: 2024-09-05 01:33:55 UTC


README

该类为 symfony 风格的每个输出方法生成新方法,仅基于定义的 verbositoy 级别创建输出,例如 title, titleIfVerbose(), titleIfVeryVerbose() 和 titleIfDebug()。

如果只需要在某个 verbosity 级别上输出,这个想法是为了减少复杂性。

示例

之前

$io = new SymfonyStyle($input, $output);

try {
    if ($output->isVerbose()) {
        $io->title('This is my title');
        $io->section('This is my section');
    }
    
    //do some stuff here
    
    if ($output->isVerbose()) {
        $io->section('Get objects');
        $io->progressStart(count($objects));
    }
    $objects = $this->repository->findBy(['active' => true]);
    
    foreach ($objects as $object) {
        //do something with the object
        if ($output->isVerbose()) {
            $io->progressAdvance();
        }
    }
    
    if ($output->isVerbose()) {
        $io->progressFinish();
        $io->success('All objects handled');
    } 
} catch (Throwable $throwable) {
    if ($output->isVerbose()) {
        $io->error('Error while handling products');
    }
    return Command::FAILURE;
}

return Command::SUCCESS;

之后

$io = new SymfonyStyleVerbose($input, $output);

try {
    $io->titleIfVerbose('This is my title');
    $io->sectionIfVerbose('This is my section');
    
    //do some stuff here
    
    $io->sectionIfVerbose('Get objects');
    $io->progressStartIfVerbose(count($objects));
    $objects = $this->repository->findBy(['active' => true]);
        
    foreach ($objects as $object) {
        //do something with the object
        $io->progressAdvanceIfVerbose();
    }
    
    $io->progressFinishIfVerbose();
    $io->successIfVerbose('All objects handled');
} catch (Throwable $throwable) {
    $io->errorIfVerbose($throwable->getMessage());
    
    return Command::FAILURE;
}

return Command::SUCCESS;

Rector 规则

有一些 rector 规则可以自动执行示例中的更改。前往 规则概述 查看规则详情。

还有一个 SetList,它组合了所有现有的规则。

    $rectorConfig->sets([
        \Elaberino\SymfonyStyleVerbose\Utils\Rector\Set\SymfonyStyleVerboseSetList::CHANGE_OUTPUT,
    ]);