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