Helper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 A HelperSet instance
  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 (!function_exists('mb_strwidth')) {
  48. return strlen($string);
  49. }
  50. if (false === $encoding = mb_detect_encoding($string)) {
  51. return strlen($string);
  52. }
  53. return mb_strwidth($string, $encoding);
  54. }
  55. public static function formatTime($secs)
  56. {
  57. static $timeFormats = array(
  58. array(0, '< 1 sec'),
  59. array(2, '1 sec'),
  60. array(59, 'secs', 1),
  61. array(60, '1 min'),
  62. array(3600, 'mins', 60),
  63. array(5400, '1 hr'),
  64. array(86400, 'hrs', 3600),
  65. array(129600, '1 day'),
  66. array(604800, 'days', 86400),
  67. );
  68. foreach ($timeFormats as $format) {
  69. if ($secs >= $format[0]) {
  70. continue;
  71. }
  72. if (2 == count($format)) {
  73. return $format[1];
  74. }
  75. return ceil($secs / $format[2]).' '.$format[1];
  76. }
  77. }
  78. public static function formatMemory($memory)
  79. {
  80. if ($memory >= 1024 * 1024 * 1024) {
  81. return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  82. }
  83. if ($memory >= 1024 * 1024) {
  84. return sprintf('%.1f MiB', $memory / 1024 / 1024);
  85. }
  86. if ($memory >= 1024) {
  87. return sprintf('%d KiB', $memory / 1024);
  88. }
  89. return sprintf('%d B', $memory);
  90. }
  91. public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
  92. {
  93. $isDecorated = $formatter->isDecorated();
  94. $formatter->setDecorated(false);
  95. // remove <...> formatting
  96. $string = $formatter->format($string);
  97. // remove already formatted characters
  98. $string = preg_replace("/\033\[[^m]*m/", '', $string);
  99. $formatter->setDecorated($isDecorated);
  100. return self::strlen($string);
  101. }
  102. }