|
0
|
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 |
/** |
|
|
15 |
* The Formatter class provides helpers to format messages. |
|
|
16 |
* |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
*/ |
|
|
19 |
class FormatterHelper extends Helper |
|
|
20 |
{ |
|
|
21 |
/** |
|
|
22 |
* Formats a message within a section. |
|
|
23 |
* |
|
|
24 |
* @param string $section The section name |
|
|
25 |
* @param string $message The message |
|
|
26 |
* @param string $style The style to apply to the section |
|
|
27 |
*/ |
|
|
28 |
public function formatSection($section, $message, $style = 'info') |
|
|
29 |
{ |
|
|
30 |
return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message); |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
/** |
|
|
34 |
* Formats a message as a block of text. |
|
|
35 |
* |
|
|
36 |
* @param string|array $messages The message to write in the block |
|
|
37 |
* @param string $style The style to apply to the whole block |
|
|
38 |
* @param Boolean $large Whether to return a large block |
|
|
39 |
* |
|
|
40 |
* @return string The formatter message |
|
|
41 |
*/ |
|
|
42 |
public function formatBlock($messages, $style, $large = false) |
|
|
43 |
{ |
|
|
44 |
$messages = (array) $messages; |
|
|
45 |
|
|
|
46 |
$len = 0; |
|
|
47 |
$lines = array(); |
|
|
48 |
foreach ($messages as $message) { |
|
|
49 |
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message); |
|
|
50 |
$len = max($this->strlen($message) + ($large ? 4 : 2), $len); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
$messages = $large ? array(str_repeat(' ', $len)) : array(); |
|
|
54 |
foreach ($lines as $line) { |
|
|
55 |
$messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); |
|
|
56 |
} |
|
|
57 |
if ($large) { |
|
|
58 |
$messages[] = str_repeat(' ', $len); |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
foreach ($messages as &$message) { |
|
|
62 |
$message = sprintf('<%s>%s</%s>', $style, $message, $style); |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
return implode("\n", $messages); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
/** |
|
|
69 |
* Returns the length of a string, uses mb_strlen if it is available. |
|
|
70 |
* |
|
|
71 |
* @param string $string The string to check its length |
|
|
72 |
* |
|
|
73 |
* @return integer The length of the string |
|
|
74 |
*/ |
|
|
75 |
private function strlen($string) |
|
|
76 |
{ |
|
|
77 |
return function_exists('mb_strlen') ? mb_strlen($string, mb_detect_encoding($string)) : strlen($string); |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
/** |
|
|
81 |
* Returns the helper's canonical name |
|
|
82 |
* |
|
|
83 |
* @return string The canonical name of the helper |
|
|
84 |
*/ |
|
|
85 |
public function getName() |
|
|
86 |
{ |
|
|
87 |
return 'formatter'; |
|
|
88 |
} |
|
|
89 |
} |