Skip to content

Commit 0a85ebc

Browse files
Copilotswissspidy
andauthored
Add passthrough arguments to PHP binary via -- separator (#90)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent e8d703c commit 0a85ebc

4 files changed

Lines changed: 51 additions & 25 deletions

File tree

features/server.feature

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@require-php-5.4
1+
@require-php-7.2
22
Feature: Serve WordPress locally
33

44
Scenario: Vanilla install
@@ -16,6 +16,20 @@ Feature: Serve WordPress locally
1616
And I run `cmp /tmp/license.txt license.txt`
1717
Then STDOUT should be empty
1818

19+
Scenario: Passthrough arguments to PHP binary
20+
Given a WP install
21+
And a mem.php file:
22+
"""
23+
<?php echo ini_get('memory_limit'); ?>
24+
"""
25+
And I launch in the background `wp server --host=localhost --port=8182 -- -dmemory_limit=256M`
26+
27+
When I run `curl -sS localhost:8182/mem.php`
28+
Then STDOUT should be:
29+
"""
30+
256M
31+
"""
32+
1933
Scenario: Access wp-login.php
2034
Given a WP install
2135
And I launch in the background `wp server --host=localhost --port=8182`

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ parameters:
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
1010
treatPhpDocTypesAsCertain: false
11-
ignoreErrors:
12-
- identifier: missingType.parameter
13-
- identifier: missingType.return

server-command.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,4 @@
99
require_once $wpcli_server_autoloader;
1010
}
1111

12-
WP_CLI::add_command(
13-
'server',
14-
'Server_Command',
15-
array(
16-
'before_invoke' => function () {
17-
$min_version = '5.4';
18-
if ( version_compare( PHP_VERSION, $min_version, '<' ) ) {
19-
WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." );
20-
}
21-
},
22-
)
23-
);
12+
WP_CLI::add_command( 'server', 'Server_Command' );

src/Server_Command.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class Server_Command extends WP_CLI_Command {
3434
* [--config=<file>]
3535
* : Configure the server with a specific .ini file.
3636
*
37+
* [<passthrough>...]
38+
* : Optional arguments to pass to the PHP binary. Any arguments after `--`
39+
* will be passed through to the `php` command.
40+
*
3741
* ## EXAMPLES
3842
*
3943
* # Make the instance available on any address (with port 8080)
@@ -57,9 +61,20 @@ class Server_Command extends WP_CLI_Command {
5761
* Document root is /
5862
* Press Ctrl-C to quit.
5963
*
64+
* # Pass extra parameters to the PHP binary
65+
* $ wp server --docroot=public -- -dzend_extension=xdebug.so
66+
* PHP 7.4.0 Development Server started at Wed Nov 10 18:00:00 2025
67+
* Listening on http://localhost:8080
68+
* Document root is /var/www/public
69+
* Press Ctrl-C to quit.
70+
*
6071
* @when before_wp_load
72+
*
73+
* @param array<string> $args Positional arguments passed through to the PHP binary.
74+
* @param array{host: string, port: string, docroot?: string, config?: string} $assoc_args Associative arguments passed to the command.
75+
* @return void
6176
*/
62-
public function __invoke( $_, $assoc_args ) {
77+
public function __invoke( $args, $assoc_args ) {
6378
$defaults = array(
6479
'host' => 'localhost',
6580
'port' => 8080,
@@ -86,14 +101,25 @@ public function __invoke( $_, $assoc_args ) {
86101
if ( ! file_exists( $router_path ) ) {
87102
WP_CLI::error( "Couldn't find router.php" );
88103
}
89-
$cmd = Utils\esc_cmd(
90-
'%s -S %s -t %s -c %s %s',
91-
WP_CLI::get_php_binary(),
92-
$assoc_args['host'] . ':' . $assoc_args['port'],
93-
$docroot,
94-
$assoc_args['config'],
95-
Utils\extract_from_phar( $router_path )
96-
);
104+
105+
// Build the command with passthrough arguments
106+
$cmd_format = '%s';
107+
$cmd_args = array( WP_CLI::get_php_binary() );
108+
109+
// Add passthrough arguments before the -S flag
110+
if ( ! empty( $args ) ) {
111+
$cmd_format .= str_repeat( ' %s', count( $args ) );
112+
$cmd_args = array_merge( $cmd_args, $args );
113+
}
114+
115+
// Add the server flags
116+
$cmd_format .= ' -S %s -t %s -c %s %s';
117+
$cmd_args[] = $assoc_args['host'] . ':' . $assoc_args['port'];
118+
$cmd_args[] = $docroot;
119+
$cmd_args[] = $assoc_args['config'];
120+
$cmd_args[] = Utils\extract_from_phar( $router_path );
121+
122+
$cmd = Utils\esc_cmd( $cmd_format, ...$cmd_args );
97123

98124
$descriptors = array( STDIN, STDOUT, STDERR );
99125

0 commit comments

Comments
 (0)