-
Notifications
You must be signed in to change notification settings - Fork 14
Add restart command and --watch option to wp shell #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f7df1b9
b921fd8
f94d780
371e60f
f502d3d
5e0157a
b539274
9ae7546
875bcd4
84c524f
d9bb14d
cf64d50
297d895
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,20 +13,51 @@ class Shell_Command extends WP_CLI_Command { | |
| * is loaded, you have access to all the functions, classes and globals | ||
| * that you can use within a WordPress plugin, for example. | ||
| * | ||
| * The `restart` command reloads the shell by spawning a new PHP process, | ||
| * allowing modified code to be fully reloaded. Note that this requires | ||
| * the `pcntl_exec()` function. If not available, the shell restarts | ||
| * in-process, which resets variables but doesn't reload PHP files. | ||
| * | ||
| * ## OPTIONS | ||
| * | ||
| * [--basic] | ||
| * : Force the use of WP-CLI's built-in PHP REPL, even if the Boris or | ||
| * PsySH PHP REPLs are available. | ||
| * | ||
| * [--watch=<path>] | ||
| * : Watch a file or directory for changes and automatically restart the shell. | ||
| * Only works with the built-in REPL (--basic). | ||
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * # Call get_bloginfo() to get the name of the site. | ||
| * $ wp shell | ||
| * wp> get_bloginfo( 'name' ); | ||
| * => string(6) "WP-CLI" | ||
| * | ||
| * # Restart the shell to reload code changes. | ||
| * $ wp shell | ||
| * wp> restart | ||
| * Restarting shell in new process... | ||
| * wp> | ||
| * | ||
| * # Watch a directory for changes and auto-restart. | ||
| * $ wp shell --watch=wp-content/plugins/my-plugin | ||
| * wp> // Make changes to files in the plugin directory | ||
| * Detected changes in wp-content/plugins/my-plugin, restarting shell... | ||
| * wp> | ||
| * | ||
| * @param string[] $_ Positional arguments. Unused. | ||
| * @param array{basic?: bool, watch?: string} $assoc_args Associative arguments. | ||
| */ | ||
| public function __invoke( $_, $assoc_args ) { | ||
| $watch_path = Utils\get_flag_value( $assoc_args, 'watch', false ); | ||
|
|
||
| if ( $watch_path && ! Utils\get_flag_value( $assoc_args, 'basic' ) ) { | ||
| WP_CLI::warning( 'The --watch option only works with the built-in REPL. Enabling --basic mode.' ); | ||
| $assoc_args['basic'] = true; | ||
| } | ||
|
|
||
| $class = WP_CLI\Shell\REPL::class; | ||
|
|
||
| $implementations = array( | ||
|
|
@@ -55,8 +86,108 @@ public function __invoke( $_, $assoc_args ) { | |
| /** | ||
| * @var class-string<WP_CLI\Shell\REPL> $class | ||
| */ | ||
| $repl = new $class( 'wp> ' ); | ||
| $repl->start(); | ||
| if ( $watch_path ) { | ||
| $watch_path = $this->resolve_watch_path( $watch_path ); | ||
| } | ||
|
|
||
| do { | ||
| $repl = new $class( 'wp> ' ); | ||
| if ( $watch_path ) { | ||
| $repl->set_watch_path( $watch_path ); | ||
| } | ||
| $exit_code = $repl->start(); | ||
|
|
||
| // If restart requested, exec a new PHP process to reload all code | ||
| if ( WP_CLI\Shell\REPL::EXIT_CODE_RESTART === $exit_code ) { | ||
| $this->restart_process( $assoc_args ); | ||
| // If restart_process() returns, pcntl_exec is not available, continue in-process | ||
| } | ||
| } while ( WP_CLI\Shell\REPL::EXIT_CODE_RESTART === $exit_code ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve and validate the watch path. | ||
| * | ||
| * @param string $path Path to watch. | ||
| * @return string|never Absolute path to watch. | ||
| */ | ||
| private function resolve_watch_path( $path ) { | ||
| if ( ! file_exists( $path ) ) { | ||
| WP_CLI::error( "Watch path does not exist: {$path}" ); | ||
| } | ||
|
|
||
| $realpath = realpath( $path ); | ||
| if ( false === $realpath ) { | ||
| WP_CLI::error( "Could not resolve watch path: {$path}" ); | ||
| } | ||
|
|
||
| return $realpath; | ||
| } | ||
|
|
||
| /** | ||
| * Restart the shell by spawning a new PHP process. | ||
| * | ||
| * This replaces the current process with a new one to fully reload all code. | ||
| * Falls back to in-process restart if pcntl_exec is not available. | ||
| * | ||
| * @param array{basic?: bool, watch?: string} $assoc_args Command arguments to preserve. | ||
| */ | ||
| private function restart_process( $assoc_args ) { | ||
| // Check if pcntl_exec is available | ||
| if ( ! function_exists( 'pcntl_exec' ) ) { | ||
| WP_CLI::debug( 'pcntl_exec not available, falling back to in-process restart', 'shell' ); | ||
| return; | ||
| } | ||
|
|
||
| // Build the command to restart wp shell with the same arguments | ||
| $php_binary = Utils\get_php_binary(); | ||
|
|
||
| // Get the WP-CLI script path | ||
| $wp_cli_script = null; | ||
| foreach ( array( 'argv', '_SERVER' ) as $source ) { | ||
| if ( 'argv' === $source && is_array( $GLOBALS['argv'] ) && isset( $GLOBALS['argv'][0] ) ) { | ||
| $wp_cli_script = $GLOBALS['argv'][0]; | ||
| break; | ||
| } elseif ( '_SERVER' === $source && is_array( $_SERVER['argv'] ) && isset( $_SERVER['argv'][0] ) ) { | ||
| $wp_cli_script = $_SERVER['argv'][0]; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ( ! $wp_cli_script ) { | ||
| WP_CLI::debug( 'Could not determine WP-CLI script path, falling back to in-process restart', 'shell' ); | ||
| return; | ||
| } | ||
|
|
||
| // Build arguments array | ||
| $args = array( $php_binary, $wp_cli_script, 'shell' ); | ||
|
|
||
| if ( Utils\get_flag_value( $assoc_args, 'basic' ) ) { | ||
| $args[] = '--basic'; | ||
| } | ||
|
|
||
| $watch_path = Utils\get_flag_value( $assoc_args, 'watch', false ); | ||
| if ( $watch_path ) { | ||
| $args[] = '--watch=' . $watch_path; | ||
| } | ||
|
|
||
| // Add the path argument if present in $_SERVER | ||
| if ( isset( $_SERVER['argv'] ) && is_array( $_SERVER['argv'] ) ) { | ||
| foreach ( $_SERVER['argv'] as $arg ) { | ||
| if ( is_string( $arg ) && '--path=' === substr( $arg, 0, 7 ) ) { | ||
| $args[] = $arg; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+175
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation for preserving the A more robust approach is to retrieve the configuration value directly from WP-CLI's runner. This would also make it easier to preserve other important global parameters (e.g., // Add the path argument if present from the runner's config.
$config = WP_CLI::get_runner()->config;
if ( isset( $config['path'] ) ) {
$args[] = '--path=' . $config['path'];
} |
||
|
|
||
| WP_CLI::log( 'Restarting shell in new process...' ); | ||
|
|
||
| // Replace the current process with a new one | ||
| // Note: pcntl_exec does not return on success | ||
| pcntl_exec( $php_binary, array_slice( $args, 1 ) ); | ||
|
|
||
| // If we reach here, exec failed | ||
| WP_CLI::warning( 'Failed to restart process, falling back to in-process restart' ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to determine the WP-CLI script path is unnecessarily complex. It can be simplified for better readability and maintainability by using a direct
if/elseifcheck on$GLOBALS['argv']and$_SERVER['argv']instead of aforeachloop.