2021-05-10 00:23:30 +00: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 .
*/
2021-07-30 07:40:46 +00:00
namespace RectorPrefix20210730\Symfony\Component\Console\Helper ;
2021-05-10 00:23:30 +00:00
2021-07-30 07:40:46 +00:00
use RectorPrefix20210730\Symfony\Component\Console\Formatter\OutputFormatterInterface ;
use RectorPrefix20210730\Symfony\Component\String\UnicodeString ;
2021-05-10 00:23:30 +00:00
/**
* Helper is the base class for all helper classes .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
2021-07-30 07:40:46 +00:00
abstract class Helper implements \RectorPrefix20210730\Symfony\Component\Console\Helper\HelperInterface
2021-05-10 00:23:30 +00:00
{
protected $helperSet = null ;
/**
* { @ inheritdoc }
2021-07-05 22:50:18 +00:00
* @ param \Symfony\Component\Console\Helper\HelperSet | null $helperSet
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function setHelperSet ( $helperSet = null )
2021-05-10 00:23:30 +00:00
{
$this -> helperSet = $helperSet ;
}
/**
* { @ inheritdoc }
*/
public function getHelperSet ()
{
return $this -> helperSet ;
}
/**
* Returns the length of a string , using mb_strwidth if it is available .
*
2021-06-01 12:39:02 +00:00
* @ deprecated since 5.3
*
2021-05-10 00:23:30 +00:00
* @ return int The length of the string
2021-07-05 22:50:18 +00:00
* @ param string | null $string
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public static function strlen ( $string )
2021-05-10 00:23:30 +00:00
{
2021-06-01 12:39:02 +00:00
trigger_deprecation ( 'symfony/console' , '5.3' , 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::width() or Helper::length() instead.' , __METHOD__ );
2021-05-10 00:23:30 +00:00
return self :: width ( $string );
}
/**
* Returns the width of a string , using mb_strwidth if it is available .
* The width is how many characters positions the string will use .
2021-07-05 22:50:18 +00:00
* @ param string | null $string
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public static function width ( $string ) : int
2021-05-10 00:23:30 +00:00
{
$string ? ? ( $string = '' );
if ( \preg_match ( '//u' , $string )) {
2021-07-30 07:40:46 +00:00
return ( new \RectorPrefix20210730\Symfony\Component\String\UnicodeString ( $string )) -> width ( \false );
2021-05-10 00:23:30 +00:00
}
if ( \false === ( $encoding = \mb_detect_encoding ( $string , null , \true ))) {
return \strlen ( $string );
}
return \mb_strwidth ( $string , $encoding );
}
/**
* Returns the length of a string , using mb_strlen if it is available .
* The length is related to how many bytes the string will use .
2021-07-05 22:50:18 +00:00
* @ param string | null $string
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public static function length ( $string ) : int
2021-05-10 00:23:30 +00:00
{
$string ? ? ( $string = '' );
if ( \preg_match ( '//u' , $string )) {
2021-07-30 07:40:46 +00:00
return ( new \RectorPrefix20210730\Symfony\Component\String\UnicodeString ( $string )) -> length ();
2021-05-10 00:23:30 +00:00
}
if ( \false === ( $encoding = \mb_detect_encoding ( $string , null , \true ))) {
return \strlen ( $string );
}
return \mb_strlen ( $string , $encoding );
}
/**
* Returns the subset of a string , using mb_substr if it is available .
*
* @ return string The string subset
2021-07-05 22:50:18 +00:00
* @ param string | null $string
* @ param int $from
* @ param int | null $length
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public static function substr ( $string , $from , $length = null )
2021-05-10 00:23:30 +00:00
{
$string ? ? ( $string = '' );
if ( \false === ( $encoding = \mb_detect_encoding ( $string , null , \true ))) {
return \substr ( $string , $from , $length );
}
return \mb_substr ( $string , $from , $length , $encoding );
}
public static function formatTime ( $secs )
{
static $timeFormats = [[ 0 , '< 1 sec' ], [ 1 , '1 sec' ], [ 2 , 'secs' , 1 ], [ 60 , '1 min' ], [ 120 , 'mins' , 60 ], [ 3600 , '1 hr' ], [ 7200 , 'hrs' , 3600 ], [ 86400 , '1 day' ], [ 172800 , 'days' , 86400 ]];
foreach ( $timeFormats as $index => $format ) {
if ( $secs >= $format [ 0 ]) {
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 ];
}
}
}
}
2021-07-05 22:50:18 +00:00
/**
* @ param int $memory
*/
public static function formatMemory ( $memory )
2021-05-10 00:23:30 +00:00
{
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 );
}
2021-06-01 12:39:02 +00:00
/**
* @ deprecated since 5.3
2021-07-05 22:50:18 +00:00
* @ param \Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter
* @ param string | null $string
2021-06-01 12:39:02 +00:00
*/
2021-07-05 22:50:18 +00:00
public static function strlenWithoutDecoration ( $formatter , $string )
2021-05-10 00:23:30 +00:00
{
2021-06-01 12:39:02 +00:00
trigger_deprecation ( 'symfony/console' , '5.3' , 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::removeDecoration() instead.' , __METHOD__ );
2021-05-10 00:23:30 +00:00
return self :: width ( self :: removeDecoration ( $formatter , $string ));
}
2021-07-05 22:50:18 +00:00
/**
* @ param \Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter
* @ param string | null $string
*/
public static function removeDecoration ( $formatter , $string )
2021-05-10 00:23:30 +00:00
{
$isDecorated = $formatter -> isDecorated ();
$formatter -> setDecorated ( \false );
// remove <...> formatting
$string = $formatter -> format ( $string ? ? '' );
// remove already formatted characters
$string = \preg_replace ( " / \33 \\ [[^m]*m/ " , '' , $string );
$formatter -> setDecorated ( $isDecorated );
return $string ;
}
}