1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Forked version that is simplified to work without Composer. |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/caendesilva/php-console |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
interface ConsoleContract |
13
|
|
|
{ |
14
|
|
|
public function write(string $string): self; |
15
|
|
|
public function line(string $message = ''): self; |
16
|
|
|
public function format(string $message): self; |
17
|
|
|
public function newline(int $count = 1): self; |
18
|
|
|
|
19
|
|
|
public function info(string $message): self; |
20
|
|
|
public function warn(string $message): self; |
21
|
|
|
public function error(string $message): self; |
22
|
|
|
public function debug(string $message): self; |
23
|
|
|
public function debugComment(string $message): self; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
class Console implements ConsoleContract |
27
|
|
|
{ |
28
|
|
|
use ColoredOutput; |
29
|
|
|
|
30
|
|
|
public function write(string $string): self |
31
|
|
|
{ |
32
|
|
|
file_put_contents('php://stdout', $string); |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function line(string $message = ''): self |
38
|
|
|
{ |
39
|
|
|
$this->write($message . PHP_EOL); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function newline(int $count = 1): self |
45
|
|
|
{ |
46
|
|
|
$this->line(str_repeat(PHP_EOL, $count - 1)); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function info(string $message): self |
52
|
|
|
{ |
53
|
|
|
$this->line(sprintf('%s%s%s: %s', |
54
|
|
|
$this->gray('['), |
55
|
|
|
$this->green('Info'), |
56
|
|
|
$this->gray(']'), |
57
|
|
|
$message) |
58
|
|
|
); |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function warn(string $message): self |
63
|
|
|
{ |
64
|
|
|
$this->line(sprintf('%s%s%s: %s', |
65
|
|
|
$this->gray('['), |
66
|
|
|
$this->yellow('Warning'), |
67
|
|
|
$this->gray(']'), |
68
|
|
|
$message) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function error(string $message): self |
75
|
|
|
{ |
76
|
|
|
$this->line(sprintf('%s%s%s: %s', |
77
|
|
|
$this->gray('['), |
78
|
|
|
$this->red('Error'), |
79
|
|
|
$this->gray(']'), |
80
|
|
|
$message) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function debug(string $message): self |
87
|
|
|
{ |
88
|
|
|
$this->line(sprintf('%s%s%s: %s', |
89
|
|
|
$this->gray('['), |
90
|
|
|
$this->lightGray('Debug'), |
91
|
|
|
$this->gray(']'), |
92
|
|
|
$message) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function debugComment(string $message): self |
99
|
|
|
{ |
100
|
|
|
$this->line(sprintf('%s%s', |
101
|
|
|
$this->gray(' > '), |
102
|
|
|
$this->lightGray($message)) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Automatically format a log message. |
110
|
|
|
* Example: 'Info: Hello World!' becomes '[Info]: Hello World!' with colors. |
111
|
|
|
* @internal May be removed in the future. |
112
|
|
|
*/ |
113
|
|
|
public function format(string $message): self |
114
|
|
|
{ |
115
|
|
|
$this->line((new FormatsAnsiString($message))->getOutputString()); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
trait ColoredOutput |
122
|
|
|
{ |
123
|
|
|
protected function ansi(string $string, string $color): string |
124
|
|
|
{ |
125
|
|
|
return "\033[" . $color . 'm' . $string . "\033[0m"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function black(string $string): string |
129
|
|
|
{ |
130
|
|
|
return $this->ansi($string, '30'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function red(string $string): string |
134
|
|
|
{ |
135
|
|
|
return $this->ansi($string, '31'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function green(string $string): string |
139
|
|
|
{ |
140
|
|
|
return $this->ansi($string, '32'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function yellow(string $string): string |
144
|
|
|
{ |
145
|
|
|
return $this->ansi($string, '33'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function blue(string $string): string |
149
|
|
|
{ |
150
|
|
|
return $this->ansi($string, '34'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function magenta(string $string): string |
154
|
|
|
{ |
155
|
|
|
return $this->ansi($string, '35'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function cyan(string $string): string |
159
|
|
|
{ |
160
|
|
|
return $this->ansi($string, '36'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function white(string $string): string |
164
|
|
|
{ |
165
|
|
|
return $this->ansi($string, '37'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function gray(string $string): string |
169
|
|
|
{ |
170
|
|
|
return $this->ansi($string, '90'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function lightGray(string $string): string |
174
|
|
|
{ |
175
|
|
|
return $this->ansi($string, '37'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @internal May be removed in the future. |
182
|
|
|
* Ported from https://github.com/hydephp/realtime-compiler/blob/1.x/src/Server.php |
183
|
|
|
*/ |
184
|
|
|
class FormatsAnsiString |
185
|
|
|
{ |
186
|
|
|
private string $inputString; |
187
|
|
|
private string $outputString; |
188
|
|
|
|
189
|
|
|
public function __construct(string $inputString) |
190
|
|
|
{ |
191
|
|
|
$this->inputString = $inputString; |
192
|
|
|
|
193
|
|
|
$this->outputString = $this->execute(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
private function execute(): string |
197
|
|
|
{ |
198
|
|
|
if ($this->canTokenize()) { |
199
|
|
|
return $this->tokenize(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->inputString; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getOutputString(): string |
206
|
|
|
{ |
207
|
|
|
return $this->outputString; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
private function canTokenize(): bool |
211
|
|
|
{ |
212
|
|
|
// Check if the string contains a word followed by ': ' and then a string |
213
|
|
|
return preg_match('/^[a-zA-Z0-9_]+: /', $this->inputString); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
private function tokenize(): string |
217
|
|
|
{ |
218
|
|
|
$tokens = explode(': ', $this->inputString); |
219
|
|
|
|
220
|
|
|
// Implode the rest of the tokens |
221
|
|
|
$value = implode(': ', array_slice($tokens, 1)); |
222
|
|
|
|
223
|
|
|
$this->outputString = static::lightGray('[') . static::main($tokens[0]) . static::lightGray(']'); |
224
|
|
|
|
225
|
|
|
if (isset($tokens[1])) { |
226
|
|
|
$this->outputString .= ': ' . $value; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->outputString; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
private static function lightGray(string $string): string |
233
|
|
|
{ |
234
|
|
|
return "\033[0;90m" . $string . "\033[0m"; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** The primary color */ |
238
|
|
|
private static function main(string $string): string |
239
|
|
|
{ |
240
|
|
|
// If token string is an action, use a less bright (visible) color |
241
|
|
|
if (in_array($string, [ |
242
|
|
|
'SourceFileFinder', |
243
|
|
|
'CompilesSourceFile', |
244
|
|
|
'FormatsAnsiString', |
245
|
|
|
])) { |
246
|
|
|
return $string; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Gold (yellow) color |
250
|
|
|
return "\033[0;33m" . $string . "\033[0m"; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public static function get(string $string): string |
254
|
|
|
{ |
255
|
|
|
return (new self($string))->getOutputString(); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|