-
Notifications
You must be signed in to change notification settings - Fork 16
/
REPL.php
152 lines (118 loc) · 3.21 KB
/
REPL.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
<?php
namespace WP_CLI\Shell;
use WP_CLI;
class REPL {
private $prompt;
private $history_file;
public function __construct( $prompt ) {
$this->prompt = $prompt;
$this->set_history_file();
}
public function start() {
while ( true ) {
$line = $this->prompt();
if ( '' === $line ) {
continue;
}
$line = rtrim( $line, ';' ) . ';';
if ( self::starts_with( self::non_expressions(), $line ) ) {
ob_start();
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
eval( $line );
$out = ob_get_clean();
if ( 0 < strlen( $out ) ) {
$out = rtrim( $out, "\n" ) . "\n";
}
fwrite( STDOUT, $out );
} else {
if ( ! self::starts_with( 'return', $line ) ) {
$line = 'return ' . $line;
}
// Write directly to STDOUT, to sidestep any output buffers created by plugins
ob_start();
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
$evl = eval( $line );
$out = ob_get_clean();
if ( 0 < strlen( $out ) ) {
echo rtrim( $out, "\n" ) . "\n";
}
echo '=> ';
var_dump( $evl );
fwrite( STDOUT, ob_get_clean() );
}
}
}
private static function non_expressions() {
return implode(
'|',
array(
'echo',
'global',
'unset',
'function',
'while',
'for',
'foreach',
'if',
'switch',
'include',
'include\_once',
'require',
'require\_once',
)
);
}
private function prompt() {
$full_line = false;
$done = false;
do {
$prompt = ( ! $done && false !== $full_line ) ? '--> ' : $this->prompt;
$fp = popen( self::create_prompt_cmd( $prompt, $this->history_file ), 'r' );
$line = fgets( $fp );
pclose( $fp );
if ( ! $line ) {
break;
}
$line = rtrim( $line, "\n" );
if ( $line && '\\' === $line[ strlen( $line ) - 1 ] ) {
$line = substr( $line, 0, -1 );
} else {
$done = true;
}
$full_line .= $line;
} while ( ! $done );
if ( false === $full_line ) {
return 'exit';
}
return $full_line;
}
private static function create_prompt_cmd( $prompt, $history_path ) {
$prompt = escapeshellarg( $prompt );
$history_path = escapeshellarg( $history_path );
if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) {
$shell_binary = getenv( 'WP_CLI_CUSTOM_SHELL' );
} else {
$shell_binary = '/bin/bash';
}
if ( ! is_file( $shell_binary ) || ! is_readable( $shell_binary ) ) {
WP_CLI::error( "The shell binary '{$shell_binary}' is not valid. You can override the shell to be used through the WP_CLI_CUSTOM_SHELL environment variable." );
}
$shell_binary = escapeshellarg( $shell_binary );
$cmd = 'set -f; '
. "history -r {$history_path}; "
. 'LINE=""; '
. "read -re -p {$prompt} LINE; "
. '[ $? -eq 0 ] || exit; '
. 'history -s "$LINE"; '
. "history -w {$history_path}; "
. 'echo $LINE; ';
return "{$shell_binary} -c " . escapeshellarg( $cmd );
}
private function set_history_file() {
$data = getcwd() . get_current_user();
$this->history_file = \WP_CLI\Utils\get_temp_dir() . 'wp-cli-history-' . md5( $data );
}
private static function starts_with( $tokens, $line ) {
return preg_match( "/^($tokens)[\(\s]+/", $line );
}
}