如何向 Symfony 控制台命令添加填充?
How to add padding to the Symfony console command?
各位开发者,大家好!
我想在我的控制台命令中输出消息,看起来就像在 Doctrine 控制台命令中实现的那样:
我尝试使用 PHP_EOL 向控制台消息添加换行符,如下所示:
$output->writeln('<bg=green>' . PHP_EOL. 'The statistics of ' . $affiliateProgramName . ' for period from ' .
$beginningDate->format('Y-m-d') . ' to ' . $endDate->format('Y-m-d') . ' has been downloaded.' . PHP_EOL . '</>');
结果如图:
背景拉伸到控制台的整个宽度,我没有像 Doctrine 命令那样填充。
你有什么想法吗?谢谢!
我找到了这个,也许这就是你要找的东西;
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
* @param string|null $type The block type (added in [] on first line)
* @param string|null $style The style to apply to the whole block
* @param string $prefix The prefix for the block
* @param bool $padding Whether to add vertical padding
*/
public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
{
$messages = is_array($messages) ? array_values($messages) : array($messages);
$this->autoPrependBlock();
$this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, true));
$this->newLine();
}
Six03,感谢您的提示!当然,我不能认为它是一个充分的答案,但它帮助我找到了所有必要的信息。
首先,您必须将上下文 'formatter' 的助手声明为 ContainerAwareCommand 对象的 属性:
$formatter = $this->getHelper('formatter');
然后您必须声明一个数组,其中包含将在控制台中显示的内容。 array 的每个元素都是您消息的新行。接下来,您需要将消息和样式添加到已声明的 'formatter' 块中,如下所示:
$infoMessage = array('INFO:', 'Date interval is ' . $interval->format('%a') . ' days.');
$formattedInfoBlock = $formatter->formatBlock($infoMessage, 'info', TRUE);
如果您将 true 作为第三个参数传递,块将被格式化为更多填充(消息上方和下方各有一个空行,左右各有 2 个空格)。 (Formatter Helper)
现在您要做的就是将具有所需设计的块传递给 'output' 对象:
$output->writeln($formattedInfoBlock);
下面是完整的代码:
/**
* Setting the console styles
*
* 'INFO' style
*/
$infoStyle = new OutputFormatterStyle('white', 'blue');
$output->getFormatter()->setStyle('info', $infoStyle);
/**
* 'SUCCESS' style
*/
$successStyle = new OutputFormatterStyle('white', 'green');
$output->getFormatter()->setStyle('success', $successStyle);
/**
* Declaring the formatter
*/
$formatter = $this->getHelper('formatter');
/**
* The output to the console
*/
$infoMessage = array('INFO:', 'Date interval is ' . $interval->format('%a') . ' days.');
$formattedInfoBlock = $formatter->formatBlock($infoMessage, 'info', TRUE);
$output->writeln($formattedInfoBlock);
现在我的命令中的消息具有适当的种类。
各位开发者,大家好!
我想在我的控制台命令中输出消息,看起来就像在 Doctrine 控制台命令中实现的那样:
我尝试使用 PHP_EOL 向控制台消息添加换行符,如下所示:
$output->writeln('<bg=green>' . PHP_EOL. 'The statistics of ' . $affiliateProgramName . ' for period from ' .
$beginningDate->format('Y-m-d') . ' to ' . $endDate->format('Y-m-d') . ' has been downloaded.' . PHP_EOL . '</>');
结果如图:
背景拉伸到控制台的整个宽度,我没有像 Doctrine 命令那样填充。
你有什么想法吗?谢谢!
我找到了这个,也许这就是你要找的东西;
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
* @param string|null $type The block type (added in [] on first line)
* @param string|null $style The style to apply to the whole block
* @param string $prefix The prefix for the block
* @param bool $padding Whether to add vertical padding
*/
public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
{
$messages = is_array($messages) ? array_values($messages) : array($messages);
$this->autoPrependBlock();
$this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, true));
$this->newLine();
}
Six03,感谢您的提示!当然,我不能认为它是一个充分的答案,但它帮助我找到了所有必要的信息。
首先,您必须将上下文 'formatter' 的助手声明为 ContainerAwareCommand 对象的 属性:
$formatter = $this->getHelper('formatter');
然后您必须声明一个数组,其中包含将在控制台中显示的内容。 array 的每个元素都是您消息的新行。接下来,您需要将消息和样式添加到已声明的 'formatter' 块中,如下所示:
$infoMessage = array('INFO:', 'Date interval is ' . $interval->format('%a') . ' days.');
$formattedInfoBlock = $formatter->formatBlock($infoMessage, 'info', TRUE);
如果您将 true 作为第三个参数传递,块将被格式化为更多填充(消息上方和下方各有一个空行,左右各有 2 个空格)。 (Formatter Helper)
现在您要做的就是将具有所需设计的块传递给 'output' 对象:
$output->writeln($formattedInfoBlock);
下面是完整的代码:
/**
* Setting the console styles
*
* 'INFO' style
*/
$infoStyle = new OutputFormatterStyle('white', 'blue');
$output->getFormatter()->setStyle('info', $infoStyle);
/**
* 'SUCCESS' style
*/
$successStyle = new OutputFormatterStyle('white', 'green');
$output->getFormatter()->setStyle('success', $successStyle);
/**
* Declaring the formatter
*/
$formatter = $this->getHelper('formatter');
/**
* The output to the console
*/
$infoMessage = array('INFO:', 'Date interval is ' . $interval->format('%a') . ' days.');
$formattedInfoBlock = $formatter->formatBlock($infoMessage, 'info', TRUE);
$output->writeln($formattedInfoBlock);
现在我的命令中的消息具有适当的种类。