Skip to content

Commit a5d0579

Browse files
committed
Merge branch 'main' into copilot/catch-exceptions-restart-session
2 parents aed0918 + 393bb10 commit a5d0579

7 files changed

Lines changed: 365 additions & 18 deletions

File tree

‎.gitattributes‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/.actrc export-ignore
2+
/.distignore export-ignore
3+
/.editorconfig export-ignore
4+
/.github export-ignore
5+
/.gitignore export-ignore
6+
/.typos.toml export-ignore
7+
/AGENTS.md export-ignore
8+
/behat.yml export-ignore
9+
/features export-ignore
10+
/phpcs.xml.dist export-ignore
11+
/phpstan.neon.dist export-ignore
12+
/phpunit.xml.dist export-ignore
13+
/tests export-ignore
14+
/wp-cli.yml export-ignore

‎.github/workflows/copilot-setup-steps.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
- name: Set up PHP environment
2727
if: steps.check_composer_file.outputs.files_exists == 'true'
28-
uses: shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1 # v2
28+
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f # v2
2929
with:
3030
php-version: 'latest'
3131
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
@@ -36,7 +36,7 @@ jobs:
3636

3737
- name: Install Composer dependencies & cache dependencies
3838
if: steps.check_composer_file.outputs.files_exists == 'true'
39-
uses: ramsey/composer-install@a35c6ebd3d08125aaf8852dff361e686a1a67947 # v3
39+
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # v3
4040
env:
4141
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
4242
with:

‎README.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contr
1010
## Using
1111

1212
~~~
13-
wp shell [--basic] [--hook=<hook>]
13+
wp shell [--basic] [--watch=<path>] [--hook=<hook>]
1414
~~~
1515

1616
`wp shell` allows you to evaluate PHP statements and expressions
@@ -19,12 +19,21 @@ hit enter, and see the code execute right before you. Because WordPress
1919
is loaded, you have access to all the functions, classes and globals
2020
that you can use within a WordPress plugin, for example.
2121

22+
The `restart` command reloads the shell by spawning a new PHP process,
23+
allowing modified code to be fully reloaded. Note that this requires
24+
the `pcntl_exec()` function. If not available, the shell restarts
25+
in-process, which resets variables but doesn't reload PHP files.
26+
2227
**OPTIONS**
2328

2429
[--basic]
2530
Force the use of WP-CLI's built-in PHP REPL, even if the Boris or
2631
PsySH PHP REPLs are available.
2732

33+
[--watch=<path>]
34+
Watch a file or directory for changes and automatically restart the shell.
35+
Only works with the built-in REPL (--basic).
36+
2837
[--hook=<hook>]
2938
Ensure that a specific WordPress action hook has fired before starting the shell.
3039
This validates that the preconditions associated with that hook are met.
@@ -40,6 +49,18 @@ that you can use within a WordPress plugin, for example.
4049
wp> get_bloginfo( 'name' );
4150
=> string(6) "WP-CLI"
4251

