|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace terminal; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* class for terminal/cli/bash output |
|
7
|
|
|
*/ |
|
8
|
|
|
final class Terminal { |
|
9
|
|
|
private $buffer = []; |
|
10
|
|
|
private $argv; |
|
11
|
|
|
private $argc; |
|
12
|
|
|
private $arguments = []; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
global $argv; |
|
17
|
|
|
global $argc; |
|
18
|
|
|
|
|
19
|
|
|
$this->argv = $argv; |
|
20
|
|
|
$this->argc = $argc; |
|
21
|
|
|
|
|
22
|
|
|
$this->parseArguments(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function addBuffer(string $buffer) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->buffer[] = $buffer; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getBuffer(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return implode(PHP_EOL, $this->buffer); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function echo(string $string, bool $to_buffer = false, bool $return = false) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($return) { |
|
38
|
|
|
return $string; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($to_buffer) { |
|
42
|
|
|
$this->addBuffer($string); |
|
43
|
|
|
} else { |
|
44
|
|
|
echo $string . PHP_EOL; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function diff(string $string, bool $to_buffer = false, bool $return = false) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$result_string = ''; |
|
51
|
|
|
$separator = "\r\n"; |
|
52
|
|
|
$line = strtok($string, $separator); |
|
53
|
|
|
|
|
54
|
|
|
while ($line !== false) { |
|
55
|
|
|
if (strpos($line, '-') === 0) { |
|
56
|
|
|
$result_string .= $this->error($line, false, true); |
|
57
|
|
|
} else if (strpos($line, '+') === 0) { |
|
58
|
|
|
$result_string .= $this->success($line, false, true); |
|
59
|
|
|
} else { |
|
60
|
|
|
$result_string .= $line; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$result_string .= PHP_EOL; |
|
64
|
|
|
|
|
65
|
|
|
$line = strtok($separator); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($to_buffer) { |
|
69
|
|
|
$this->setBuffer($result_string); |
|
|
|
|
|
|
70
|
|
|
} else { |
|
71
|
|
|
echo $result_string; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function info(string $string, bool $to_buffer = false, bool $return = false) |
|
76
|
|
|
{ |
|
77
|
|
|
$output = "\e[46m" . $string . "\e[0m"; |
|
78
|
|
|
|
|
79
|
|
|
if ($return) { |
|
80
|
|
|
return $output; |
|
81
|
|
|
} else { |
|
82
|
|
|
$this->echo($output, $to_buffer); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function success(string $string, bool $to_buffer = false, bool $return = false) |
|
87
|
|
|
{ |
|
88
|
|
|
$output = "\e[32m" . $string . "\e[0m"; |
|
89
|
|
|
|
|
90
|
|
|
if ($return) { |
|
91
|
|
|
return $output; |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->echo($output, $to_buffer); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function warning(string $string, bool $to_buffer = false, bool $return = false) |
|
98
|
|
|
{ |
|
99
|
|
|
$output = "\e[43m" . $string . "\e[0m"; |
|
100
|
|
|
|
|
101
|
|
|
if ($return) { |
|
102
|
|
|
return $output; |
|
103
|
|
|
} else { |
|
104
|
|
|
$this->echo($output, $to_buffer); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function error(string $string, bool $to_buffer = false, bool $return = false) |
|
109
|
|
|
{ |
|
110
|
|
|
$output = "\e[1;31m" . $string . "\e[0m"; |
|
111
|
|
|
|
|
112
|
|
|
if ($return) { |
|
113
|
|
|
return $output; |
|
114
|
|
|
} else { |
|
115
|
|
|
$this->echo($output, $to_buffer); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function forceOutput() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->echo( |
|
122
|
|
|
$this->getBuffer() |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Requests user for confirm his action |
|
128
|
|
|
* |
|
129
|
|
|
* @param callable $success_action |
|
130
|
|
|
* @param callable $cancel_action |
|
131
|
|
|
* @param string $question |
|
132
|
|
|
* @param string $confirmation_word |
|
133
|
|
|
*/ |
|
134
|
|
|
public function confirm( |
|
135
|
|
|
callable $success_action, |
|
136
|
|
|
callable $cancel_action = null, |
|
137
|
|
|
string $question = "Are you sure?\n Type 'Y' to continue: ", |
|
138
|
|
|
string $confirmation_word = 'Y' |
|
139
|
|
|
): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->warning($question); |
|
142
|
|
|
$handle = fopen('php://stdin', 'r'); |
|
143
|
|
|
$line = fgets($handle); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
if(trim($line) != $confirmation_word) { |
|
146
|
|
|
$this->warning('ABORTING'); |
|
147
|
|
|
call_user_func($cancel_action); |
|
148
|
|
|
exit; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
fclose($handle); |
|
|
|
|
|
|
152
|
|
|
call_user_func($success_action); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Requests config item from user |
|
157
|
|
|
*/ |
|
158
|
|
|
public function requestSetting( |
|
159
|
|
|
string $name, |
|
160
|
|
|
callable $result_action, |
|
161
|
|
|
$default = '' |
|
162
|
|
|
): void |
|
163
|
|
|
{ |
|
164
|
|
|
$this->echo("Please, set up $name"); |
|
165
|
|
|
$handle = fopen('php://stdin', 'r'); |
|
166
|
|
|
$line = fgets($handle); |
|
|
|
|
|
|
167
|
|
|
$result = trim($line); |
|
168
|
|
|
|
|
169
|
|
|
call_user_func($result_action, $result ?: $default); |
|
170
|
|
|
fclose($handle); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function expectParams( |
|
174
|
|
|
array $opts, |
|
|
|
|
|
|
175
|
|
|
callable $result_action |
|
|
|
|
|
|
176
|
|
|
) |
|
177
|
|
|
{ |
|
178
|
|
|
// $options = getopt('', ['addon.id:']); |
|
179
|
|
|
// print_r($options); |
|
180
|
|
|
// die(); |
|
181
|
|
|
// $options = getopt(implode('', array_keys($opts)), $opts); |
|
182
|
|
|
// print_r($options); |
|
183
|
|
|
// array_walk($options, function($option) use ($result_action) { |
|
184
|
|
|
// echo 2; |
|
185
|
|
|
// call_user_func($result_action, $option); |
|
186
|
|
|
// }); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Set arguments |
|
191
|
|
|
* |
|
192
|
|
|
* @param array $arguments |
|
193
|
|
|
* |
|
194
|
|
|
* @return void |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setArguments(array $arguments): void |
|
197
|
|
|
{ |
|
198
|
|
|
$this->arguments = $arguments; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get arguments |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getArguments(): array |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->arguments; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Parse arguments from terminal to array |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
private function parseArguments(): void |
|
217
|
|
|
{ |
|
218
|
|
|
$arguments = arguments(implode(' ', $this->argv)); |
|
219
|
|
|
$controller = @$this->argv[1] ?: ''; |
|
220
|
|
|
list($generator, $command) = array_pad(explode('/', $controller), 2, ''); |
|
221
|
|
|
$generator = (1 === preg_match('/^[\w]+/ui', $generator)) ? $generator : ''; |
|
222
|
|
|
|
|
223
|
|
|
$arguments['generator'] = to_camel_case($generator); |
|
224
|
|
|
$arguments['command'] = $command; |
|
225
|
|
|
$this->setArguments($arguments); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Cheks is terminal at autocomplete mode |
|
230
|
|
|
* |
|
231
|
|
|
* @return bool |
|
232
|
|
|
*/ |
|
233
|
|
|
public function isAutocomplete() |
|
234
|
|
|
{ |
|
235
|
|
|
return ( |
|
236
|
|
|
isset($this->arguments['autocomplete']) |
|
237
|
|
|
&& $this->arguments['autocomplete'] === 'y' |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Echo autocompletes to terminal |
|
243
|
|
|
* |
|
244
|
|
|
* @param array |
|
245
|
|
|
* |
|
246
|
|
|
* @return Terminal |
|
247
|
|
|
*/ |
|
248
|
|
|
public function autocomplete(array $variants) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->echo(implode(' ', $variants)) . ' '; |
|
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
return $this; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Closes program execution |
|
257
|
|
|
*/ |
|
258
|
|
|
public function exit() |
|
259
|
|
|
{ |
|
260
|
|
|
exit; |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.