-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththink
110 lines (90 loc) · 2.45 KB
/
think
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
<?php
use App\Models\Sample;
if (php_sapi_name() !== 'cli') {
exit(1);
}
function input() {
return fgets(STDIN);
}
function output($val) {
fputs(STDOUT, $val);
}
function colour($text = '', $colour = null) {
$styles = array(
'green' => "\033[0;32m%s\033[0m",
'red' => "\033[31;31m%s\033[0m",
'yellow' => "\033[33;33m%s\033[0m",
'blue' => "\033[33;34m%s\033[0m",
);
return sprintf(isset($styles[$colour]) ? $styles[$colour] : "%s", $text);
}
function cmd($cmd, $input) {
$input = trim($input);
return substr($input, 0, strlen($cmd)) === $cmd;
}
function snip($cmd, $input) {
return substr($input, strlen($cmd));
}
function asi($input) {
if (trim($input) === '') {
return $input;
}
return rtrim($input, "\n;") . ';';
}
output('PHP Version ' . colour(PHP_VERSION, 'green') . PHP_EOL);
output('quit: Exit the REPL' . PHP_EOL);
output('dump: Perform a vardump()' . PHP_EOL);
output('printr: Perform a print_r()' . PHP_EOL);
output('exec: Execute an external program' . PHP_EOL);
output('>>>: Start a heredoc of PHP code' . PHP_EOL);
output('<<<: End a heredoc of PHP code' . PHP_EOL);
output('!!!: Discard a heredoc of PHP code' . PHP_EOL);
output(PHP_EOL);
$buffer = '';
$buffering = false;
while (true) {
output(colour('> ', $buffering ? 'blue' : 'yellow'));
$input = input();
if (cmd('quit', $input)) {
exit;
}
if (cmd('exec', $input)) {
exec(snip('exec', $input), $result);
$result = trim(implode("\n", $result));
if (! empty($result)) {
output($result . PHP_EOL);
}
continue;
}
if (cmd('dump', $input)) {
$input = 'var_dump(' . snip('dump', $input) . ');';
} elseif (cmd('printr', $input)) {
$input = 'print_r(' . snip('printr', $input) . ');';
}
if (cmd('>>>', $input)) {
$buffering = true;
$input = snip('>>>', $input);
} elseif (cmd('<<<', $input)) {
if (! $buffering) {
continue;
}
$buffering = false;
$input = snip('<<<', $input);
} elseif (cmd('!!!', $input)) {
$buffering = false;
$buffer = '';
continue;
}
if ($buffering) {
$buffer .= $input;
continue;
}
if (! empty($buffer)) {
$input = $buffer;
$buffer = '';
} else {
$input = asi($input) . PHP_EOL;
}
eval($input);
output(PHP_EOL);
}