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