Repository navigation
Expand file tree
/
Copy pathEvalFile_Command.php
More file actions
235 lines (206 loc) · 7.65 KB
/
Copy pathEvalFile_Command.php
File metadata and controls
235 lines (206 loc) · 7.65 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
<?php
use WP_CLI\Utils;
class EvalFile_Command extends WP_CLI_Command {
/**
* Regular expression pattern to match the shell shebang.
*
* @var string
*/
const SHEBANG_PATTERN = '/^(#!.*)$/m';
/**
* Cache for STDIN contents to support alias groups.
*
* When using alias groups, the same command is executed multiple times
* in separate processes. STDIN can only be read once, so we cache it
* in a temporary file that persists across processes.
*
* @var string|null
*/
private static $stdin_cache = null;
/**
* Temporary file path for STDIN cache.
*
* @var string|null
*/
private static $stdin_cache_file = null;
/**
* Loads and executes a PHP file.
*
* Note: because code is executed within a method, global variables need
* to be explicitly globalized.
*
* ## OPTIONS
*
* <file>
* : The path to the PHP file to execute. Use '-' to run code from STDIN.
*
* [<arg>...]
* : One or more positional arguments to pass to the file. They are placed in the $args variable.
*
* [--skip-wordpress]
* : Load and execute file without loading WordPress.
*
* [--use-include]
* : Process the provided file via include instead of evaluating its contents.
*
* [--hook=<hook>]
* : Execute file after a specific WordPress hook has fired.
*
* @when before_wp_load
*
* ## EXAMPLES
*
* # Execute file my-code.php and pass value1 and value2 arguments.
* # Access arguments in $args array ($args[0] = value1, $args[1] = value2).
* $ wp eval-file my-code.php value1 value2
*
* # Execute file after the 'init' hook.
* $ wp eval-file my-code.php --hook=init
*/
public function __invoke( $args, $assoc_args ) {
$file = array_shift( $args );
if ( '-' !== $file && ! file_exists( $file ) ) {
WP_CLI::error( "'$file' does not exist." );
}
$use_include = Utils\get_flag_value( $assoc_args, 'use-include', false );
if ( '-' === $file && $use_include ) {
WP_CLI::error( '"-" and "--use-include" parameters cannot be used at the same time' );
}
$execute_closure = function () use ( $file, $args, $use_include ) {
self::execute_eval( $file, $args, $use_include );
};
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
$skip_wordpress = Utils\get_flag_value( $assoc_args, 'skip-wordpress' );
if ( $hook && null !== $skip_wordpress ) {
WP_CLI::error( 'The --hook parameter cannot be used with --skip-wordpress.' );
}
if ( $hook ) {
WP_CLI::add_wp_hook( $hook, $execute_closure );
}
if ( null === $skip_wordpress ) {
WP_CLI::get_runner()->load_wordpress();
}
if ( ! $hook ) {
$execute_closure();
}
}
/**
* Evaluate a provided file.
*
* @param string $file Filepath to execute, or - for STDIN.
* @param mixed $positional_args Array of positional arguments to pass to the file.
* @param bool $use_include Process the provided file via include instead of evaluating its contents.
*/
private static function execute_eval( $file, $positional_args, $use_include ) {
global $args;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$args = $positional_args;
unset( $positional_args );
if ( '-' === $file ) {
// Cache STDIN contents to support alias groups.
// When using alias groups, each site runs in a separate process,
// but they all share the same STDIN. Once STDIN is read, it's consumed.
// We cache it in a temporary file that persists across processes.
if ( null === self::$stdin_cache ) {
// Get a unique identifier for the cache file based on STDIN's inode.
// All processes sharing the same STDIN pipe will have the same inode,
// allowing them to share the cache file even if they have different PPIDs.
$cache_id = false;
$stdin_stat = fstat( STDIN );
if ( false !== $stdin_stat && isset( $stdin_stat['ino'] ) && isset( $stdin_stat['dev'] ) ) {
// Use device + inode to uniquely identify this STDIN stream
$cache_id = $stdin_stat['dev'] . '-' . $stdin_stat['ino'];
} elseif ( function_exists( 'posix_getppid' ) ) {
// Fallback to parent PID if fstat fails.
$ppid = posix_getppid();
// PHPStan doesn't understand that posix_getppid() can return false even after function_exists check.
// @phpstan-ignore-next-line
if ( false !== $ppid ) {
$cache_id = (string) $ppid;
}
}
// If we couldn't determine a shared cache key, fall back to environment variable or process ID.
if ( false === $cache_id ) {
$cache_id = getenv( 'WP_CLI_STDIN_CACHE_ID' );
if ( ! $cache_id ) {
$cache_id = getmypid();
WP_CLI::debug(
sprintf(
'Could not determine a shared cache key for STDIN. Falling back to process ID (%d). STDIN with alias groups may not work on this system without setting WP_CLI_STDIN_CACHE_ID.',
$cache_id
),
'eval'
);
}
}
self::$stdin_cache_file = sys_get_temp_dir() . '/wp-cli-eval-stdin-' . $cache_id . '.php';
// Check if cache file already exists (created by a previous subprocess)
if ( file_exists( self::$stdin_cache_file ) ) {
$stdin_contents = file_get_contents( self::$stdin_cache_file );
if ( false === $stdin_contents ) {
WP_CLI::error( 'Failed to read from STDIN cache file.' );
}
self::$stdin_cache = (string) $stdin_contents;
// Clean up old cache files (older than 1 hour) to prevent accumulation
self::cleanup_old_cache_files();
} else {
// First process: read from STDIN and cache it
$stdin_contents = file_get_contents( 'php://stdin' );
if ( false === $stdin_contents ) {
WP_CLI::error( 'Failed to read from STDIN.' );
}
self::$stdin_cache = (string) $stdin_contents;
// Save to cache file for subsequent processes
$write_result = file_put_contents( self::$stdin_cache_file, self::$stdin_cache );
if ( false === $write_result ) {
WP_CLI::error( 'Failed to write STDIN cache file.' );
}
// Clean up old cache files (older than 1 hour) to prevent accumulation
self::cleanup_old_cache_files();
// Note: We intentionally don't clean up the current cache file immediately.
// If we delete it in the shutdown function, subsequent child processes
// won't be able to read it. The cleanup_old_cache_files() method handles
// removal of stale cache files.
}
}
eval( '?>' . self::$stdin_cache );
} elseif ( $use_include ) {
include $file;
} else {
$file_contents = (string) file_get_contents( $file );
// Adjust for __FILE__ and __DIR__ magic constants.
$file_contents = Utils\replace_path_consts( $file_contents, $file );
// Check for and remove she-bang.
if ( 0 === strncmp( $file_contents, '#!', 2 ) ) {
$file_contents = preg_replace( static::SHEBANG_PATTERN, '', $file_contents );
}
eval( '?>' . $file_contents );
}
}
/**
* Clean up old STDIN cache files to prevent accumulation.
*
* Removes cache files older than 1 hour from the system temp directory.
* This is called when creating or reading a cache file to ensure stale
* files don't accumulate if processes terminate unexpectedly.
*/
private static function cleanup_old_cache_files() {
$temp_dir = sys_get_temp_dir();
$cache_pattern = $temp_dir . '/wp-cli-eval-stdin-*.php';
$cache_files = glob( $cache_pattern );
$one_hour_ago = time() - 3600;
if ( ! empty( $cache_files ) ) {
foreach ( $cache_files as $cache_file ) {
// Skip the current cache file
if ( $cache_file === self::$stdin_cache_file ) {
continue;
}
// Delete files older than 1 hour
$file_time = filemtime( $cache_file );
if ( false !== $file_time && $file_time < $one_hour_ago ) {
@unlink( $cache_file );
}
}
}
}
}