Helper.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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;
  21. /**
  22. * @return void
  23. */
  24. public function setHelperSet(?HelperSet $helperSet = null)
  25. {
  26. if (1 > \func_num_args()) {
  27. trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  28. }
  29. $this->helperSet = $helperSet;
  30. }
  31. public function getHelperSet(): ?HelperSet
  32. {
  33. return $this->helperSet;
  34. }
  35. /**
  36. * Returns the width of a string, using mb_strwidth if it is available.
  37. * The width is how many characters positions the string will use.
  38. */
  39. public static function width(?string $string): int
  40. {
  41. $string ??= '';
  42. if (preg_match('//u', $string)) {
  43. return (new UnicodeString($string))->width(false);
  44. }
  45. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  46. return \strlen($string);
  47. }
  48. return mb_strwidth($string, $encoding);
  49. }
  50. /**
  51. * Returns the length of a string, using mb_strlen if it is available.
  52. * The length is related to how many bytes the string will use.
  53. */
  54. public static function length(?string $string): int
  55. {
  56. $string ??= '';
  57. if (preg_match('//u', $string)) {
  58. return (new UnicodeString($string))->length();
  59. }
  60. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  61. return \strlen($string);
  62. }
  63. return mb_strlen($string, $encoding);
  64. }
  65. /**
  66. * Returns the subset of a string, using mb_substr if it is available.
  67. */
  68. public static function substr(?string $string, int $from, ?int $length = null): string
  69. {
  70. $string ??= '';
  71. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  72. return substr($string, $from, $length);
  73. }
  74. return mb_substr($string, $from, $length, $encoding);
  75. }
  76. /**
  77. * @return string
  78. */
  79. public static function formatTime(int|float $secs, int $precision = 1)
  80. {
  81. $secs = (int) floor($secs);
  82. if (0 === $secs) {
  83. return '< 1 sec';
  84. }
  85. static $timeFormats = [
  86. [1, '1 sec', 'secs'],
  87. [60, '1 min', 'mins'],
  88. [3600, '1 hr', 'hrs'],
  89. [86400, '1 day', 'days'],
  90. ];
  91. $times = [];
  92. foreach ($timeFormats as $index => $format) {
  93. $seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
  94. if (isset($times[$index - $precision])) {
  95. unset($times[$index - $precision]);
  96. }
  97. if (0 === $seconds) {
  98. continue;
  99. }
  100. $unitCount = ($seconds / $format[0]);
  101. $times[$index] = 1 === $unitCount ? $format[1] : $unitCount.' '.$format[2];
  102. if ($secs === $seconds) {
  103. break;
  104. }
  105. $secs -= $seconds;
  106. }
  107. return implode(', ', array_reverse($times));
  108. }
  109. /**
  110. * @return string
  111. */
  112. public static function formatMemory(int $memory)
  113. {
  114. if ($memory >= 1024 * 1024 * 1024) {
  115. return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  116. }
  117. if ($memory >= 1024 * 1024) {
  118. return sprintf('%.1f MiB', $memory / 1024 / 1024);
  119. }
  120. if ($memory >= 1024) {
  121. return sprintf('%d KiB', $memory / 1024);
  122. }
  123. return sprintf('%d B', $memory);
  124. }
  125. /**
  126. * @return 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("/\033\[[^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. }