2015-08-17 17:00:26 -07:00
< ? 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 .
*
2017-02-02 16:28:38 -08:00
* @ return HelperSet | null
2015-08-17 17:00:26 -07:00
*/
public function getHelperSet ()
{
return $this -> helperSet ;
}
/**
* Returns the length of a string , using mb_strwidth if it is available .
*
* @ param string $string The string to check its length
*
* @ return int The length of the string
*/
public static function strlen ( $string )
{
2016-04-20 09:56:34 -07:00
if ( false === $encoding = mb_detect_encoding ( $string , null , true )) {
2015-08-17 17:00:26 -07:00
return strlen ( $string );
}
return mb_strwidth ( $string , $encoding );
}
public static function formatTime ( $secs )
{
static $timeFormats = array (
array ( 0 , '< 1 sec' ),
2017-02-02 16:28:38 -08:00
array ( 1 , '1 sec' ),
array ( 2 , 'secs' , 1 ),
2015-08-17 17:00:26 -07:00
array ( 60 , '1 min' ),
2017-02-02 16:28:38 -08:00
array ( 120 , 'mins' , 60 ),
array ( 3600 , '1 hr' ),
array ( 7200 , 'hrs' , 3600 ),
array ( 86400 , '1 day' ),
array ( 172800 , 'days' , 86400 ),
2015-08-17 17:00:26 -07:00
);
2017-02-02 16:28:38 -08:00
foreach ( $timeFormats as $index => $format ) {
2015-08-17 17:00:26 -07:00
if ( $secs >= $format [ 0 ]) {
2017-02-02 16:28:38 -08: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 ];
}
2015-08-17 17:00:26 -07:00
}
}
}
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 )
2017-04-13 15:53:35 +01:00
{
return self :: strlen ( self :: removeDecoration ( $formatter , $string ));
}
public static function removeDecoration ( OutputFormatterInterface $formatter , $string )
2015-08-17 17:00:26 -07:00
{
$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 );
2017-04-13 15:53:35 +01:00
return $string ;
2015-08-17 17:00:26 -07:00
}
}