| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 <[email protected]> |
| 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 |
$string = preg_replace('/[\p{Cc}\x7F]++/u', '', $string, -1, $count); |
| 46 |
|
| 47 |
return (new UnicodeString($string))->width(false) + $count; |
| 48 |
} |
| 49 |
|
| 50 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 51 |
return \strlen($string); |
| 52 |
} |
| 53 |
|
| 54 |
return mb_strwidth($string, $encoding); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the length of a string, using mb_strlen if it is available. |
| 59 |
* The length is related to how many bytes the string will use. |
| 60 |
*/ |
| 61 |
public static function length(?string $string): int |
| 62 |
{ |
| 63 |
$string ??= ''; |
| 64 |
|
| 65 |
if (preg_match('//u', $string)) { |
| 66 |
return (new UnicodeString($string))->length(); |
| 67 |
} |
| 68 |
|
| 69 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 70 |
return \strlen($string); |
| 71 |
} |
| 72 |
|
| 73 |
return mb_strlen($string, $encoding); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the subset of a string, using mb_substr if it is available. |
| 78 |
*/ |
| 79 |
public static function substr(?string $string, int $from, ?int $length = null): string |
| 80 |
{ |
| 81 |
$string ??= ''; |
| 82 |
|
| 83 |
if (preg_match('//u', $string)) { |
| 84 |
return (new UnicodeString($string))->slice($from, $length); |
| 85 |
} |
| 86 |
|
| 87 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 88 |
return substr($string, $from, $length); |
| 89 |
} |
| 90 |
|
| 91 |
return mb_substr($string, $from, $length, $encoding); |
| 92 |
} |
| 93 |
|
| 94 |
public static function formatTime(int|float $secs, int $precision = 1): string |
| 95 |
{ |
| 96 |
$ms = (int) ($secs * 1000); |
| 97 |
$secs = (int) floor($secs); |
| 98 |
|
| 99 |
if (0 === $ms) { |
| 100 |
return '< 1 ms'; |
| 101 |
} |
| 102 |
|
| 103 |
static $timeFormats = [ |
| 104 |
[1, 'ms'], |
| 105 |
[1000, 's'], |
| 106 |
[60000, 'min'], |
| 107 |
[3600000, 'h'], |
| 108 |
[86_400_000, 'd'], |
| 109 |
]; |
| 110 |
|
| 111 |
$times = []; |
| 112 |
foreach ($timeFormats as $index => $format) { |
| 113 |
$milliSeconds = isset($timeFormats[$index + 1]) ? $ms % $timeFormats[$index + 1][0] : $ms; |
| 114 |
|
| 115 |
if (isset($times[$index - $precision])) { |
| 116 |
unset($times[$index - $precision]); |
| 117 |
} |
| 118 |
|
| 119 |
if (0 === $milliSeconds) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
$unitCount = ($milliSeconds / $format[0]); |
| 124 |
$times[$index] = $unitCount.' '.$format[1]; |
| 125 |
|
| 126 |
if ($ms === $milliSeconds) { |
| 127 |
break; |
| 128 |
} |
| 129 |
|
| 130 |
$ms -= $milliSeconds; |
| 131 |
} |
| 132 |
|
| 133 |
return implode(', ', array_reverse($times)); |
| 134 |
} |
| 135 |
|
| 136 |
public static function formatMemory(int $memory): string |
| 137 |
{ |
| 138 |
if ($memory >= 1024 * 1024 * 1024) { |
| 139 |
return \sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024); |
| 140 |
} |
| 141 |
|
| 142 |
if ($memory >= 1024 * 1024) { |
| 143 |
return \sprintf('%.1f MiB', $memory / 1024 / 1024); |
| 144 |
} |
| 145 |
|
| 146 |
if ($memory >= 1024) { |
| 147 |
return \sprintf('%d KiB', $memory / 1024); |
| 148 |
} |
| 149 |
|
| 150 |
return \sprintf('%d B', $memory); |
| 151 |
} |
| 152 |
|
| 153 |
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string): string |
| 154 |
{ |
| 155 |
$isDecorated = $formatter->isDecorated(); |
| 156 |
$formatter->setDecorated(false); |
| 157 |
// remove <...> formatting |
| 158 |
$string = $formatter->format($string ?? ''); |
| 159 |
// remove already formatted characters |
| 160 |
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? ''); |
| 161 |
// remove terminal hyperlinks |
| 162 |
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? ''); |
| 163 |
$formatter->setDecorated($isDecorated); |
| 164 |
|
| 165 |
return $string; |
| 166 |
} |
| 167 |
} |