52+
# Restart the shell to reload code changes.
53+
$ wp shell
54+
wp> restart
55+
Restarting shell in new process...
56+
wp>
57+
58+
# Watch a directory for changes and auto-restart.
59+
$ wp shell --watch=wp-content/plugins/my-plugin
60+
wp> // Make changes to files in the plugin directory
61+
Detected changes in wp-content/plugins/my-plugin, restarting shell...
62+
wp>
63+
4364
# Start a shell, ensuring the 'init' hook has already fired.
4465
$ wp shell --hook=init
4566

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"wp-cli/wp-cli": "^2.12"
15+
"wp-cli/wp-cli": "^2.13"
1616
},
1717
"require-dev": {
1818
"wp-cli/wp-cli-tests": "^5"

‎features/shell.feature‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,43 @@ Feature: WordPress REPL
6161
"""
6262
And STDERR should be empty
6363
64+
Scenario: Restart shell
65+
Given a WP install
66+
And a session file:
67+
"""
68+
$a = 1;
69+
restart
70+
$b = 2;
71+
"""
72+
73+
When I run `wp shell --basic < session`
74+
Then STDOUT should contain:
75+
"""
76+
Restarting shell...
77+
"""
78+
And STDOUT should contain:
79+
"""
80+
=> int(2)
81+
"""
82+
83+
Scenario: Exit shell
84+
Given a WP install
85+
And a session file:
86+
"""
87+
$a = 1;
88+
exit
89+
"""
90+
91+
When I run `wp shell --basic < session`
92+
Then STDOUT should contain:
93+
"""
94+
=> int(1)
95+
"""
96+
And STDOUT should not contain:
97+
"""
98+
exit
99+
"""
100+
64101
Scenario: Use SHELL environment variable as fallback for bash
65102
Given a WP install
66103

‎src/Shell_Command.php‎

Lines changed: 149 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,21 @@ class Shell_Command extends WP_CLI_Command {
1313
* is loaded, you have access to all the functions, classes and globals
1414
* that you can use within a WordPress plugin, for example.
1515
*
16+
* The `restart` command reloads the shell by spawning a new PHP process,
17+
* allowing modified code to be fully reloaded. Note that this requires
18+
* the `pcntl_exec()` function. If not available, the shell restarts
19+
* in-process, which resets variables but doesn't reload PHP files.
20+
*
1621
* ## OPTIONS
1722
*
1823
* [--basic]
1924
* : Force the use of WP-CLI's built-in PHP REPL, even if the Boris or
2025
* PsySH PHP REPLs are available.
2126
*
27+
* [--watch=<path>]
28+
* : Watch a file or directory for changes and automatically restart the shell.
29+
* Only works with the built-in REPL (--basic).
30+
*
2231
* [--hook=<hook>]
2332
* : Ensure that a specific WordPress action hook has fired before starting the shell.
2433
* This validates that the preconditions associated with that hook are met.
@@ -34,10 +43,32 @@ class Shell_Command extends WP_CLI_Command {
3443
* wp> get_bloginfo( 'name' );
3544
* => string(6) "WP-CLI"
3645
*
46+
* # Restart the shell to reload code changes.
47+
* $ wp shell
48+
* wp> restart
49+
* Restarting shell in new process...
50+
* wp>
51+
*
52+
* # Watch a directory for changes and auto-restart.
53+
* $ wp shell --watch=wp-content/plugins/my-plugin
54+
* wp> // Make changes to files in the plugin directory
55+
* Detected changes in wp-content/plugins/my-plugin, restarting shell...
56+
* wp>
57+
*
3758
* # Start a shell, ensuring the 'init' hook has already fired.
3859
* $ wp shell --hook=init
60+
*
61+
* @param string[] $_ Positional arguments. Unused.
62+
* @param array{basic?: bool, watch?: string} $assoc_args Associative arguments.
3963
*/
4064
public function __invoke( $_, $assoc_args ) {
65+
$watch_path = Utils\get_flag_value( $assoc_args, 'watch', false );
66+
67+
if ( $watch_path && ! Utils\get_flag_value( $assoc_args, 'basic' ) ) {
68+
WP_CLI::warning( 'The --watch option only works with the built-in REPL. Enabling --basic mode.' );
69+
$assoc_args['basic'] = true;
70+
}
71+
4172
$hook = Utils\get_flag_value( $assoc_args, 'hook', '' );
4273

4374
// No hook specified, start immediately.
@@ -67,9 +98,16 @@ public function __invoke( $_, $assoc_args ) {
6798
/**
6899
* Start the shell REPL.
69100
*
70-
* @param array<string,bool|string> $assoc_args Associative arguments.
101+
* @param array{basic?: bool, watch?: string} $assoc_args Associative arguments.
71102
*/
72103
private function start_shell( $assoc_args ) {
104+
$watch_path = Utils\get_flag_value( $assoc_args, 'watch', '' );
105+
106+
if ( $watch_path && ! Utils\get_flag_value( $assoc_args, 'basic' ) ) {
107+
WP_CLI::warning( 'The --watch option only works with the built-in REPL. Enabling --basic mode.' );
108+
$assoc_args['basic'] = true;
109+
}
110+
73111
$class = WP_CLI\Shell\REPL::class;
74112

75113
$implementations = array(
@@ -98,8 +136,116 @@ private function start_shell( $assoc_args ) {
98136
/**
99137
* @var class-string<WP_CLI\Shell\REPL> $class
100138
*/
101-
$repl = new $class( 'wp> ' );
102-
$repl->start();
139+
if ( $watch_path ) {
140+
$watch_path = $this->resolve_watch_path( $watch_path );
141+
}
142+
143+
do {
144+
$repl = new $class( 'wp> ' );
145+
if ( $watch_path ) {
146+
$repl->set_watch_path( $watch_path );
147+
}
148+
$exit_code = $repl->start();
149+
150+
// If restart requested, exec a new PHP process to reload all code
151+
if ( WP_CLI\Shell\REPL::EXIT_CODE_RESTART === $exit_code ) {
152+
$this->restart_process( $assoc_args );
153+
// If restart_process() returns, pcntl_exec is not available, continue in-process
154+
}
155+
} while ( WP_CLI\Shell\REPL::EXIT_CODE_RESTART === $exit_code );
156+
}
157+
}
158+
159+
/**
160+
* Resolve and validate the watch path.
161+
*
162+
* @param string $path Path to watch.
163+
* @return string|never Absolute path to watch.
164+
*/
165+
private function resolve_watch_path( $path ) {
166+
if ( ! file_exists( $path ) ) {
167+
WP_CLI::error( "Watch path does not exist: {$path}" );
168+
}
169+
170+
$realpath = realpath( $path );
171+
if ( false === $realpath ) {
172+
WP_CLI::error( "Could not resolve watch path: {$path}" );
173+
}
174+
175+
return $realpath;
176+
}
177+
178+
/**
179+
* Restart the shell by spawning a new PHP process.
180+
*
181+
* This replaces the current process with a new one to fully reload all code.
182+
* Falls back to in-process restart if pcntl_exec is not available.
183+
*
184+
* @param array{basic?: bool, watch?: string} $assoc_args Command arguments to preserve.
185+
*/
186+
private function restart_process( $assoc_args ) {
187+
/**
188+
* @var array{0?: string} $argv
189+
*/
190+
global $argv;
191+
192+
// Check if pcntl_exec is available
193+
if ( ! function_exists( 'pcntl_exec' ) ) {
194+
WP_CLI::debug( 'pcntl_exec not available, falling back to in-process restart', 'shell' );
195+
return;
103196
}
197+
198+
// Build the command to restart wp shell with the same arguments
199+
$php_binary = Utils\get_php_binary();
200+
201+
/**
202+
* @var array{argv: array{0?: string}} $_SERVER
203+
*/
204+
205+
// Get the WP-CLI script path
206+
$wp_cli_script = null;
207+
if ( isset( $argv[0] ) ) {
208+
$wp_cli_script = $argv[0];
209+
} elseif ( isset( $_SERVER['argv'][0] ) ) {
210+
$wp_cli_script = $_SERVER['argv'][0];
211+
}
212+
213+
if ( ! $wp_cli_script ) {
214+
WP_CLI::debug( 'Could not determine WP-CLI script path, falling back to in-process restart', 'shell' );
215+
return;
216+
}
217+
218+
// Build arguments array
219+
$args = array( $php_binary, $wp_cli_script, 'shell' );
220+
221+
if ( Utils\get_flag_value( $assoc_args, 'basic' ) ) {
222+
$args[] = '--basic';
223+
}
224+
225+
$watch_path = Utils\get_flag_value( $assoc_args, 'watch', false );
226+
if ( $watch_path ) {
227+
$args[] = '--watch=' . $watch_path;
228+
}
229+
230+
// Add global config values to preserve the environment after restart
231+
$config = WP_CLI::get_runner()->config;
232+
if ( isset( $config['path'] ) ) {
233+
$args[] = '--path=' . $config['path'];
234+
}
235+
if ( isset( $config['user'] ) ) {
236+
$args[] = '--user=' . $config['user'];
237+
}
238+
if ( isset( $config['url'] ) ) {
239+
$args[] = '--url=' . $config['url'];
240+
}
241+
242+
WP_CLI::log( 'Restarting shell in new process...' );
243+
244+
// Replace the current process with a new one
245+
// Note: pcntl_exec does not return on success
246+
pcntl_exec( $php_binary, array_slice( $args, 1 ) );
247+
248+
// If we reach here, exec failed
249+
WP_CLI::warning( 'Failed to restart process, falling back to in-process restart' );
104250
}
105251
}

0 commit comments

Comments
 (0)