gpt4 book ai didi

php - 如何使用 symfony2 组件 OutputInterface 和 Table helper 缩进写操作?

转载 作者:可可西里 更新时间:2023-11-01 00:59:19 26 4
gpt4 key购买 nike

我有一个 OutputInterface,我用它通过 Table 将一堆表写到它们上面 helper 。该信息具有嵌套上下文,因此我希望输出缩进 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 注入(inject)到 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 组件和 Table 助手来实现缩进?

最佳答案

这样就可以了

输出装饰器

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

关于php - 如何使用 symfony2 组件 OutputInterface 和 Table helper 缩进写操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31946977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com