1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Console\Helper;
13:
14: use Symfony\Component\Console\Formatter\OutputFormatter;
15:
16: /**
17: * The Formatter class provides helpers to format messages.
18: *
19: * @author Fabien Potencier <fabien@symfony.com>
20: */
21: class FormatterHelper extends Helper
22: {
23: /**
24: * Formats a message within a section.
25: *
26: * @param string $section The section name
27: * @param string $message The message
28: * @param string $style The style to apply to the section
29: *
30: * @return string The format section
31: */
32: public function formatSection($section, $message, $style = 'info')
33: {
34: return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
35: }
36:
37: /**
38: * Formats a message as a block of text.
39: *
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
43: *
44: * @return string The formatter message
45: */
46: public function formatBlock($messages, $style, $large = false)
47: {
48: $messages = (array) $messages;
49:
50: $len = 0;
51: $lines = array();
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);
56: }
57:
58: $messages = $large ? array(str_repeat(' ', $len)) : array();
59: foreach ($lines as $line) {
60: $messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
61: }
62: if ($large) {
63: $messages[] = str_repeat(' ', $len);
64: }
65:
66: foreach ($messages as &$message) {
67: $message = sprintf('<%s>%s</%s>', $style, $message, $style);
68: }
69:
70: return implode("\n", $messages);
71: }
72:
73: /**
74: * Returns the length of a string, using mb_strlen if it is available.
75: *
76: * @param string $string The string to check its length
77: *
78: * @return integer The length of the string
79: */
80: private function strlen($string)
81: {
82: if (!function_exists('mb_strlen')) {
83: return strlen($string);
84: }
85:
86: if (false === $encoding = mb_detect_encoding($string)) {
87: return strlen($string);
88: }
89:
90: return mb_strlen($string, $encoding);
91: }
92:
93: /**
94: * {@inheritDoc}
95: */
96: public function getName()
97: {
98: return 'formatter';
99: }
100: }
101: