【问题标题】:How to add padding to the Symfony console command?如何向 Symfony 控制台命令添加填充?
【发布时间】:2016-12-04 04:05:14
【问题描述】:

开发者,嗨!

我想在我的控制台命令中输出消息,看起来就像在 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 命令中那样得到填充。

你有什么想法吗?谢谢!

【问题讨论】:

    标签: console command output symfony


    【解决方案1】:

    我找到了这个,也许这就是你要找的东西;

    /**
     * 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();
    }
    

    You can find the entire file here.

    【讨论】:

      【解决方案2】:

      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);
      

      现在我的命令中的消息具有适当的类型。

      【讨论】:

        猜你喜欢
        • 2015-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 2017-11-02
        • 2012-07-04
        • 1970-01-01
        相关资源
        最近更新 更多