diff --git a/features/server.feature b/features/server.feature index 65f6e9c..4a47a73 100644 --- a/features/server.feature +++ b/features/server.feature @@ -1,4 +1,4 @@ -@require-php-5.4 +@require-php-7.2 Feature: Serve WordPress locally Scenario: Vanilla install @@ -16,6 +16,20 @@ Feature: Serve WordPress locally And I run `cmp /tmp/license.txt license.txt` Then STDOUT should be empty + Scenario: Passthrough arguments to PHP binary + Given a WP install + And a mem.php file: + """ + + """ + And I launch in the background `wp server --host=localhost --port=8182 -- -dmemory_limit=256M` + + When I run `curl -sS localhost:8182/mem.php` + Then STDOUT should be: + """ + 256M + """ + Scenario: Access wp-login.php Given a WP install And I launch in the background `wp server --host=localhost --port=8182` diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6fe826f..88efb15 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,6 +8,3 @@ parameters: scanFiles: - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php treatPhpDocTypesAsCertain: false - ignoreErrors: - - identifier: missingType.parameter - - identifier: missingType.return diff --git a/server-command.php b/server-command.php index 6a0b018..8cd9e4e 100644 --- a/server-command.php +++ b/server-command.php @@ -9,15 +9,4 @@ require_once $wpcli_server_autoloader; } -WP_CLI::add_command( - 'server', - 'Server_Command', - array( - 'before_invoke' => function () { - $min_version = '5.4'; - if ( version_compare( PHP_VERSION, $min_version, '<' ) ) { - WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." ); - } - }, - ) -); +WP_CLI::add_command( 'server', 'Server_Command' ); diff --git a/src/Server_Command.php b/src/Server_Command.php index 7b37992..2c2e350 100644 --- a/src/Server_Command.php +++ b/src/Server_Command.php @@ -34,6 +34,10 @@ class Server_Command extends WP_CLI_Command { * [--config=] * : Configure the server with a specific .ini file. * + * [...] + * : Optional arguments to pass to the PHP binary. Any arguments after `--` + * will be passed through to the `php` command. + * * ## EXAMPLES * * # Make the instance available on any address (with port 8080) @@ -57,9 +61,20 @@ class Server_Command extends WP_CLI_Command { * Document root is / * Press Ctrl-C to quit. * + * # Pass extra parameters to the PHP binary + * $ wp server --docroot=public -- -dzend_extension=xdebug.so + * PHP 7.4.0 Development Server started at Wed Nov 10 18:00:00 2025 + * Listening on http://localhost:8080 + * Document root is /var/www/public + * Press Ctrl-C to quit. + * * @when before_wp_load + * + * @param array $args Positional arguments passed through to the PHP binary. + * @param array{host: string, port: string, docroot?: string, config?: string} $assoc_args Associative arguments passed to the command. + * @return void */ - public function __invoke( $_, $assoc_args ) { + public function __invoke( $args, $assoc_args ) { $defaults = array( 'host' => 'localhost', 'port' => 8080, @@ -86,14 +101,25 @@ public function __invoke( $_, $assoc_args ) { if ( ! file_exists( $router_path ) ) { WP_CLI::error( "Couldn't find router.php" ); } - $cmd = Utils\esc_cmd( - '%s -S %s -t %s -c %s %s', - WP_CLI::get_php_binary(), - $assoc_args['host'] . ':' . $assoc_args['port'], - $docroot, - $assoc_args['config'], - Utils\extract_from_phar( $router_path ) - ); + + // Build the command with passthrough arguments + $cmd_format = '%s'; + $cmd_args = array( WP_CLI::get_php_binary() ); + + // Add passthrough arguments before the -S flag + if ( ! empty( $args ) ) { + $cmd_format .= str_repeat( ' %s', count( $args ) ); + $cmd_args = array_merge( $cmd_args, $args ); + } + + // Add the server flags + $cmd_format .= ' -S %s -t %s -c %s %s'; + $cmd_args[] = $assoc_args['host'] . ':' . $assoc_args['port']; + $cmd_args[] = $docroot; + $cmd_args[] = $assoc_args['config']; + $cmd_args[] = Utils\extract_from_phar( $router_path ); + + $cmd = Utils\esc_cmd( $cmd_format, ...$cmd_args ); $descriptors = array( STDIN, STDOUT, STDERR );