| 148 |
lars |
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 |
public function formatSection(string $section, string $message, string $style = 'info'): string
|
|
|
27 |
{
|
|
|
28 |
return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Formats a message as a block of text.
|
|
|
33 |
*/
|
|
|
34 |
public function formatBlock(string|array $messages, string $style, bool $large = false): string
|
|
|
35 |
{
|
|
|
36 |
if (!\is_array($messages)) {
|
|
|
37 |
$messages = [$messages];
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$len = 0;
|
|
|
41 |
$lines = [];
|
|
|
42 |
foreach ($messages as $message) {
|
|
|
43 |
$message = OutputFormatter::escape($message);
|
|
|
44 |
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
|
|
|
45 |
$len = max(self::width($message) + ($large ? 4 : 2), $len);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$messages = $large ? [str_repeat(' ', $len)] : [];
|
|
|
49 |
for ($i = 0; isset($lines[$i]); ++$i) {
|
|
|
50 |
$messages[] = $lines[$i].str_repeat(' ', $len - self::width($lines[$i]));
|
|
|
51 |
}
|
|
|
52 |
if ($large) {
|
|
|
53 |
$messages[] = str_repeat(' ', $len);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
for ($i = 0; isset($messages[$i]); ++$i) {
|
|
|
57 |
$messages[$i] = sprintf('<%s>%s</%s>', $style, $messages[$i], $style);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
return implode("\n", $messages);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Truncates a message to the given length.
|
|
|
65 |
*/
|
|
|
66 |
public function truncate(string $message, int $length, string $suffix = '...'): string
|
|
|
67 |
{
|
|
|
68 |
$computedLength = $length - self::width($suffix);
|
|
|
69 |
|
|
|
70 |
if ($computedLength > self::width($message)) {
|
|
|
71 |
return $message;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
return self::substr($message, 0, $length).$suffix;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public function getName(): string
|
|
|
78 |
{
|
|
|
79 |
return 'formatter';
|
|
|
80 |
}
|
|
|
81 |
}
|