-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathREPL.php
More file actions
334 lines (283 loc) · 8.84 KB
/
REPL.php
File metadata and controls
334 lines (283 loc) · 8.84 KB
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
<?php
namespace WP_CLI\Shell;
use WP_CLI;
class REPL {
private $prompt;
private $history_file;
/** @var bool Whether to suppress automatic output. */
private $quiet;
private $watch_path;
private $watch_mtime;
const EXIT_CODE_RESTART = 10;
/**
* @param string $prompt Prompt to display.
* @param bool $quiet Whether to suppress automatic output.
*/
public function __construct( $prompt, $quiet = false ) {
$this->prompt = $prompt;
$this->quiet = $quiet;
$this->set_history_file();
}
/**
* Set a path to watch for changes.
*
* @param string $path Path to watch for changes.
*/
public function set_watch_path( $path ) {
$this->watch_path = $path;
$this->watch_mtime = $this->get_recursive_mtime( $path );
}
public function start() {
while ( true ) {
// Check for file changes if watching
if ( $this->watch_path && $this->has_changes() ) {
WP_CLI::log( "Detected changes in {$this->watch_path}, restarting shell..." );
return self::EXIT_CODE_RESTART;
}
$__repl_input_line = $this->prompt();
if ( '' === $__repl_input_line ) {
continue;
}
// Check for special exit command
if ( 'exit' === trim( $__repl_input_line ) ) {
return 0;
}
// Check for special restart command
if ( 'restart' === trim( $__repl_input_line ) ) {
WP_CLI::log( 'Restarting shell...' );
return self::EXIT_CODE_RESTART;
}
$__repl_input_line = rtrim( $__repl_input_line, ';' ) . ';';
if ( self::starts_with( self::non_expressions(), $__repl_input_line ) ) {
ob_start();
try {
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
eval( $__repl_input_line );
} catch ( \Throwable $e ) {
// Display the error message but continue the session
fwrite( STDERR, get_class( $e ) . ': ' . $e->getMessage() . "\n" );
}
$__repl_output = (string) ob_get_clean();
if ( 0 < strlen( $__repl_output ) ) {
$__repl_output = rtrim( $__repl_output, "\n" ) . "\n";
}
fwrite( STDOUT, $__repl_output );
} else {
if ( ! self::starts_with( 'return', $__repl_input_line ) ) {
$__repl_input_line = 'return ' . $__repl_input_line;
}
// Write directly to STDOUT, to sidestep any output buffers created by plugins
ob_start();
$__repl_eval_had_error = false;
try {
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
$__repl_eval_result = eval( $__repl_input_line );
} catch ( \Throwable $e ) {
// Display the error message but continue the session
fwrite( STDERR, get_class( $e ) . ': ' . $e->getMessage() . "\n" );
$__repl_eval_had_error = true;
$__repl_eval_result = null;
}
$__repl_output = (string) ob_get_clean();
if ( 0 < strlen( $__repl_output ) ) {
echo rtrim( $__repl_output, "\n" ) . "\n";
}
if ( $__repl_eval_had_error ) {
continue;
}
ob_start();
if ( ! $this->quiet ) {
echo '=> ';
var_dump( $__repl_eval_result );
}
fwrite( STDOUT, (string) ob_get_clean() );
}
}
}
private static function non_expressions() {
return implode(
'|',
array(
'echo',
'global',
'unset',
'function',
'do',
'while',
'for',
'foreach',
'if',
'switch',
'include',
'include\_once',
'require',
'require\_once',
)
);
}
private function prompt() {
$full_line = false;
$done = false;
do {
// @phpstan-ignore booleanNot.alwaysTrue
$prompt = ( ! $done && false !== $full_line ) ? '--> ' : $this->prompt;
$fp = popen( self::create_prompt_cmd( $prompt, $this->history_file ), 'r' );
$line = $fp ? fgets( $fp ) : '';
if ( $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 = (string) getenv( 'WP_CLI_CUSTOM_SHELL' );
} elseif ( is_file( '/bin/bash' ) && is_readable( '/bin/bash' ) ) {
// Prefer /bin/bash when available since we use bash-specific commands.
$shell_binary = '/bin/bash';
} elseif ( getenv( 'SHELL' ) && self::is_supported_shell( (string) getenv( 'SHELL' ) ) ) {
// Use SHELL as fallback if it's a supported shell (bash or ksh).
$shell_binary = (string) getenv( 'SHELL' );
} else {
// Final fallback for systems without /bin/bash.
$shell_binary = '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." );
}
$is_ksh = self::is_ksh_shell( $shell_binary );
$shell_binary = escapeshellarg( $shell_binary );
if ( $is_ksh ) {
// ksh does not support bash-specific history commands or `read -e`/`read -p`.
// Use POSIX-compatible read and print the prompt via printf to stderr.
$cmd = 'set -f; '
. 'LINE=""; '
. "printf %s {$prompt} >&2; "
. 'IFS= read -r LINE || exit; '
. 'printf \'%s\n\' "$LINE"; ';
} else {
$cmd = 'set -f; '
. "history -r {$history_path}; "
. 'LINE=""; '
. "read -re -p {$prompt} LINE; "
. '[ $? -eq 0 ] || exit; '
. 'history -s -- "$LINE"; '
. "history -w {$history_path}; "
. 'printf \'%s\n\' "$LINE"; ';
}
return "{$shell_binary} -c " . escapeshellarg( $cmd );
}
/**
* Check if a shell binary is bash or bash-compatible.
*
* @param string $shell_path Path to the shell binary.
* @return bool True if the shell is bash, false otherwise.
*/
private static function is_bash_shell( $shell_path ) {
if ( ! is_file( $shell_path ) || ! is_readable( $shell_path ) ) {
return false;
}
// Check if the basename is exactly 'bash' or starts with 'bash' followed by a version/variant.
$basename = basename( $shell_path );
return 'bash' === $basename || 0 === strpos( $basename, 'bash-' );
}
/**
* Check if a shell binary is ksh or a ksh-compatible shell (mksh, pdksh, ksh93, etc.).
*
* @param string $shell_path Path to the shell binary.
* @return bool True if the shell is ksh-compatible, false otherwise.
*/
private static function is_ksh_shell( $shell_path ) {
if ( ! is_file( $shell_path ) || ! is_readable( $shell_path ) ) {
return false;
}
$basename = basename( $shell_path );
// Matches ksh, ksh93, ksh88, mksh, pdksh, etc.
return 0 === strpos( $basename, 'ksh' )
|| 'mksh' === $basename
|| 'pdksh' === $basename;
}
/**
* Check if a shell binary is supported (bash or ksh-compatible).
*
* @param string $shell_path Path to the shell binary.
* @return bool True if the shell is supported, false otherwise.
*/
private static function is_supported_shell( $shell_path ) {
return self::is_bash_shell( $shell_path ) || self::is_ksh_shell( $shell_path );
}
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 );
}
/**
* Check if the watched path has changes.
*
* @return bool True if changes detected, false otherwise.
*/
private function has_changes() {
if ( ! $this->watch_path ) {
return false;
}
$current_mtime = $this->get_recursive_mtime( $this->watch_path );
return $current_mtime !== $this->watch_mtime;
}
/**
* Get the most recent modification time for a path recursively.
*
* @param string $path Path to check.
* @return int Most recent modification time.
*/
private function get_recursive_mtime( $path ) {
$mtime = 0;
if ( is_file( $path ) ) {
$file_mtime = filemtime( $path );
return false !== $file_mtime ? $file_mtime : 0;
}
if ( is_dir( $path ) ) {
$dir_mtime = filemtime( $path );
$mtime = false !== $dir_mtime ? $dir_mtime : 0;
try {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $path, \RecursiveDirectoryIterator::SKIP_DOTS ),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ( $iterator as $file ) {
/** @var \SplFileInfo $file */
$file_mtime = $file->getMTime();
if ( $file_mtime > $mtime ) {
$mtime = $file_mtime;
}
}
} catch ( \UnexpectedValueException $e ) {
// Handle unreadable directories/files gracefully.
WP_CLI::warning(
sprintf(
'Could not read path "%s" while checking for changes: %s',
$path,
$e->getMessage()
)
);
}
}
return $mtime;
}
}