Helper.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $string = preg_replace('/[\p{Cc}\x7F]++/u', '', $string, -1, $count);
  38. return (new UnicodeString($string))->width(false) + $count;
  39. }
  40. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  41. return \strlen($string);
  42. }
  43. return mb_strwidth($string, $encoding);
  44. }
  45. /**
  46. * Returns the length of a string, using mb_strlen if it is available.
  47. * The length is related to how many bytes the string will use.
  48. */
  49. public static function length(?string $string): int
  50. {
  51. $string ??= '';
  52. if (preg_match('//u', $string)) {
  53. return (new UnicodeString($string))->length();
  54. }
  55. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  56. return \strlen($string);
  57. }
  58. return mb_strlen($string, $encoding);
  59. }
  60. /**
  61. * Returns the subset of a string, using mb_substr if it is available.
  62. */
  63. public static function substr(?string $string, int $from, ?int $length = null): string
  64. {
  65. $string ??= '';
  66. if (preg_match('//u', $string)) {
  67. return (new UnicodeString($string))->slice($from, $length);
  68. }
  69. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  70. return substr($string, $from, $length);
  71. }
  72. return mb_substr($string, $from, $length, $encoding);
  73. }
  74. public static function formatTime(int|float $secs, int $precision = 1): string
  75. {
  76. $ms = (int) ($secs * 1000);
  77. $secs = (int) floor($secs);
  78. if (0 === $ms) {
  79. return '< 1 ms';
  80. }
  81. static $timeFormats = [
  82. [1, 'ms'],
  83. [1000, 's'],
  84. [60000, 'min'],
  85. [3600000, 'h'],
  86. [86_400_000, 'd'],
  87. ];
  88. $times = [];
  89. foreach ($timeFormats as $index => $format) {
  90. $milliSeconds = isset($timeFormats[$index + 1]) ? $ms % $timeFormats[$index + 1][0] : $ms;
  91. if (isset($times[$index - $precision])) {
  92. unset($times[$index - $precision]);
  93. }
  94. if (0 === $milliSeconds) {
  95. continue;
  96. }
  97. $unitCount = ($milliSeconds / $format[0]);
  98. $times[$index] = $unitCount.' '.$format[1];
  99. if ($ms === $milliSeconds) {
  100. break;
  101. }
  102. $ms -= $milliSeconds;
  103. }
  104. return implode(', ', array_reverse($times));
  105. }
  106. public static function formatMemory(int $memory): string
  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. public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string): string
  120. {
  121. $isDecorated = $formatter->isDecorated();
  122. $formatter->setDecorated(false);
  123. // remove <...> formatting
  124. $string = $formatter->format($string ?? '');
  125. // remove already formatted characters
  126. $string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
  127. // remove terminal hyperlinks
  128. $string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
  129. $formatter->setDecorated($isDecorated);
  130. return $string;
  131. }
  132. }