diff options
Diffstat (limited to 'vendor/symfony/console/Helper/Helper.php')
-rw-r--r-- | vendor/symfony/console/Helper/Helper.php | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/vendor/symfony/console/Helper/Helper.php b/vendor/symfony/console/Helper/Helper.php new file mode 100644 index 0000000..de09006 --- /dev/null +++ b/vendor/symfony/console/Helper/Helper.php | |||
@@ -0,0 +1,159 @@ | |||
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 $helperSet = null; | ||
25 | |||
26 | public function setHelperSet(?HelperSet $helperSet): void | ||
27 | { | ||
28 | $this->helperSet = $helperSet; | ||
29 | } | ||
30 | |||
31 | public function getHelperSet(): ?HelperSet | ||
32 | { | ||
33 | return $this->helperSet; | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Returns the width of a string, using mb_strwidth if it is available. | ||
38 | * The width is how many characters positions the string will use. | ||
39 | */ | ||
40 | public static function width(?string $string): int | ||
41 | { | ||
42 | $string ??= ''; | ||
43 | |||
44 | if (preg_match('//u', $string)) { | ||
45 | return (new UnicodeString($string))->width(false); | ||
46 | } | ||
47 | |||
48 | if (false === $encoding = mb_detect_encoding($string, null, true)) { | ||
49 | return \strlen($string); | ||
50 | } | ||
51 | |||
52 | return mb_strwidth($string, $encoding); | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Returns the length of a string, using mb_strlen if it is available. | ||
57 | * The length is related to how many bytes the string will use. | ||
58 | */ | ||
59 | public static function length(?string $string): int | ||
60 | { | ||
61 | $string ??= ''; | ||
62 | |||
63 | if (preg_match('//u', $string)) { | ||
64 | return (new UnicodeString($string))->length(); | ||
65 | } | ||
66 | |||
67 | if (false === $encoding = mb_detect_encoding($string, null, true)) { | ||
68 | return \strlen($string); | ||
69 | } | ||
70 | |||
71 | return mb_strlen($string, $encoding); | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * Returns the subset of a string, using mb_substr if it is available. | ||
76 | */ | ||
77 | public static function substr(?string $string, int $from, ?int $length = null): string | ||
78 | { | ||
79 | $string ??= ''; | ||
80 | |||
81 | if (false === $encoding = mb_detect_encoding($string, null, true)) { | ||
82 | return substr($string, $from, $length); | ||
83 | } | ||
84 | |||
85 | return mb_substr($string, $from, $length, $encoding); | ||
86 | } | ||
87 | |||
88 | public static function formatTime(int|float $secs, int $precision = 1): string | ||
89 | { | ||
90 | $secs = (int) floor($secs); | ||
91 | |||
92 | if (0 === $secs) { | ||
93 | return '< 1 sec'; | ||
94 | } | ||
95 | |||
96 | static $timeFormats = [ | ||
97 | [1, '1 sec', 'secs'], | ||
98 | [60, '1 min', 'mins'], | ||
99 | [3600, '1 hr', 'hrs'], | ||
100 | [86400, '1 day', 'days'], | ||
101 | ]; | ||
102 | |||
103 | $times = []; | ||
104 | foreach ($timeFormats as $index => $format) { | ||
105 | $seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs; | ||
106 | |||
107 | if (isset($times[$index - $precision])) { | ||
108 | unset($times[$index - $precision]); | ||
109 | } | ||
110 | |||
111 | if (0 === $seconds) { | ||
112 | continue; | ||
113 | } | ||
114 | |||
115 | $unitCount = ($seconds / $format[0]); | ||
116 | $times[$index] = 1 === $unitCount ? $format[1] : $unitCount.' '.$format[2]; | ||
117 | |||
118 | if ($secs === $seconds) { | ||
119 | break; | ||
120 | } | ||
121 | |||
122 | $secs -= $seconds; | ||
123 | } | ||
124 | |||
125 | return implode(', ', array_reverse($times)); | ||
126 | } | ||
127 | |||
128 | public static function formatMemory(int $memory): string | ||
129 | { | ||
130 | if ($memory >= 1024 * 1024 * 1024) { | ||
131 | return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024); | ||
132 | } | ||
133 | |||
134 | if ($memory >= 1024 * 1024) { | ||
135 | return sprintf('%.1f MiB', $memory / 1024 / 1024); | ||
136 | } | ||
137 | |||
138 | if ($memory >= 1024) { | ||
139 | return sprintf('%d KiB', $memory / 1024); | ||
140 | } | ||
141 | |||
142 | return sprintf('%d B', $memory); | ||
143 | } | ||
144 | |||
145 | public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string): string | ||
146 | { | ||
147 | $isDecorated = $formatter->isDecorated(); | ||
148 | $formatter->setDecorated(false); | ||
149 | // remove <...> formatting | ||
150 | $string = $formatter->format($string ?? ''); | ||
151 | // remove already formatted characters | ||
152 | $string = preg_replace("/\033\[[^m]*m/", '', $string ?? ''); | ||
153 | // remove terminal hyperlinks | ||
154 | $string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? ''); | ||
155 | $formatter->setDecorated($isDecorated); | ||
156 | |||
157 | return $string; | ||
158 | } | ||
159 | } | ||