Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
CLI | |
0.00% |
0 / 49 |
|
0.00% |
0 / 12 |
870 | |
0.00% |
0 / 1 |
connectDBWithRetries | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
getOpt | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
156 | |||
printData | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
p | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
h1 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
h2 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
h3 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
h | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
warning | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
success | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
printColored | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | // TODO unit test |
5 | |
6 | class CLI { |
7 | private const foreground = [ |
8 | "Black" => "30", |
9 | "Red" => "31", |
10 | "Green" => "32", |
11 | "Brown" => "33", |
12 | "Blue" => "34", |
13 | "Magenta" => "35", |
14 | "Cyan" => "36", |
15 | "Grey" => "37", |
16 | ]; |
17 | |
18 | private const background = [ |
19 | "Black" => "40", |
20 | "Red" => "41", |
21 | "Green" => "42", |
22 | "Yellow" => "43", |
23 | "Blue" => "44", |
24 | "Magenta" => "45", |
25 | "Cyan" => "46", |
26 | "Grey" => "47", |
27 | ]; |
28 | |
29 | static function connectDBWithRetries(int $retries = 5): void { |
30 | while ($retries--) { |
31 | try { |
32 | CLI::p("Database Connection attempt."); |
33 | DB::connect(); |
34 | CLI::success("Database Connection successful!"); |
35 | return; |
36 | } catch (Throwable) { |
37 | CLI::warning("Database Connection failed! Retry: $retries attempts left."); |
38 | usleep(20 * 1000000); // give database container time to come up |
39 | } |
40 | } |
41 | |
42 | // CLI::printData(SystemConfig); |
43 | throw new Exception("Database connection failed."); |
44 | } |
45 | |
46 | // PHP's getopt is bogus: it can not handle empty strings as params properly |
47 | static function getOpt(): array { |
48 | $result = []; |
49 | $params = $GLOBALS['argv']; |
50 | $skipNext = true; |
51 | |
52 | foreach ($params as $i => $param) { |
53 | if ($skipNext) { |
54 | $skipNext = false; |
55 | continue; |
56 | } |
57 | |
58 | if ($param[0] == '-') { |
59 | $paramName = substr($param, 1); |
60 | $value = null; |
61 | |
62 | if ($paramName[0] == '-') { // long-opt (--<param>) |
63 | $paramName = substr($paramName, 1); |
64 | if (str_contains($param, '=')) { // value specified inline (--<param>=<value>) |
65 | list($paramName, $value) = explode('=', substr($param, 2), 2); |
66 | } |
67 | } |
68 | |
69 | if (!$paramName) { |
70 | $result[] = '--'; |
71 | continue; |
72 | } |
73 | |
74 | if (is_numeric($paramName)) { |
75 | $paramName = '_' . $paramName; |
76 | } |
77 | |
78 | if (is_null($value)) { |
79 | $nextParam = $params[$i + 1] ?? true; |
80 | $nextIsValue = (is_string($nextParam) and (($nextParam === "") or ($nextParam[0] !== "-"))); |
81 | $value = $nextIsValue ? $nextParam : true; |
82 | $skipNext = $nextIsValue; |
83 | } |
84 | |
85 | $result[$paramName] = $value; |
86 | |
87 | } else { |
88 | $result[] = $param; |
89 | } |
90 | } |
91 | return $result; |
92 | } |
93 | |
94 | static function printData(DataCollection $dataCollection): void { |
95 | echo "\n " . get_class($dataCollection); |
96 | foreach ($dataCollection->jsonSerialize() as $key => $value) { |
97 | echo "\n - $key: " . (strstr('password', $key) ? Password::shorten($value) : $value); |
98 | } |
99 | } |
100 | |
101 | static function p(mixed $text): void { |
102 | echo "\n" . print_r($text, true); |
103 | } |
104 | |
105 | static function h1(mixed $text): void { |
106 | CLI::printColored(print_r($text, true), "Blue", "Grey", true); |
107 | } |
108 | |
109 | static function h2(mixed $text): void { |
110 | CLI::printColored(print_r($text, true), "Black", "Grey", true); |
111 | } |
112 | |
113 | static function h3(mixed $text): void { |
114 | CLI::printColored(print_r($text, true), "Brown", "Grey", true); |
115 | } |
116 | |
117 | static function h(mixed $text): void { |
118 | CLI::printColored(print_r($text, true), "Grey", "Black", true); |
119 | } |
120 | |
121 | static function warning(mixed $text): void { |
122 | CLI::printColored(print_r($text, true), "Brown"); |
123 | } |
124 | |
125 | static function error(mixed $text): void { |
126 | CLI::printColored(print_r($text, true), "Red", null, true); |
127 | } |
128 | |
129 | static function success(mixed $text): void { |
130 | CLI::printColored(print_r($text, true), "Green"); |
131 | } |
132 | |
133 | static private function printColored(mixed $text, string $fg, string $bg = null, bool $bold = false): void { |
134 | $colorString = ($bold ? '1' : '0') . ';' . CLI::foreground[$fg] . ($bg ? ';' . CLI::background[$bg] : ''); |
135 | echo "\n\e[{$colorString}m$text\e[0m"; |
136 | } |
137 | } |