/
Cli.php
507 lines (398 loc) · 14.7 KB
/
Cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// cli
// class with static methods to generate output for cli
final class Cli extends Root
{
// config
protected static array $config = [
'escape'=>"\033", // caractère d'échappement
'eol'=>PHP_EOL, // caractère de fin de ligne
'foreground'=>[ // code pour couleur avant-plan
'black'=>['0;30',['color'=>'black']],
'darkGray'=>['1;30',['color'=>'darkgray']],
'red'=>['0;31',['color'=>'red']],
'lightRed'=>['1;31',['color'=>'palevioletred']],
'green'=>['0;32',['color'=>'green']],
'lightGreen'=>['1;32',['color'=>'lightgreen']],
'brown'=>['0;33',['color'=>'brown']],
'yellow'=>['1;33',['color'=>'yellow']],
'blue'=>['0;34',['color'=>'blue']],
'lightBlue'=>['1;34',['color'=>'lightblue']],
'purple'=>['0;35',['color'=>'purple']],
'lightPurple'=>['1;35',['color'=>'mediumpurple']],
'cyan'=>['0;36',['color'=>'cyan']],
'lightCyan'=>['1;36',['color'=>'lightcyan']],
'lightGray'=>['0;37',['color'=>'lightgray']],
'white'=>['1;37',['color'=>'white']]],
'background'=>[ // code pour couleur arrière-plan
'black'=>[40,['background-color'=>'black']],
'red'=>[41,['background-color'=>'red']],
'green'=>[42,['background-color'=>'green']],
'yellow'=>[43,['background-color'=>'yellow']],
'blue'=>[44,['background-color'=>'blue']],
'magenta'=>[45,['background-color'=>'magenta']],
'cyan'=>[46,['background-color'=>'cyan']],
'gray'=>[47,['background-color'=>'gray']]],
'style'=>[ // code pour des styles
'bold'=>[1,['font-weight'=>'bold']],
'italic'=>[3,['font-style'=>'italic']],
'underline'=>[4,['text-decoration'=>'underline']]],
'preset'=>[ // règle de styles pour certaines presets
'pos'=>['green','bold','black'],
'neg'=>['white','bold','red'],
'neutral'=>['black',null,'gray']],
'htmlPadding'=>'5px', // valeur utilisé pour le padding de html
'htmlOverload'=>false, // permet d'overload les appels aux méthodes clis avec du html
'opt'=>'--', // caractères pour identifier opt
'cmd'=>'->' // caractères pour identifier cmd
];
// is
// retourne vrai si php est présentement dans cli
final public static function is():bool
{
return Server::isCli();
}
// isHtmlOverload
// retourne vrai si les méthodes cli doivent générer du html
final public static function isHtmlOverload():bool
{
return self::$config['htmlOverload'] === true;
}
// isStdinLine
// retourne vrai si la dernière ligne du stdin est la valeur en argument
final public static function isStdinLine($value,$stdin,bool $lower=false):bool
{
$return = false;
if(is_resource($stdin))
{
$line = self::stdinLine($stdin,$lower);
if(!is_array($value))
$value = (array) $value;
$return = (in_array($line,$value,true));
}
return $return;
}
// callStatic
// méthode qui attrape tous les appels à des méthodes non reconnus
// renvoie vers flushPreset ou vers preset si camelCase a une longueur de deux et commence par get
final public static function __callStatic(string $key,array $arg)
{
$return = null;
$lower = strtolower($key);
$camelCase = null;
if($lower !== $key)
$camelCase = Str::explodeCamelCase($key);
if(is_array($camelCase) && count($camelCase) === 2 && $camelCase[0] === 'get')
{
$key = strtolower($camelCase[1]);
$return = self::preset($key,...$arg);
}
else
$return = self::flushPreset($key,...$arg);
return $return;
}
// flush
// écrit et flush une valeur au cli
final public static function flush($value,?string $foreground=null,?string $style=null,?string $background=null,int $eol=1):void
{
Buffer::flushEcho(self::make($value,$foreground,$style,$background,$eol));
}
// flushPreset
// écrit et flush une valeur au cli en utilisant le style d'un preset
final public static function flushPreset(string $key,$value,int $eol=1):void
{
Buffer::flushEcho(self::preset($key,$value,$eol));
}
// eol
// écrit et flush une ou plusieurs fins de lignes
final public static function eol(int $value=1):void
{
$eol = self::getEol();
while ($value > 0)
{
Buffer::flushEcho($eol);
$value--;
}
}
// make
// cette méthode envoie à makeCli pour générer la string
// possible d'envoyer à makeHtml si la configuration htmlOverload est true
final public static function make($value,?string $foreground=null,?string $style=null,?string $background=null,int $eol=1):string
{
$return = '';
if(self::isHtmlOverload())
$return .= self::makeHtml($value,$foreground,$style,$background,$eol);
else
$return .= self::makeCli($value,$foreground,$style,$background,$eol);
return $return;
}
// makeCli
// génère une version avec couleur d'une valeur à envoyer au cli
// possible de générer des newlines après
final public static function makeCli($value,?string $foreground=null,?string $style=null,?string $background=null,int $eol=1):string
{
$return = '';
$value = self::prepareValue($value);
$foreground = (is_string($foreground))? self::getForegroundColor($foreground,0):null;
$style = (is_string($style))? self::getStyle($style,0):null;
$background = (is_string($background))? self::getBackgroundColor($background,0):null;
$changed = false;
$escape = self::getEscape();
$eolChar = self::getEol();
if(is_string($value) && strlen($value))
{
if(is_string($foreground) || is_int($style) || is_int($background))
{
$changed = true;
if(is_string($foreground))
$return .= $escape.'['.$foreground.'m';
if(is_int($style))
$return .= $escape.'['.$style.'m';
if(is_int($background))
$return .= $escape.'['.$background.'m';
}
$return .= $value;
if($changed === true)
$return .= $escape.'[0m';
}
$return .= Str::eol($eol,$eolChar);
return $return;
}
// makeHtml
// génère une version html couleur d'une valeur
// possible de générer des br après
final public static function makeHtml($value,?string $foreground=null,?string $style=null,?string $background=null,int $eol=1):string
{
$return = '';
$value = self::prepareValue($value);
$foreground = (is_string($foreground))? self::getForegroundColor($foreground,1):null;
$style = (is_string($style))? self::getStyle($style,1):null;
$background = (is_string($background))? self::getBackgroundColor($background,1):null;
$styles = Arr::replace($foreground,$style,$background);
$styles['padding'] = self::$config['htmlPadding'] ?? null;
if(is_string($value) && strlen($value))
{
$value = Html::nl2br($value);
$return .= Html::div($value,['style'=>$styles]);
}
$return .= Html::brs($eol);
return $return;
}
// preset
// génère une string via un preset
final public static function preset(string $key,$value,int $eol=1):string
{
$return = '';
$arg = self::getPreset($key);
if(!empty($arg))
{
$arg[] = $eol;
$return = self::make($value,...$arg);
}
return $return;
}
// getEol
// retourne le caractère de fin de ligne
final public static function getEol():string
{
return self::$config['eol'];
}
// prepareValue
// prépare la valeur à envoyer au cli
// si c'est un tableau, utilise print_r
final public static function prepareValue($value):?string
{
$value = Obj::cast($value);
if(is_array($value))
$value = Debug::printr($value);
return Str::cast($value);
}
// getPreset
// retourne les arguments pour générer un preset
final public static function getPreset(string $value):array
{
return self::$config['preset'][$value] ?? [];
}
// setPreset
// permet d'ajouter un nouveau preset dans la configuration
// value doit être un tableau avec trois valeurs
final public static function setPreset(string $key,array $value):void
{
if(count($value) === 3)
self::$config['preset'][$key] = array_values($value);
}
// getEscape
// retourne le caractère d'échappement
final public static function getEscape():string
{
return self::$config['escape'];
}
// getForegroundColor
// retourne le code de la couleur de texte à utiliser
final public static function getForegroundColor(string $value,int $index=0)
{
return self::$config['foreground'][$value][$index] ?? null;
}
// getBackgroundColor
// retourne le code de la couleur d'arrière-plan à utiliser
final public static function getBackgroundColor(string $value,int $index=0)
{
return self::$config['background'][$value][$index] ?? null;
}
// getStyle
// retourne le code du style à utiliser
final public static function getStyle(string $value,int $index=0)
{
return self::$config['style'][$value][$index] ?? null;
}
// write
// permet d'écrire une valeur au cli
// si c'est un tableau unidimensionnel, la valeur sera implode avec différents séparateurs et la date sera ajouté au début
final public static function write(?string $method,$data,$separator=', ',?array $option=null):void
{
$option = Arr::plus(['timeSeparator'=>'|','firstSeparator'=>':','dateFormat'=>'sql'],$option);
$time = Datetime::format($option['dateFormat']);
$method = (is_string($method))? $method:'flush';
if(is_string($data) && is_string($separator))
$data = [$data];
if(is_array($data))
$data = Arr::clean($data);
if(is_array($data) && Arr::isUni($data) && is_string($separator))
{
$first = array_shift($data);
$data = implode($separator,$data);
$value = $time;
if(is_scalar($first))
{
$value .= ' '.$option['timeSeparator'].' ';
$value .= $first;
if(strlen($data))
{
$value .= $option['firstSeparator'].' ';
$value .= $data;
}
}
}
else
$value = $data;
self::$method($value);
}
// exec
// permet d'éxécuter une commande au terminal
// la commande peut être bloquante ou non
final public static function exec(string $cmd,bool $escape=true,bool $block=true):?string
{
if($escape === true)
$cmd = escapeshellcmd($cmd);
if($block === false)
$cmd .= ' <&- > /dev/null 2>&1 & echo $!';
return exec($cmd);
}
// beep
// permet d'émettre un beep à la console
final public static function beep($amount=null):void
{
if(!is_int($amount))
$amount = 1;
while ($amount > 0)
{
echo "\x07";
$amount--;
}
}
// stdin
// retourne la resource stdin
final public static function stdin(bool $block=true)
{
return Res::stdin(['block'=>$block]);
}
// stdinLine
// retourne la dernière ligne du stdin
// possible de ramener en lowercase
final public static function stdinLine($stdin,bool $lower=false):?string
{
$return = null;
if(is_resource($stdin))
{
$line = fgets($stdin);
if(is_string($line))
{
$return = trim($line);
if($lower === true)
$return = strtolower($return);
}
}
return $return;
}
// parseOpt
// prend un tableau des argv et retourne un tableau avec toutes les options longues
// l'ordre des options n'a pas d'importance, mais il faut que l'entrée commence par --
// une entrée sans égale, prend la valeur true
final public static function parseOpt(string ...$values):array
{
$return = [];
foreach ($values as $key => $value)
{
if(Str::isStart(static::$config['opt'],$value))
{
$value = substr($value,2);
if(strlen($value) && !Str::isStart('=',$value))
{
if(strpos($value,'=') === false)
$value .= '=1';
$x = Str::explodeKeyValue('=',$value,true);
$x = Arr::cast($x);
$return = Arr::replace($return,$x);
}
}
}
return $return;
}
// parseCmd
// permet de parse une string contenant une commande et des opts
// retourne un tableau correctement formatté
final public static function parseCmd(string $value):?array
{
$return = null;
$value = trim($value);
if(Str::isStart(static::$config['cmd'],$value))
{
$value = substr($value,2);
$value = trim($value);
if(strlen($value))
{
$explode = Str::wordExplode($value,2,true,true);
if(!empty($explode[0]))
{
$opt = [];
if(!empty($explode[1]))
$opt = Str::wordExplode($explode[1],null,true,true);
$return = [];
$return['cmd'] = $explode[0];
$return['opt'] = static::parseOpt(...$opt);
}
}
}
return $return;
}
// outputMethod
// retourne la méthode de output cli à utiliser selon une valeur bool ou null
final public static function outputMethod(?bool $value):string
{
return (is_bool($value))? (($value === true) ? 'pos':'neg'):'neutral';
}
// setHtmlOverload
// active ou désactive le overload du html
final public static function setHtmlOverload(bool $value):void
{
self::$config['htmlOverload'] = $value;
}
}
?>