| 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 |
|
| 16 |
/** |
| 17 |
* Helper is the base class for all helper classes. |
| 18 |
* |
| 19 |
* @author Fabien Potencier <fabien@symfony.com> |
| 20 |
*/ |
| 21 |
abstract class Helper implements HelperInterface |
| 22 |
{ |
| 23 |
protected $helperSet = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* {@inheritdoc} |
| 27 |
*/ |
| 28 |
public function setHelperSet(HelperSet $helperSet = null) |
| 29 |
{ |
| 30 |
$this->helperSet = $helperSet; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* {@inheritdoc} |
| 35 |
*/ |
| 36 |
public function getHelperSet() |
| 37 |
{ |
| 38 |
return $this->helperSet; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns the length of a string, using mb_strwidth if it is available. |
| 43 |
* |
| 44 |
* @param string $string The string to check its length |
| 45 |
* |
| 46 |
* @return int The length of the string |
| 47 |
*/ |
| 48 |
public static function strlen($string) |
| 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 subset of a string, using mb_substr if it is available. |
| 59 |
* |
| 60 |
* @param string $string String to subset |
| 61 |
* @param int $from Start offset |
| 62 |
* @param int|null $length Length to read |
| 63 |
* |
| 64 |
* @return string The string subset |
| 65 |
*/ |
| 66 |
public static function substr($string, $from, $length = null) |
| 67 |
{ |
| 68 |
if (false === $encoding = mb_detect_encoding($string, null, true)) { |
| 69 |
return substr($string, $from, $length); |
| 70 |
} |
| 71 |
|
| 72 |
return mb_substr($string, $from, $length, $encoding); |
| 73 |
} |
| 74 |
|
| 75 |
public static function formatTime($secs) |
| 76 |
{ |
| 77 |
static $timeFormats = [ |
| 78 |
[0, '< 1 sec'], |
| 79 |
[1, '1 sec'], |
| 80 |
[2, 'secs', 1], |
| 81 |
[60, '1 min'], |
| 82 |
[120, 'mins', 60], |
| 83 |
[3600, '1 hr'], |
| 84 |
[7200, 'hrs', 3600], |
| 85 |
[86400, '1 day'], |
| 86 |
[172800, 'days', 86400], |
| 87 |
]; |
| 88 |
|
| 89 |
foreach ($timeFormats as $index => $format) { |
| 90 |
if ($secs >= $format[0]) { |
| 91 |
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0]) |
| 92 |
|| $index == \count($timeFormats) - 1 |
| 93 |
) { |
| 94 |
if (2 == \count($format)) { |
| 95 |
return $format[1]; |
| 96 |
} |
| 97 |
|
| 98 |
return floor($secs / $format[2]).' '.$format[1]; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public static function formatMemory($memory) |
| 105 |
{ |
| 106 |
if ($memory >= 1024 * 1024 * 1024) { |
| 107 |
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024); |
| 108 |
} |
| 109 |
|
| 110 |
if ($memory >= 1024 * 1024) { |
| 111 |
return sprintf('%.1f MiB', $memory / 1024 / 1024); |
| 112 |
} |
| 113 |
|
| 114 |
if ($memory >= 1024) { |
| 115 |
return sprintf('%d KiB', $memory / 1024); |
| 116 |
} |
| 117 |
|
| 118 |
return sprintf('%d B', $memory); |
| 119 |
} |
| 120 |
|
| 121 |
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string) |
| 122 |
{ |
| 123 |
return self::strlen(self::removeDecoration($formatter, $string)); |
| 124 |
} |
| 125 |
|
| 126 |
public static function removeDecoration(OutputFormatterInterface $formatter, $string) |
| 127 |
{ |
| 128 |
$isDecorated = $formatter->isDecorated(); |
| 129 |
$formatter->setDecorated(false); |
| 130 |
// remove <...> formatting |
| 131 |
$string = $formatter->format($string); |
| 132 |
// remove already formatted characters |
| 133 |
$string = preg_replace("/\033\[[^m]*m/", '', $string); |
| 134 |
$formatter->setDecorated($isDecorated); |
| 135 |
|
| 136 |
return $string; |
| 137 |
} |
| 138 |
} |
| 139 |
|