Helper.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 = null;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setHelperSet(HelperSet $helperSet = null)
  25. {
  26. $this->helperSet = $helperSet;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getHelperSet()
  32. {
  33. return $this->helperSet;
  34. }
  35. /**
  36. * Returns the length of a string, using mb_strwidth if it is available.
  37. *
  38. * @return int The length of the string
  39. */
  40. public static function strlen(?string $string)
  41. {
  42. return self::width($string);
  43. }
  44. /**
  45. * Returns the width of a string, using mb_strwidth if it is available.
  46. * The width is how many characters positions the string will use.
  47. *
  48. * @internal in Symfony 5.2
  49. */
  50. public static function width(?string $string): int
  51. {
  52. $string ?? $string = '';
  53. if (preg_match('//u', $string)) {
  54. return (new UnicodeString($string))->width(false);
  55. }
  56. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  57. return \strlen($string);
  58. }
  59. return mb_strwidth($string, $encoding);
  60. }
  61. /**
  62. * Returns the length of a string, using mb_strlen if it is available.
  63. * The length is related to how many bytes the string will use.
  64. *
  65. * @internal in Symfony 5.2
  66. */
  67. public static function length(?string $string): int
  68. {
  69. $string ?? $string = '';
  70. if (preg_match('//u', $string)) {
  71. return (new UnicodeString($string))->length();
  72. }
  73. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  74. return \strlen($string);
  75. }
  76. return mb_strlen($string, $encoding);
  77. }
  78. /**
  79. * Returns the subset of a string, using mb_substr if it is available.
  80. *
  81. * @return string The string subset
  82. */
  83. public static function substr(?string $string, int $from, int $length = null)
  84. {
  85. $string ?? $string = '';
  86. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  87. return substr($string, $from, $length);
  88. }
  89. return mb_substr($string, $from, $length, $encoding);
  90. }
  91. public static function formatTime($secs)
  92. {
  93. static $timeFormats = [
  94. [0, '< 1 sec'],
  95. [1, '1 sec'],
  96. [2, 'secs', 1],
  97. [60, '1 min'],
  98. [120, 'mins', 60],
  99. [3600, '1 hr'],
  100. [7200, 'hrs', 3600],
  101. [86400, '1 day'],
  102. [172800, 'days', 86400],
  103. ];
  104. foreach ($timeFormats as $index => $format) {
  105. if ($secs >= $format[0]) {
  106. if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
  107. || $index == \count($timeFormats) - 1
  108. ) {
  109. if (2 == \count($format)) {
  110. return $format[1];
  111. }
  112. return floor($secs / $format[2]).' '.$format[1];
  113. }
  114. }
  115. }
  116. }
  117. public static function formatMemory(int $memory)
  118. {
  119. if ($memory >= 1024 * 1024 * 1024) {
  120. return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  121. }
  122. if ($memory >= 1024 * 1024) {
  123. return sprintf('%.1f MiB', $memory / 1024 / 1024);
  124. }
  125. if ($memory >= 1024) {
  126. return sprintf('%d KiB', $memory / 1024);
  127. }
  128. return sprintf('%d B', $memory);
  129. }
  130. public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string)
  131. {
  132. return self::width(self::removeDecoration($formatter, $string));
  133. }
  134. public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)
  135. {
  136. $isDecorated = $formatter->isDecorated();
  137. $formatter->setDecorated(false);
  138. // remove <...> formatting
  139. $string = $formatter->format($string ?? '');
  140. // remove already formatted characters
  141. $string = preg_replace("/\033\[[^m]*m/", '', $string);
  142. $formatter->setDecorated($isDecorated);
  143. return $string;
  144. }
  145. }