Helper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  13. * Helper is the base class for all helper classes.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. abstract class Helper implements HelperInterface
  18. {
  19. protected $helperSet = null;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function setHelperSet(HelperSet $helperSet = null)
  24. {
  25. $this->helperSet = $helperSet;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getHelperSet()
  31. {
  32. return $this->helperSet;
  33. }
  34. /**
  35. * Returns the length of a string, using mb_strwidth if it is available.
  36. *
  37. * @return int The length of the string
  38. */
  39. public static function strlen(?string $string)
  40. {
  41. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  42. return \strlen($string);
  43. }
  44. return mb_strwidth($string, $encoding);
  45. }
  46. /**
  47. * Returns the subset of a string, using mb_substr if it is available.
  48. *
  49. * @return string The string subset
  50. */
  51. public static function substr(string $string, int $from, int $length = null)
  52. {
  53. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  54. return substr($string, $from, $length);
  55. }
  56. return mb_substr($string, $from, $length, $encoding);
  57. }
  58. public static function formatTime($secs)
  59. {
  60. static $timeFormats = [
  61. [0, '< 1 sec'],
  62. [1, '1 sec'],
  63. [2, 'secs', 1],
  64. [60, '1 min'],
  65. [120, 'mins', 60],
  66. [3600, '1 hr'],
  67. [7200, 'hrs', 3600],
  68. [86400, '1 day'],
  69. [172800, 'days', 86400],
  70. ];
  71. foreach ($timeFormats as $index => $format) {
  72. if ($secs >= $format[0]) {
  73. if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
  74. || $index == \count($timeFormats) - 1
  75. ) {
  76. if (2 == \count($format)) {
  77. return $format[1];
  78. }
  79. return floor($secs / $format[2]).' '.$format[1];
  80. }
  81. }
  82. }
  83. }
  84. public static function formatMemory(int $memory)
  85. {
  86. if ($memory >= 1024 * 1024 * 1024) {
  87. return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  88. }
  89. if ($memory >= 1024 * 1024) {
  90. return sprintf('%.1f MiB', $memory / 1024 / 1024);
  91. }
  92. if ($memory >= 1024) {
  93. return sprintf('%d KiB', $memory / 1024);
  94. }
  95. return sprintf('%d B', $memory);
  96. }
  97. public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
  98. {
  99. return self::strlen(self::removeDecoration($formatter, $string));
  100. }
  101. public static function removeDecoration(OutputFormatterInterface $formatter, $string)
  102. {
  103. $isDecorated = $formatter->isDecorated();
  104. $formatter->setDecorated(false);
  105. // remove <...> formatting
  106. $string = $formatter->format($string);
  107. // remove already formatted characters
  108. $string = preg_replace("/\033\[[^m]*m/", '', $string);
  109. $formatter->setDecorated($isDecorated);
  110. return $string;
  111. }
  112. }