You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.0 KiB

3 years ago
  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. * @deprecated since 5.3
  39. *
  40. * @return int The length of the string
  41. */
  42. public static function strlen(?string $string)
  43. {
  44. trigger_deprecation('symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::width() or Helper::length() instead.', __METHOD__);
  45. return self::width($string);
  46. }
  47. /**
  48. * Returns the width of a string, using mb_strwidth if it is available.
  49. * The width is how many characters positions the string will use.
  50. */
  51. public static function width(?string $string): int
  52. {
  53. $string ?? $string = '';
  54. if (preg_match('//u', $string)) {
  55. return (new UnicodeString($string))->width(false);
  56. }
  57. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  58. return \strlen($string);
  59. }
  60. return mb_strwidth($string, $encoding);
  61. }
  62. /**
  63. * Returns the length of a string, using mb_strlen if it is available.
  64. * The length is related to how many bytes the string will use.
  65. */
  66. public static function length(?string $string): int
  67. {
  68. $string ?? $string = '';
  69. if (preg_match('//u', $string)) {
  70. return (new UnicodeString($string))->length();
  71. }
  72. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  73. return \strlen($string);
  74. }
  75. return mb_strlen($string, $encoding);
  76. }
  77. /**
  78. * Returns the subset of a string, using mb_substr if it is available.
  79. *
  80. * @return string The string subset
  81. */
  82. public static function substr(?string $string, int $from, int $length = null)
  83. {
  84. $string ?? $string = '';
  85. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  86. return substr($string, $from, $length);
  87. }
  88. return mb_substr($string, $from, $length, $encoding);
  89. }
  90. public static function formatTime($secs)
  91. {
  92. static $timeFormats = [
  93. [0, '< 1 sec'],
  94. [1, '1 sec'],
  95. [2, 'secs', 1],
  96. [60, '1 min'],
  97. [120, 'mins', 60],
  98. [3600, '1 hr'],
  99. [7200, 'hrs', 3600],
  100. [86400, '1 day'],
  101. [172800, 'days', 86400],
  102. ];
  103. foreach ($timeFormats as $index => $format) {
  104. if ($secs >= $format[0]) {
  105. if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
  106. || $index == \count($timeFormats) - 1
  107. ) {
  108. if (2 == \count($format)) {
  109. return $format[1];
  110. }
  111. return floor($secs / $format[2]).' '.$format[1];
  112. }
  113. }
  114. }
  115. }
  116. public static function formatMemory(int $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. * @deprecated since 5.3
  131. */
  132. public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string)
  133. {
  134. trigger_deprecation('symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::removeDecoration() instead.', __METHOD__);
  135. return self::width(self::removeDecoration($formatter, $string));
  136. }
  137. public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)
  138. {
  139. $isDecorated = $formatter->isDecorated();
  140. $formatter->setDecorated(false);
  141. // remove <...> formatting
  142. $string = $formatter->format($string ?? '');
  143. // remove already formatted characters
  144. $string = preg_replace("/\033\[[^m]*m/", '', $string);
  145. $formatter->setDecorated($isDecorated);
  146. return $string;
  147. }
  148. }