This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Helper/Helper.php

120 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
/**
* Helper is the base class for all helper classes.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Helper implements HelperInterface
{
protected $helperSet = null;
/**
* Sets the helper set associated with this helper.
*
* @param HelperSet $helperSet A HelperSet instance
*/
public function setHelperSet(HelperSet $helperSet = null)
{
$this->helperSet = $helperSet;
}
/**
* Gets the helper set associated with this helper.
*
* @return HelperSet A HelperSet instance
*/
public function getHelperSet()
{
return $this->helperSet;
}
2012-12-15 15:08:23 +00:00
/**
* Returns the length of a string, using mb_strwidth if it is available.
2012-12-15 15:08:23 +00:00
*
* @param string $string The string to check its length
*
2014-11-30 13:33:44 +00:00
* @return int The length of the string
2012-12-15 15:08:23 +00:00
*/
public static function strlen($string)
2012-12-15 15:08:23 +00:00
{
2015-10-14 15:40:43 +01:00
if (false === $encoding = mb_detect_encoding($string, null, true)) {
2012-12-15 15:08:23 +00:00
return strlen($string);
}
return mb_strwidth($string, $encoding);
2012-12-15 15:08:23 +00:00
}
public static function formatTime($secs)
{
static $timeFormats = array(
array(0, '< 1 sec'),
2016-04-04 08:13:10 +01:00
array(1, '1 sec'),
array(2, 'secs', 1),
array(60, '1 min'),
2016-04-04 08:13:10 +01:00
array(120, 'mins', 60),
array(3600, '1 hr'),
array(7200, 'hrs', 3600),
array(86400, '1 day'),
array(172800, 'days', 86400),
);
2016-04-04 08:13:10 +01:00
foreach ($timeFormats as $index => $format) {
if ($secs >= $format[0]) {
2016-04-04 08:13:10 +01:00
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
|| $index == count($timeFormats) - 1
) {
if (2 == count($format)) {
return $format[1];
}
return floor($secs / $format[2]).' '.$format[1];
}
}
}
}
public static function formatMemory($memory)
{
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
}
if ($memory >= 1024 * 1024) {
return sprintf('%.1f MiB', $memory / 1024 / 1024);
}
if ($memory >= 1024) {
return sprintf('%d KiB', $memory / 1024);
}
return sprintf('%d B', $memory);
}
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
{
$isDecorated = $formatter->isDecorated();
$formatter->setDecorated(false);
// remove <...> formatting
$string = $formatter->format($string);
// remove already formatted characters
$string = preg_replace("/\033\[[^m]*m/", '', $string);
$formatter->setDecorated($isDecorated);
return self::strlen($string);
}
}