| 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 |
/** |
| 27 |
* {@inheritdoc} |
| 28 |
*/ |
| 29 |
public function setHelperSet(?HelperSet $helperSet = null) |
| 30 |
{ |
| 31 |
$this->helperSet = $helperSet; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* {@inheritdoc} |
| 36 |
*/ |
| 37 |
public function getHelperSet() |
| 38 |
{ |
| 39 |
return $this->helperSet; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the length of a string, using mb_strwidth if it is available. |
| 44 |
* |
| 45 |
* @deprecated since Symfony 5.3 |
| 46 |
* |
| 47 |
* @return int |
| 48 |
*/ |
| 49 |
public static function strlen(?string $string) |
| 50 |
{ |
| 51 |
trigger_deprecation('symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::width() or Helper::length() instead.', __METHOD__); |
| 52 |
|
| 53 |
return self::width($string); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the width of a string, using mb_strwidth if it is available. |
| 58 |
* The width is how many characters positions the string will use. |
| 59 |
*/ |
| 60 |
public static function width(?string $string): int |
| 61 |
{ |
| 62 |
$string ?? $string = ''; |
| 63 |
|
| 64 |
if (preg_match('//u', $string)) { |
| 65 |
return (new UnicodeString($string))->width(false); |
| 66 |
} |
| 67 |
|
| 68 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 69 |
return \strlen($string); |
| 70 |
} |
| 71 |
|
| 72 |
return mb_strwidth($string, $encoding); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the length of a string, using mb_strlen if it is available. |
| 77 |
* The length is related to how many bytes the string will use. |
| 78 |
*/ |
| 79 |
public static function length(?string $string): int |
| 80 |
{ |
| 81 |
$string ?? $string = ''; |
| 82 |
|
| 83 |
if (preg_match('//u', $string)) { |
| 84 |
return (new UnicodeString($string))->length(); |
| 85 |
} |
| 86 |
|
| 87 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 88 |
return \strlen($string); |
| 89 |
} |
| 90 |
|
| 91 |
return mb_strlen($string, $encoding); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the subset of a string, using mb_substr if it is available. |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public static function substr(?string $string, int $from, ?int $length = null) |
| 100 |
{ |
| 101 |
$string ?? $string = ''; |
| 102 |
|
| 103 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 104 |
return substr($string, $from, $length); |
| 105 |
} |
| 106 |
|
| 107 |
return mb_substr($string, $from, $length, $encoding); |
| 108 |
} |
| 109 |
|
| 110 |
public static function formatTime($secs) |
| 111 |
{ |
| 112 |
static $timeFormats = [ |
| 113 |
[0, '< 1 sec'], |
| 114 |
[1, '1 sec'], |
| 115 |
[2, 'secs', 1], |
| 116 |
[60, '1 min'], |
| 117 |
[120, 'mins', 60], |
| 118 |
[3600, '1 hr'], |
| 119 |
[7200, 'hrs', 3600], |
| 120 |
[86400, '1 day'], |
| 121 |
[172800, 'days', 86400], |
| 122 |
]; |
| 123 |
|
| 124 |
foreach ($timeFormats as $index => $format) { |
| 125 |
if ($secs >= $format[0]) { |
| 126 |
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0]) |
| 127 |
|| $index == \count($timeFormats) - 1 |
| 128 |
) { |
| 129 |
if (2 == \count($format)) { |
| 130 |
return $format[1]; |
| 131 |
} |
| 132 |
|
| 133 |
return floor($secs / $format[2]).' '.$format[1]; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public static function formatMemory(int $memory) |
| 140 |
{ |
| 141 |
if ($memory >= 1024 * 1024 * 1024) { |
| 142 |
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024); |
| 143 |
} |
| 144 |
|
| 145 |
if ($memory >= 1024 * 1024) { |
| 146 |
return sprintf('%.1f MiB', $memory / 1024 / 1024); |
| 147 |
} |
| 148 |
|
| 149 |
if ($memory >= 1024) { |
| 150 |
return sprintf('%d KiB', $memory / 1024); |
| 151 |
} |
| 152 |
|
| 153 |
return sprintf('%d B', $memory); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @deprecated since Symfony 5.3 |
| 158 |
*/ |
| 159 |
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) |
| 160 |
{ |
| 161 |
trigger_deprecation('symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::removeDecoration() instead.', __METHOD__); |
| 162 |
|
| 163 |
return self::width(self::removeDecoration($formatter, $string)); |
| 164 |
} |
| 165 |
|
| 166 |
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) |
| 167 |
{ |
| 168 |
$isDecorated = $formatter->isDecorated(); |
| 169 |
$formatter->setDecorated(false); |
| 170 |
// remove <...> formatting |
| 171 |
$string = $formatter->format($string ?? ''); |
| 172 |
// remove already formatted characters |
| 173 |
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? ''); |
| 174 |
// remove terminal hyperlinks |
| 175 |
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? ''); |
| 176 |
$formatter->setDecorated($isDecorated); |
| 177 |
|
| 178 |
return $string; |
| 179 |
} |
| 180 |
} |
| 181 |
|