Helper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  12. use Symfony\Component\String\UnicodeString;
  13. /**
  14. * Helper is the base class for all helper classes.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. abstract class Helper implements HelperInterface
  19. {
  20. protected ?HelperSet $helperSet = null;
  21. public function setHelperSet(?HelperSet $helperSet): void
  22. {
  23. $this->helperSet = $helperSet;
  24. }
  25. public function getHelperSet(): ?HelperSet
  26. {
  27. return $this->helperSet;
  28. }
  29. /**
  30. * Returns the width of a string, using mb_strwidth if it is available.
  31. * The width is how many characters positions the string will use.
  32. */
  33. public static function width(?string $string): int
  34. {
  35. $string ??= '';
  36. if (preg_match('//u', $string)) {
  37. return (new UnicodeString($string))->width(false);
  38. }
  39. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  40. return \strlen($string);
  41. }
  42. return mb_strwidth($string, $encoding);
  43. }
  44. /**
  45. * Returns the length of a string, using mb_strlen if it is available.
  46. * The length is related to how many bytes the string will use.
  47. */
  48. public static function length(?string $string): int
  49. {
  50. $string ??= '';
  51. if (preg_match('//u', $string)) {
  52. return (new UnicodeString($string))->length();
  53. }
  54. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  55. return \strlen($string);
  56. }
  57. return mb_strlen($string, $encoding);
  58. }
  59. /**
  60. * Returns the subset of a string, using mb_substr if it is available.
  61. */
  62. public static function substr(?string $string, int $from, ?int $length = null): string
  63. {
  64. $string ??= '';
  65. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  66. return substr($string, $from, $length);
  67. }
  68. return mb_substr($string, $from, $length, $encoding);
  69. }
  70. public static function formatTime(int|float $secs, int $precision = 1): string
  71. {
  72. $secs = (int) floor($secs);
  73. if (0 === $secs) {
  74. return '< 1 sec';
  75. }
  76. static $timeFormats = [
  77. [1, '1 sec', 'secs'],
  78. [60, '1 min', 'mins'],
  79. [3600, '1 hr', 'hrs'],
  80. [86400, '1 day', 'days'],
  81. ];
  82. $times = [];
  83. foreach ($timeFormats as $index => $format) {
  84. $seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
  85. if (isset($times[$index - $precision])) {
  86. unset($times[$index - $precision]);
  87. }
  88. if (0 === $seconds) {
  89. continue;
  90. }
  91. $unitCount = ($seconds / $format[0]);
  92. $times[$index] = 1 === $unitCount ? $format[1] : $unitCount.' '.$format[2];
  93. if ($secs === $seconds) {
  94. break;
  95. }
  96. $secs -= $seconds;
  97. }
  98. return implode(', ', array_reverse($times));
  99. }
  100. public static function formatMemory(int $memory): string
  101. {
  102. if ($memory >= 1024 * 1024 * 1024) {
  103. return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  104. }
  105. if ($memory >= 1024 * 1024) {
  106. return sprintf('%.1f MiB', $memory / 1024 / 1024);
  107. }
  108. if ($memory >= 1024) {
  109. return sprintf('%d KiB', $memory / 1024);
  110. }
  111. return sprintf('%d B', $memory);
  112. }
  113. public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string): string
  114. {
  115. $isDecorated = $formatter->isDecorated();
  116. $formatter->setDecorated(false);
  117. // remove <...> formatting
  118. $string = $formatter->format($string ?? '');
  119. // remove already formatted characters
  120. $string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
  121. // remove terminal hyperlinks
  122. $string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
  123. $formatter->setDecorated($isDecorated);
  124. return $string;
  125. }
  126. }