PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用symfony2 compoments OutputInterface和表助手缩进写操作?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个OutpuTinterface,我用它通过Table帮助程序将一堆表写到它们上.该信息具有嵌套上下文,因此我希望输出缩进4个空格.

我认为这样的事情应该可行:

 new Table($output);
 $output->writeln('0. run');
 $soMetable->render();
 $output->increaseIndentLevel(); // pseudocode
 $output->writeln('1. run');
 $soMetable->render();

创建预期的输出

0. run
+---------------+-----------------------+------------------+
| ISBN          | title                 | Author           |
+---------------+-----------------------+------------------+
| 99921-58-10-7 | Divine Comedy         | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities  | Charles Dickens  |
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
+---------------+-----------------------+------------------+
     1. run
     +---------------+-----------------------+------------------+
     | ISBN          | title                 | Author           |
     +---------------+-----------------------+------------------+
     | 99921-58-10-7 | Divine Comedy         | Dante Alighieri  |
     | 9971-5-0210-0 | A Tale of Two Cities  | Charles Dickens  |
     | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
     +---------------+-----------------------+------------------+

搜索了实现此方法方法.我注意到OutpuTinterface提供了OutputFormatterStyle,但这似乎只能更改文本的颜色,可以设置一些与在写操作之前或之后添加内容无关的选项.

我可以扩展一个OutpuTinterface,例如ConsoLeoutput,但我也希望能够将此功能添加到任何OutpuTinterfaces(例如BufferedOutput),而不必为每个接口创建手动版本.

我最后的尝试是将自己的OutputFormatter注入OutpuTinterface:

<?PHP
namespace Hive\App;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
use Symfony\Component\Console\Output\OutpuTinterface;

/**
 * IndentedOutputFormatter
 **/
class IndentedOutputFormatter extends OutputFormatter
{
    const INDENT_amouNT = 4;

    private $indentLevel = 0;

    /**
     * Formats a message according to the given styles.
     * @param String $message The message to style
     * @return String The styled message
     * @api
     */
    public function format($messagE)
    {
        $message = parent::format($messagE);
        if ($this->indentLevel === 0) {
            return $message;
        }

        $amount = self::INDENT_amouNT * $this->indentLevel;
        $prependBy = str_repeat(' ', $amount);
        $message = $prependBy . $message;

        return $message;
    }

    /**
     *
     */
    public function increaSELEvel()
    {
        $this->indentLevel = $this->indentLevel + 1;
    }

    /**
     *
     */
    public function decreaSELEvel()
    {
        $this->indentLevel = $this->indentLevel - 1;
    }
}

并从命令中像这样使用它:

/**
 * @param InpuTinterface  $input
 * @param OutpuTinterface $output
 * @return int
 */
protected function execute(InpuTinterface $input, OutpuTinterface $output)
{
    $headers = [
        'ISBN',
        'title',
        'Author',
    ];

    $rows = [
        [
            '99921-58-10-7',
            'Divine Comedy',
            'Dante Alighieri',

        ],
        [
            '9971-5-0210-0',
            'A Tale of Two Cities',
            'Charles Dickens',

        ],
        [
            '960-425-059-0',
            'The Lord of the Rings',
            'J. R. R. Tolkien',
        ],
    ];


    $formatter = new IndentedOutputFormatter();
    $output->setFormatter($formatter);
    $table = new Table($output);
    $table->setHeaders($headers);
    $table->setRows($rows);

    foreach (range(0, 3) as $currentRun) {
        $output->writeln("$currentRun. run");
        $formatter->increaSELEvel();
        $table->render();
    }

    return 0;
}

但这产生了一个问题,不仅表是通过缩进呈现的,还包括它的内容字段:

0. run
    +-------------------+---------------------------+----------------------+
    |     ISBN              |     title                     |     Author               |    
    +-------------------+---------------------------+----------------------+
    |     99921-58-10-7     |     Divine Comedy             |     Dante Alighieri      |    
    |     9971-5-0210-0     |     A Tale of Two Cities      |     Charles Dickens      |    
    |     960-425-059-0     |     The Lord of the Rings     |     J. R. R. Tolkien     |    
    +-------------------+---------------------------+----------------------+
    1. run
        +-----------------------+-------------------------------+--------------------------+
        |         ISBN                  |         title                         |         Author                   |        
        +-----------------------+-------------------------------+--------------------------+
        |         99921-58-10-7         |         Divine Comedy                 |         Dante Alighieri          |        
        |         9971-5-0210-0         |         A Tale of Two Cities          |         Charles Dickens          |        
        |         960-425-059-0         |         The Lord of the Rings         |         J. R. R. Tolkien         |        
        +-----------------------+-------------------------------+--------------------------+
        2. run
            +---------------------------+-----------------------------------+------------------------------+
            |             ISBN                      |             title                             |             Author                       |            
            +---------------------------+-----------------------------------+------------------------------+
            |             99921-58-10-7             |             Divine Comedy                     |             Dante Alighieri              |            
            |             9971-5-0210-0             |             A Tale of Two Cities              |             Charles Dickens              |            
            |             960-425-059-0             |             The Lord of the Rings             |             J. R. R. Tolkien             |            
            +---------------------------+-----------------------------------+------------------------------+
            3. run
                +-------------------------------+---------------------------------------+----------------------------------+
                |                 ISBN                          |                 title                                 |                 Author                           |                
                +-------------------------------+---------------------------------------+----------------------------------+
                |                 99921-58-10-7                 |                 Divine Comedy                         |                 Dante Alighieri                  |                
                |                 9971-5-0210-0                 |                 A Tale of Two Cities                  |                 Charles Dickens                  |                
                |                 960-425-059-0                 |                 The Lord of the Rings                 |                 J. R. R. Tolkien                 |                
                +-------------------------------+---------------------------------------+----------------------------------+

