-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathREPL.php
More file actions
178 lines (144 loc) · 4.33 KB
/
REPL.php
File metadata and controls
178 lines (144 loc) · 4.33 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
<?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 ) {
$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 = (string) 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 = (string) ob_get_clean();
if ( 0 < strlen( $out ) ) {
echo rtrim( $out, "\n" ) . "\n";
}
echo '=> ';
var_dump( $evl );
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_bash_shell( (string) getenv( 'SHELL' ) ) ) {
// Only use SHELL as fallback if it's bash (we use bash-specific commands).
$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." );
}
$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 );
}
/**
* 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-' );
}
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 );
}
}