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@0: * Sets the helper set associated with this helper. Chris@0: * Chris@0: * @param HelperSet $helperSet A HelperSet instance Chris@0: */ Chris@0: public function setHelperSet(HelperSet $helperSet = null) Chris@0: { Chris@0: $this->helperSet = $helperSet; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the helper set associated with this helper. Chris@0: * Chris@0: * @return HelperSet|null 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@0: return strlen($string); Chris@0: } Chris@0: Chris@0: return mb_strwidth($string, $encoding); Chris@0: } Chris@0: Chris@0: public static function formatTime($secs) Chris@0: { Chris@0: static $timeFormats = array( Chris@0: array(0, '< 1 sec'), Chris@0: array(1, '1 sec'), Chris@0: array(2, 'secs', 1), Chris@0: array(60, '1 min'), Chris@0: array(120, 'mins', 60), Chris@0: array(3600, '1 hr'), Chris@0: array(7200, 'hrs', 3600), Chris@0: array(86400, '1 day'), Chris@0: array(172800, 'days', 86400), Chris@0: ); 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@0: || $index == count($timeFormats) - 1 Chris@0: ) { Chris@0: 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: }