Helper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Sets the helper set associated with this helper.
  22. *
  23. * @param HelperSet $helperSet A HelperSet instance
  24. */
  25. public function setHelperSet(HelperSet $helperSet = null)
  26. {
  27. $this->helperSet = $helperSet;
  28. }
  29. /**
  30. * Gets the helper set associated with this helper.
  31. *
  32. * @return HelperSet|null
  33. */
  34. public function getHelperSet()
  35. {
  36. return $this->helperSet;
  37. }
  38. /**
  39. * Returns the length of a string, using mb_strwidth if it is available.
  40. *
  41. * @param string $string The string to check its length
  42. *
  43. * @return int The length of the string
  44. */
  45. public static function strlen($string)
  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 subset of a string, using mb_substr if it is available.
  54. *
  55. * @param string $string String to subset
  56. * @param int $from Start offset
  57. * @param int|null $length Length to read
  58. *
  59. * @return string The string subset
  60. */
  61. public static function substr($string, $from, $length = null)
  62. {
  63. if (false === $encoding = mb_detect_encoding($string, null, true)) {
  64. return substr($string, $from, $length);
  65. }
  66. return mb_substr($string, $from, $length, $encoding);
  67. }
  68. public static function formatTime($secs)
  69. {
  70. static $timeFormats = array(
  71. array(0, '< 1 sec'),
  72. array(1, '1 sec'),
  73. array(2, 'secs', 1),
  74. array(60, '1 min'),
  75. array(120, 'mins', 60),
  76. array(3600, '1 hr'),
  77. array(7200, 'hrs', 3600),
  78. array(86400, '1 day'),
  79. array(172800, 'days', 86400),
  80. );
  81. foreach ($timeFormats as $index => $format) {
  82. if ($secs >= $format[0]) {
  83. if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
  84. || $index == count($timeFormats) - 1
  85. ) {
  86. if (2 == count($format)) {
  87. return $format[1];
  88. }
  89. return floor($secs / $format[2]).' '.$format[1];
  90. }
  91. }
  92. }
  93. }
  94. public static function formatMemory($memory)
  95. {
  96. if ($memory >= 1024 * 1024 * 1024) {
  97. return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  98. }
  99. if ($memory >= 1024 * 1024) {
  100. return sprintf('%.1f MiB', $memory / 1024 / 1024);
  101. }
  102. if ($memory >= 1024) {
  103. return sprintf('%d KiB', $memory / 1024);
  104. }
  105. return sprintf('%d B', $memory);
  106. }
  107. public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
  108. {
  109. return self::strlen(self::removeDecoration($formatter, $string));
  110. }
  111. public static function removeDecoration(OutputFormatterInterface $formatter, $string)
  112. {
  113. $isDecorated = $formatter->isDecorated();
  114. $formatter->setDecorated(false);
  115. // remove <...> formatting
  116. $string = $formatter->format($string);
  117. // remove already formatted characters
  118. $string = preg_replace("/\033\[[^m]*m/", '', $string);
  119. $formatter->setDecorated($isDecorated);
  120. return $string;
  121. }
  122. }