1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Helpers; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Helpers for cli applications |
9
|
|
|
*/ |
10
|
|
|
class Cli |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @const ERR_COLOR_NOT_AVAILABLE Exception code if the color is not |
14
|
|
|
* available. |
15
|
|
|
*/ |
16
|
|
|
const ERR_COLOR_NOT_AVAILABLE = 1201001; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @const ERR_STYLE_NOT_AVAILABLE Exception code if the style is not |
20
|
|
|
* available. |
21
|
|
|
*/ |
22
|
|
|
const ERR_STYLE_NOT_AVAILABLE = 1201002; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @const FLUSH_AUTO Value to use on $flushMethod property to automaticaly |
26
|
|
|
* call the method ob_flush into function displayMsg and displayMsgNoNL |
27
|
|
|
*/ |
28
|
|
|
const FLUSH_AUTO = 'auto'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @const FLUSH_MANUAL Value to use on $flushMethod property to NOT |
32
|
|
|
* automaticaly call the method ob_flush into function displayMsg |
33
|
|
|
* and displayMsgNL |
34
|
|
|
*/ |
35
|
|
|
const FLUSH_MANUAL = 'manual'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string $callObFlush (default: self::FLUSH_AUTO) Define if the |
39
|
|
|
* method ob_flush is called or not into the method displayMsg |
40
|
|
|
* and displayMsgNL |
41
|
|
|
*/ |
42
|
|
|
public static $callObFlush = self::FLUSH_AUTO; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Display a message in the console without a line break |
46
|
|
|
* If only the first parameter is passed, the colors will be those |
47
|
|
|
* currently used in the console |
48
|
|
|
* |
49
|
|
|
* @param string $msg Message to display |
50
|
|
|
* @param string $colorTxt (default "white") Text color |
51
|
|
|
* @param string $colorBg (default "black") Background color |
52
|
|
|
* @param string $style (default "normal") Style for the message (bold etc) |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public static function displayMsg( |
57
|
|
|
$msg, |
58
|
|
|
$colorTxt = 'white', |
59
|
|
|
$colorBg = 'black', |
60
|
|
|
$style = 'normal' |
61
|
|
|
) { |
62
|
|
|
if (func_num_args() === 1) { |
63
|
|
|
echo $msg; |
64
|
|
|
|
65
|
|
|
if (self::$callObFlush === self::FLUSH_AUTO) { |
66
|
|
|
ob_flush(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
//Get colors values |
73
|
|
|
$styleNum = self::styleForShell($style); |
74
|
|
|
$colorTxtNum = self::colorForShell($colorTxt, 'txt'); |
75
|
|
|
$colorBgNum = self::colorForShell($colorBg, 'bg'); |
76
|
|
|
|
77
|
|
|
echo "\033[".$styleNum.";".$colorBgNum.";".$colorTxtNum."m" |
78
|
|
|
.$msg |
79
|
|
|
."\033[0m"; |
80
|
|
|
|
81
|
|
|
if (self::$callObFlush === self::FLUSH_AUTO) { |
82
|
|
|
ob_flush(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Display a message in the console with a line break |
88
|
|
|
* If only the first parameter is passed, the colors will be those |
89
|
|
|
* currently used in the console |
90
|
|
|
* |
91
|
|
|
* @param string $msg Message to display |
92
|
|
|
* @param string $colorTxt (default "white") Text color |
93
|
|
|
* @param string $colorBg (default "black") Background color |
94
|
|
|
* @param string $style (default "normal") Style for the message (bold etc) |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public static function displayMsgNL( |
99
|
|
|
$msg, |
100
|
|
|
$colorTxt = 'white', |
101
|
|
|
$colorBg = 'black', |
102
|
|
|
$style = 'normal' |
103
|
|
|
) { |
104
|
|
|
$currentClass = get_called_class(); |
105
|
|
|
|
106
|
|
|
if (func_num_args() === 1) { |
107
|
|
|
$currentClass::displayMsg($msg."\n"); |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$currentClass::displayMsg($msg."\n", $colorTxt, $colorBg, $style); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Convert text color to shell value |
116
|
|
|
* |
117
|
|
|
* @param string $color The human color text |
118
|
|
|
* @param string $type ("txt"|"bg") If the color is for text or background |
119
|
|
|
* |
120
|
|
|
* @return integer |
121
|
|
|
* |
122
|
|
|
* @throws Exception If the color is not available |
123
|
|
|
*/ |
124
|
|
|
protected static function colorForShell($color, $type) |
125
|
|
|
{ |
126
|
|
|
$colorList = [ |
127
|
|
|
'black' => 0, |
128
|
|
|
'red' => 1, |
129
|
|
|
'green' => 2, |
130
|
|
|
'yellow' => 3, |
131
|
|
|
'blue' => 4, |
132
|
|
|
'magenta' => 5, |
133
|
|
|
'cyan' => 6, |
134
|
|
|
'white' => 7 |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
if (!isset($colorList[$color])) { |
138
|
|
|
throw new Exception( |
139
|
|
|
'Color '.$color.' is not available.', |
140
|
|
|
self::ERR_COLOR_NOT_AVAILABLE |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
//Text color |
145
|
|
|
if ($type === 'txt') { |
146
|
|
|
return $colorList[$color] + 30; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
//Background color |
150
|
|
|
return $colorList[$color] + 40; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Convert a human style text to shell value |
155
|
|
|
* |
156
|
|
|
* @param string $style The style value |
157
|
|
|
* |
158
|
|
|
* @return integer |
159
|
|
|
* |
160
|
|
|
* @throws Exception If the style is not available |
161
|
|
|
*/ |
162
|
|
|
protected static function styleForShell($style) |
163
|
|
|
{ |
164
|
|
|
$styleList = [ |
165
|
|
|
'normal' => 0, |
166
|
|
|
'bold' => 1, |
167
|
|
|
'not-bold' => 21, |
168
|
|
|
'underline' => 4, |
169
|
|
|
'not-underline' => 24, |
170
|
|
|
'blink' => 5, |
171
|
|
|
'not-blink' => 25, |
172
|
|
|
'reverse' => 7, |
173
|
|
|
'not-reverse' => 27 |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
if (!isset($styleList[$style])) { |
177
|
|
|
throw new Exception( |
178
|
|
|
'Style '.$style.' is not available.', |
179
|
|
|
self::ERR_STYLE_NOT_AVAILABLE |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $styleList[$style]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Override the PHP function getopt to always use the short "f:". |
188
|
|
|
* If "f:" is not present, nothing is returned (#79) |
189
|
|
|
* |
190
|
|
|
* @link http://php.net/manual/en/function.getopt.php |
191
|
|
|
* |
192
|
|
|
* @param string $options Each character in this string will be used as |
193
|
|
|
* option characters and matched against options passed to the script |
194
|
|
|
* starting with a single hyphen (-). For example, an option string "x" |
195
|
|
|
* recognizes an option -x. Only a-z, A-Z and 0-9 are allowed. |
196
|
|
|
* @param array $longopts (default []) An array of options. |
197
|
|
|
* Each element in this array will be used as option strings and matched |
198
|
|
|
* against options passed to the script starting with two hyphens (--). |
199
|
|
|
* For example, an longopts element "opt" recognizes an option --opt. |
200
|
|
|
* |
201
|
|
|
* @return boolean|array |
202
|
|
|
*/ |
203
|
|
|
public static function getopt($options , $longopts = []) |
204
|
|
|
{ |
205
|
|
|
if (strpos($options, 'f:') === false) { |
206
|
|
|
$options .= 'f:'; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return \getopt($options, $longopts); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|