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