| 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\OutputFormatterInterface;
|
|
|
15 |
use Symfony\Component\String\UnicodeString;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Helper is the base class for all helper classes.
|
|
|
19 |
*
|
|
|
20 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
21 |
*/
|
|
|
22 |
abstract class Helper implements HelperInterface
|
|
|
23 |
{
|
|
|
24 |
protected $helperSet = null;
|
|
|
25 |
|
|
|
26 |
public function setHelperSet(HelperSet $helperSet = null)
|
|
|
27 |
{
|
|
|
28 |
if (1 > \func_num_args()) {
|
|
|
29 |
trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
|
|
|
30 |
}
|
|
|
31 |
$this->helperSet = $helperSet;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
public function getHelperSet(): ?HelperSet
|
|
|
35 |
{
|
|
|
36 |
return $this->helperSet;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Returns the width of a string, using mb_strwidth if it is available.
|
|
|
41 |
* The width is how many characters positions the string will use.
|
|
|
42 |
*/
|
|
|
43 |
public static function width(?string $string): int
|
|
|
44 |
{
|
|
|
45 |
$string ??= '';
|
|
|
46 |
|
|
|
47 |
if (preg_match('//u', $string)) {
|
|
|
48 |
return (new UnicodeString($string))->width(false);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
if (false === $encoding = mb_detect_encoding($string, null, true)) {
|
|
|
52 |
return \strlen($string);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
return mb_strwidth($string, $encoding);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Returns the length of a string, using mb_strlen if it is available.
|
|
|
60 |
* The length is related to how many bytes the string will use.
|
|
|
61 |
*/
|
|
|
62 |
public static function length(?string $string): int
|
|
|
63 |
{
|
|
|
64 |
$string ??= '';
|
|
|
65 |
|
|
|
66 |
if (preg_match('//u', $string)) {
|
|
|
67 |
return (new UnicodeString($string))->length();
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (false === $encoding = mb_detect_encoding($string, null, true)) {
|
|
|
71 |
return \strlen($string);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
return mb_strlen($string, $encoding);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Returns the subset of a string, using mb_substr if it is available.
|
|
|
79 |
*/
|
|
|
80 |
public static function substr(?string $string, int $from, int $length = null): string
|
|
|
81 |
{
|
|
|
82 |
$string ??= '';
|
|
|
83 |
|
|
|
84 |
if (false === $encoding = mb_detect_encoding($string, null, true)) {
|
|
|
85 |
return substr($string, $from, $length);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return mb_substr($string, $from, $length, $encoding);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public static function formatTime(int|float $secs)
|
|
|
92 |
{
|
|
|
93 |
static $timeFormats = [
|
|
|
94 |
[0, '< 1 sec'],
|
|
|
95 |
[1, '1 sec'],
|
|
|
96 |
[2, 'secs', 1],
|
|
|
97 |
[60, '1 min'],
|
|
|
98 |
[120, 'mins', 60],
|
|
|
99 |
[3600, '1 hr'],
|
|
|
100 |
[7200, 'hrs', 3600],
|
|
|
101 |
[86400, '1 day'],
|
|
|
102 |
[172800, 'days', 86400],
|
|
|
103 |
];
|
|
|
104 |
|
|
|
105 |
foreach ($timeFormats as $index => $format) {
|
|
|
106 |
if ($secs >= $format[0]) {
|
|
|
107 |
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
|
|
|
108 |
|| $index == \count($timeFormats) - 1
|
|
|
109 |
) {
|
|
|
110 |
if (2 == \count($format)) {
|
|
|
111 |
return $format[1];
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
return floor($secs / $format[2]).' '.$format[1];
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
public static function formatMemory(int $memory)
|
|
|
121 |
{
|
|
|
122 |
if ($memory >= 1024 * 1024 * 1024) {
|
|
|
123 |
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if ($memory >= 1024 * 1024) {
|
|
|
127 |
return sprintf('%.1f MiB', $memory / 1024 / 1024);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if ($memory >= 1024) {
|
|
|
131 |
return sprintf('%d KiB', $memory / 1024);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return sprintf('%d B', $memory);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)
|
|
|
138 |
{
|
|
|
139 |
$isDecorated = $formatter->isDecorated();
|
|
|
140 |
$formatter->setDecorated(false);
|
|
|
141 |
// remove <...> formatting
|
|
|
142 |
$string = $formatter->format($string ?? '');
|
|
|
143 |
// remove already formatted characters
|
|
|
144 |
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
|
|
|
145 |
// remove terminal hyperlinks
|
|
|
146 |
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
|
|
|
147 |
$formatter->setDecorated($isDecorated);
|
|
|
148 |
|
|
|
149 |
return $string;
|
|
|
150 |
}
|
|
|
151 |
}
|