Six03,感谢您的提示!当然,我不能认为它是一个足够的答案,但它帮助我找到了所有必要的信息。
首先,您必须将具有上下文“格式化程序”的助手声明为 ContainerAwareCommand 对象的属性:
$formatter = $this->getHelper('formatter');
然后,您必须使用将在控制台中显示的内容声明数组。数组的每个元素都是消息的新行。接下来,您需要将消息和样式添加到声明的“格式化程序”块中,如下所示:
$infoMessage = array('INFO:', 'Date interval is ' . $interval->format('%a') . ' days.');
$formattedInfoBlock = $formatter->formatBlock($infoMessage, 'info', TRUE);
如果您将 true 作为第三个参数传递,该块将被格式化为更多的填充(消息上方和下方各有一个空行,左右各有 2 个空格)。 (Formatter Helper)
现在您要做的就是将具有所需设计的块传递给“输出”对象:
$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);
现在我的命令中的消息具有适当的类型。