4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Formatter\OutputFormatter;
17 * The Formatter class provides helpers to format messages.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class FormatterHelper extends Helper
24 * Formats a message within a section.
26 * @param string $section The section name
27 * @param string $message The message
28 * @param string $style The style to apply to the section
30 * @return string The format section
32 public function formatSection($section, $message, $style = 'info')
34 return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
38 * Formats a message as a block of text.
40 * @param string|array $messages The message to write in the block
41 * @param string $style The style to apply to the whole block
42 * @param Boolean $large Whether to return a large block
44 * @return string The formatter message
46 public function formatBlock($messages, $style, $large = false)
48 $messages = (array) $messages;
52 foreach ($messages as $message) {
53 $message = OutputFormatter::escape($message);
54 $lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
55 $len = max($this->strlen($message) + ($large ? 4 : 2), $len);
58 $messages = $large ? array(str_repeat(' ', $len)) : array();
59 foreach ($lines as $line) {
60 $messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
63 $messages[] = str_repeat(' ', $len);
66 foreach ($messages as &$message) {
67 $message = sprintf('<%s>%s</%s>', $style, $message, $style);
70 return implode("\n", $messages);
74 * Returns the length of a string, using mb_strlen if it is available.
76 * @param string $string The string to check its length
78 * @return integer The length of the string
80 private function strlen($string)
82 if (!function_exists('mb_strlen')) {
83 return strlen($string);
86 if (false === $encoding = mb_detect_encoding($string)) {
87 return strlen($string);
90 return mb_strlen($string, $encoding);
94 * Returns the helper's canonical name.
96 * @return string The canonical name of the helper
98 public function getName()