-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathREPL.php
More file actions
216 lines (180 loc) · 5.86 KB
/
REPL.php
File metadata and controls
216 lines (180 loc) · 5.86 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
<?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() {
// @phpstan-ignore while.alwaysTrue
while ( true ) {
$__repl_input_line = $this->prompt();
if ( '' === $__repl_input_line ) {
continue;
}
$__repl_input_line = rtrim( $__repl_input_line, ';' ) . ';';
if ( self::starts_with( self::non_expressions(), $__repl_input_line ) ) {
ob_start();
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
eval( $__repl_input_line );
$__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();
// 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 );
$__repl_output = (string) ob_get_clean();
if ( 0 < strlen( $__repl_output ) ) {
echo rtrim( $__repl_output, "\n" ) . "\n";
}
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 );
}
}