TYPO3  7.6
vendor/symfony/console/Helper/Helper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Helper;
13 
15 
21 abstract class Helper implements HelperInterface
22 {
23  protected $helperSet = null;
24 
30  public function setHelperSet(HelperSet $helperSet = null)
31  {
32  $this->helperSet = $helperSet;
33  }
34 
40  public function getHelperSet()
41  {
42  return $this->helperSet;
43  }
44 
52  public static function strlen($string)
53  {
54  if (!function_exists('mb_strwidth')) {
55  return strlen($string);
56  }
57 
58  if (false === $encoding = mb_detect_encoding($string)) {
59  return strlen($string);
60  }
61 
62  return mb_strwidth($string, $encoding);
63  }
64 
65  public static function formatTime($secs)
66  {
67  static $timeFormats = array(
68  array(0, '< 1 sec'),
69  array(2, '1 sec'),
70  array(59, 'secs', 1),
71  array(60, '1 min'),
72  array(3600, 'mins', 60),
73  array(5400, '1 hr'),
74  array(86400, 'hrs', 3600),
75  array(129600, '1 day'),
76  array(604800, 'days', 86400),
77  );
78 
79  foreach ($timeFormats as $format) {
80  if ($secs >= $format[0]) {
81  continue;
82  }
83 
84  if (2 == count($format)) {
85  return $format[1];
86  }
87 
88  return ceil($secs / $format[2]).' '.$format[1];
89  }
90  }
91 
92  public static function formatMemory($memory)
93  {
94  if ($memory >= 1024 * 1024 * 1024) {
95  return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
96  }
97 
98  if ($memory >= 1024 * 1024) {
99  return sprintf('%.1f MiB', $memory / 1024 / 1024);
100  }
101 
102  if ($memory >= 1024) {
103  return sprintf('%d KiB', $memory / 1024);
104  }
105 
106  return sprintf('%d B', $memory);
107  }
108 
109  public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
110  {
111  $isDecorated = $formatter->isDecorated();
112  $formatter->setDecorated(false);
113  // remove <...> formatting
114  $string = $formatter->format($string);
115  // remove already formatted characters
116  $string = preg_replace("/\033\[[^m]*m/", '', $string);
117  $formatter->setDecorated($isDecorated);
118 
119  return self::strlen($string);
120  }
121 }