Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Helper;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Helper is the base class for all helper classes.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class Helper implements HelperInterface
|
Chris@0
|
22 {
|
Chris@0
|
23 protected $helperSet = null;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@14
|
26 * {@inheritdoc}
|
Chris@0
|
27 */
|
Chris@0
|
28 public function setHelperSet(HelperSet $helperSet = null)
|
Chris@0
|
29 {
|
Chris@0
|
30 $this->helperSet = $helperSet;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@14
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 public function getHelperSet()
|
Chris@0
|
37 {
|
Chris@0
|
38 return $this->helperSet;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Returns the length of a string, using mb_strwidth if it is available.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param string $string The string to check its length
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return int The length of the string
|
Chris@0
|
47 */
|
Chris@0
|
48 public static function strlen($string)
|
Chris@0
|
49 {
|
Chris@0
|
50 if (false === $encoding = mb_detect_encoding($string, null, true)) {
|
Chris@17
|
51 return \strlen($string);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 return mb_strwidth($string, $encoding);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@14
|
57 /**
|
Chris@14
|
58 * Returns the subset of a string, using mb_substr if it is available.
|
Chris@14
|
59 *
|
Chris@14
|
60 * @param string $string String to subset
|
Chris@14
|
61 * @param int $from Start offset
|
Chris@14
|
62 * @param int|null $length Length to read
|
Chris@14
|
63 *
|
Chris@14
|
64 * @return string The string subset
|
Chris@14
|
65 */
|
Chris@14
|
66 public static function substr($string, $from, $length = null)
|
Chris@14
|
67 {
|
Chris@14
|
68 if (false === $encoding = mb_detect_encoding($string, null, true)) {
|
Chris@14
|
69 return substr($string, $from, $length);
|
Chris@14
|
70 }
|
Chris@14
|
71
|
Chris@14
|
72 return mb_substr($string, $from, $length, $encoding);
|
Chris@14
|
73 }
|
Chris@14
|
74
|
Chris@0
|
75 public static function formatTime($secs)
|
Chris@0
|
76 {
|
Chris@17
|
77 static $timeFormats = [
|
Chris@17
|
78 [0, '< 1 sec'],
|
Chris@17
|
79 [1, '1 sec'],
|
Chris@17
|
80 [2, 'secs', 1],
|
Chris@17
|
81 [60, '1 min'],
|
Chris@17
|
82 [120, 'mins', 60],
|
Chris@17
|
83 [3600, '1 hr'],
|
Chris@17
|
84 [7200, 'hrs', 3600],
|
Chris@17
|
85 [86400, '1 day'],
|
Chris@17
|
86 [172800, 'days', 86400],
|
Chris@17
|
87 ];
|
Chris@0
|
88
|
Chris@0
|
89 foreach ($timeFormats as $index => $format) {
|
Chris@0
|
90 if ($secs >= $format[0]) {
|
Chris@0
|
91 if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
|
Chris@17
|
92 || $index == \count($timeFormats) - 1
|
Chris@0
|
93 ) {
|
Chris@17
|
94 if (2 == \count($format)) {
|
Chris@0
|
95 return $format[1];
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 return floor($secs / $format[2]).' '.$format[1];
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 public static function formatMemory($memory)
|
Chris@0
|
105 {
|
Chris@0
|
106 if ($memory >= 1024 * 1024 * 1024) {
|
Chris@0
|
107 return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 if ($memory >= 1024 * 1024) {
|
Chris@0
|
111 return sprintf('%.1f MiB', $memory / 1024 / 1024);
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 if ($memory >= 1024) {
|
Chris@0
|
115 return sprintf('%d KiB', $memory / 1024);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return sprintf('%d B', $memory);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
|
Chris@0
|
122 {
|
Chris@0
|
123 return self::strlen(self::removeDecoration($formatter, $string));
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 public static function removeDecoration(OutputFormatterInterface $formatter, $string)
|
Chris@0
|
127 {
|
Chris@0
|
128 $isDecorated = $formatter->isDecorated();
|
Chris@0
|
129 $formatter->setDecorated(false);
|
Chris@0
|
130 // remove <...> formatting
|
Chris@0
|
131 $string = $formatter->format($string);
|
Chris@0
|
132 // remove already formatted characters
|
Chris@0
|
133 $string = preg_replace("/\033\[[^m]*m/", '', $string);
|
Chris@0
|
134 $formatter->setDecorated($isDecorated);
|
Chris@0
|
135
|
Chris@0
|
136 return $string;
|
Chris@0
|
137 }
|
Chris@0
|
138 }
|