如何使用缩进和OutpuTinterface的symfony2组件以及表格助手来工作?

解决方法:

这做好了

输出装饰器

use Symfony\Component\Console\Output\Output;

class IndentedOutput extends Output
{
    const INDENT_amouNT = 4;

    private $output;
    private $indentLevel = 0;
    private $resetLine = false;

    public function setOutput(OutpuTinterface $output)
    {
        $this->output = $output;
    }

    public function increaSELEvel()
    {
        $this->indentLevel += 1;
    }

    public function decreaSELEvel()
    {
        $this->indentLevel -= 1;
    }

    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
    {
        $prependBy = str_repeat(' ', self::INDENT_amouNT * $this->indentLevel);

        if ($newlinE) {
            $this->resetLine = true;
            $messages = $prependBy.$messages;
        }

        if ($this->resetLine && !$newlinE) {
            $messages = $prependBy.$messages;
            $this->resetLine = false;
        }

        $this->output->write($messages, $newline, $typE);
    }

    public function doWrite($message, $newlinE)
    {
        $this->output->doWrite($message, $newlinE);
    }
}

测试

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InpuTinterface;
use Symfony\Component\Console\Output\OutpuTinterface;
use Symfony\Component\Console\Output\BufferedOutput;

$headers = [
    'ISBN',
    'title',
    'Author',
];

$rows = [[
    '99921-58-10-7',
    'Divine Comedy',
    'Dante Alighieri',

],[
    '9971-5-0210-0',
    'A Tale of Two Cities',
    'Charles Dickens',

],[
    '960-425-059-0',
    'The Lord of the Rings',
    'J. R. R. Tolkien',
]];

$app = new Application();
$app
    ->register('foo')
    ->setCode(function(InpuTinterface $input, OutpuTinterface $output) use ($headers, $rows) {

        $buffered = new BufferedOutput;
        $indented = new IndentedOutput;
        $indented->setOutput($buffered);

        $table = new Table($indented);
        $table->setHeaders($headers);
        $table->setRows($rows);

        foreach (range(0, 3) as $currentRun) {
            $indented->writeln("$currentRun. run");
            $table->render();
            $indented->increaSELEvel();
        }

        $indented->decreaSELEvel();
        $indented->decreaSELEvel();
        $indented->decreaSELEvel();
        $indented->decreaSELEvel();
        $indented->write('Hello world');

        $output->write($buffered->fetch());
    });
$app->run();

输出

0. run
+---------------+-----------------------+------------------+
| ISBN          | title                 | Author           |
+---------------+-----------------------+------------------+
| 99921-58-10-7 | Divine Comedy         | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities  | Charles Dickens  |
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
+---------------+-----------------------+------------------+
    1. run
    +---------------+-----------------------+------------------+
    | ISBN          | title                 | Author           |    
    +---------------+-----------------------+------------------+
    | 99921-58-10-7 | Divine Comedy         | Dante Alighieri  |    
    | 9971-5-0210-0 | A Tale of Two Cities  | Charles Dickens  |    
    | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |    
    +---------------+-----------------------+------------------+
        2. run
        +---------------+-----------------------+------------------+
        | ISBN          | title                 | Author           |        
        +---------------+-----------------------+------------------+
        | 99921-58-10-7 | Divine Comedy         | Dante Alighieri  |        
        | 9971-5-0210-0 | A Tale of Two Cities  | Charles Dickens  |        
        | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |        
        +---------------+-----------------------+------------------+
            3. run
            +---------------+-----------------------+------------------+
            | ISBN          | title                 | Author           |            
            +---------------+-----------------------+------------------+
            | 99921-58-10-7 | Divine Comedy         | Dante Alighieri  |            
            | 9971-5-0210-0 | A Tale of Two Cities  | Charles Dickens  |            
            | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |            
            +---------------+-----------------------+------------------+
Hello world

大佬总结

以上是大佬教程为你收集整理的如何使用symfony2 compoments OutputInterface和表助手缩进写操作?全部内容,希望文章能够帮你解决如何使用symfony2 compoments OutputInterface和表助手缩进写操作?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: