Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Helper; Chris@0: Chris@0: use Symfony\Component\Console\Formatter\OutputFormatterInterface; Chris@0: Chris@0: /** Chris@0: * Helper is the base class for all helper classes. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class Helper implements HelperInterface Chris@0: { Chris@0: protected $helperSet = null; Chris@0: Chris@0: /** Chris@14: * {@inheritdoc} Chris@0: */ Chris@0: public function setHelperSet(HelperSet $helperSet = null) Chris@0: { Chris@0: $this->helperSet = $helperSet; Chris@0: } Chris@0: Chris@0: /** Chris@14: * {@inheritdoc} Chris@0: */ Chris@0: public function getHelperSet() Chris@0: { Chris@0: return $this->helperSet; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the length of a string, using mb_strwidth if it is available. Chris@0: * Chris@0: * @param string $string The string to check its length Chris@0: * Chris@0: * @return int The length of the string Chris@0: */ Chris@0: public static function strlen($string) Chris@0: { Chris@0: if (false === $encoding = mb_detect_encoding($string, null, true)) { Chris@17: return \strlen($string); Chris@0: } Chris@0: Chris@0: return mb_strwidth($string, $encoding); Chris@0: } Chris@0: Chris@14: /** Chris@14: * Returns the subset of a string, using mb_substr if it is available. Chris@14: * Chris@14: * @param string $string String to subset Chris@14: * @param int $from Start offset Chris@14: * @param int|null $length Length to read Chris@14: * Chris@14: * @return string The string subset Chris@14: */ Chris@14: public static function substr($string, $from, $length = null) Chris@14: { Chris@14: if (false === $encoding = mb_detect_encoding($string, null, true)) { Chris@14: return substr($string, $from, $length); Chris@14: } Chris@14: Chris@14: return mb_substr($string, $from, $length, $encoding); Chris@14: } Chris@14: Chris@0: public static function formatTime($secs) Chris@0: { Chris@17: static $timeFormats = [ Chris@17: [0, '< 1 sec'], Chris@17: [1, '1 sec'], Chris@17: [2, 'secs', 1], Chris@17: [60, '1 min'], Chris@17: [120, 'mins', 60], Chris@17: [3600, '1 hr'], Chris@17: [7200, 'hrs', 3600], Chris@17: [86400, '1 day'], Chris@17: [172800, 'days', 86400], Chris@17: ]; Chris@0: Chris@0: foreach ($timeFormats as $index => $format) { Chris@0: if ($secs >= $format[0]) { Chris@0: if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0]) Chris@17: || $index == \count($timeFormats) - 1 Chris@0: ) { Chris@17: if (2 == \count($format)) { Chris@0: return $format[1]; Chris@0: } Chris@0: Chris@0: return floor($secs / $format[2]).' '.$format[1]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: public static function formatMemory($memory) Chris@0: { Chris@0: if ($memory >= 1024 * 1024 * 1024) { Chris@0: return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024); Chris@0: } Chris@0: Chris@0: if ($memory >= 1024 * 1024) { Chris@0: return sprintf('%.1f MiB', $memory / 1024 / 1024); Chris@0: } Chris@0: Chris@0: if ($memory >= 1024) { Chris@0: return sprintf('%d KiB', $memory / 1024); Chris@0: } Chris@0: Chris@0: return sprintf('%d B', $memory); Chris@0: } Chris@0: Chris@0: public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string) Chris@0: { Chris@0: return self::strlen(self::removeDecoration($formatter, $string)); Chris@0: } Chris@0: Chris@0: public static function removeDecoration(OutputFormatterInterface $formatter, $string) Chris@0: { Chris@0: $isDecorated = $formatter->isDecorated(); Chris@0: $formatter->setDecorated(false); Chris@0: // remove <...> formatting Chris@0: $string = $formatter->format($string); Chris@0: // remove already formatted characters Chris@0: $string = preg_replace("/\033\[[^m]*m/", '', $string); Chris@0: $formatter->setDecorated($isDecorated); Chris@0: Chris@0: return $string; Chris@0: } Chris@0: }