@@ -1676,6 +1676,88 @@ public function proc( $command, $assoc_args = [], $path = '' ): Process {
16761676 return Process ::create ( $ command , $ cwd , $ env );
16771677 }
16781678
1679+ /**
1680+ * Run several WP-CLI commands one after the other in a single `wp` process.
1681+ *
1682+ * Each command has to start with `wp `, and has to succeed. A command can use the
1683+ * output saved by an earlier command in the same batch as a {VARIABLE}.
1684+ *
1685+ * @param array<array{0: string, 1: string}> $commands Pairs of command and the name of the variable to save its STDOUT into, or ''.
1686+ */
1687+ public function run_commands_in_single_process ( array $ commands ): void {
1688+ $ batch_variables = array_filter ( array_column ( $ commands , 1 ) );
1689+
1690+ $ rows = [];
1691+ foreach ( $ commands as list ( $ command , $ save ) ) {
1692+ // Variables saved within the batch are only known once the process runs.
1693+ $ command = (string ) preg_replace_callback (
1694+ '/\{([A-Z_][A-Z_0-9]*)\}/ ' ,
1695+ function ( $ matches ) use ( $ batch_variables ) {
1696+ return in_array ( $ matches [1 ], $ batch_variables , true ) ? $ matches [0 ] : $ this ->replace_variables ( $ matches [0 ] );
1697+ },
1698+ $ command
1699+ );
1700+
1701+ if ( 0 !== strpos ( $ command , 'wp ' ) ) {
1702+ throw new RuntimeException ( "Only WP-CLI commands can run in a single process, got: {$ command }" );
1703+ }
1704+
1705+ // These change how WordPress is loaded, which has already happened for every command but the first.
1706+ if ( preg_match ( '/(?:^|\s)--(?:require|path|url|skip-plugins|skip-themes|skip-packages|context|exec|ssh|http)(?:[=\s]|$)/ ' , $ command ) ) {
1707+ throw new RuntimeException ( "Commands that change how WordPress is loaded cannot share a process, got: {$ command }" );
1708+ }
1709+
1710+ $ rows [] = [ substr ( $ command , 3 ), $ save ];
1711+ }
1712+
1713+ $ marker = 'WP_CLI_TEST_SAVED_VARIABLES: ' ;
1714+ $ started = 'WP_CLI_TEST_STARTED_COMMAND: ' ;
1715+ $ code = '<?php
1716+ $wp_cli_test_saved = [];
1717+ foreach ( ' . var_export ( $ rows , true ) . ' as $wp_cli_test_index => $wp_cli_test_row ) {
1718+ echo "\n ' . $ started . '{$wp_cli_test_index}\n";
1719+ $wp_cli_test_command = strtr( $wp_cli_test_row[0], $wp_cli_test_saved );
1720+ $wp_cli_test_result = WP_CLI::runcommand( $wp_cli_test_command, [ "launch" => false, "return" => "all", "exit_error" => false ] );
1721+ if ( 0 !== $wp_cli_test_result->return_code ) {
1722+ fwrite( STDERR, "\$ wp {$wp_cli_test_command}\n{$wp_cli_test_result->stdout}\n{$wp_cli_test_result->stderr}\nexit status: {$wp_cli_test_result->return_code}\n" );
1723+ exit( 1 );
1724+ }
1725+ if ( "" !== $wp_cli_test_row[1] ) {
1726+ $wp_cli_test_saved[ "{" . $wp_cli_test_row[1] . "}" ] = trim( $wp_cli_test_result->stdout, "\n" );
1727+ }
1728+ }
1729+ echo "\n ' . $ marker . '" . json_encode( $wp_cli_test_saved ) . "\n";
1730+ ' ;
1731+
1732+ $ file = $ this ->variables ['RUN_DIR ' ] . '/ ' . uniqid ( 'wp-cli-test-commands- ' , true ) . '.php ' ;
1733+ file_put_contents ( $ file , $ code );
1734+
1735+ try {
1736+ $ result = $ this ->proc ( Utils \esc_cmd ( 'wp eval-file %s ' , $ file ) )->run ();
1737+ } finally {
1738+ unlink ( $ file );
1739+ }
1740+
1741+ if ( 0 !== $ result ->return_code ) {
1742+ $ stdout = trim ( (string ) preg_replace ( '/^ ' . $ started . '\d+$/m ' , '' , $ result ->stdout ) );
1743+ throw new RuntimeException ( "Running the commands in a single process failed: \n{$ result ->stderr }\n{$ stdout }" );
1744+ }
1745+
1746+ $ position = strrpos ( $ result ->stdout , $ marker );
1747+ $ saved = false !== $ position ? json_decode ( trim ( substr ( $ result ->stdout , $ position + strlen ( $ marker ) ) ), true ) : null ;
1748+ if ( ! is_array ( $ saved ) ) {
1749+ $ last = preg_match_all ( '/^ ' . $ started . '(\d+)$/m ' , $ result ->stdout , $ matches ) ? (int ) end ( $ matches [1 ] ) : null ;
1750+ if ( null !== $ last ) {
1751+ throw new RuntimeException ( "The process ended while running `wp {$ rows [ $ last ][0 ]}`. A command that exits on its own cannot share a process with other commands. " );
1752+ }
1753+ throw new RuntimeException ( "Could not read the saved variables from the output: \n{$ result ->stdout }" );
1754+ }
1755+
1756+ foreach ( $ saved as $ placeholder => $ value ) {
1757+ $ this ->variables [ trim ( $ placeholder , '{} ' ) ] = $ value ;
1758+ }
1759+ }
1760+
16791761 /**
16801762 * Start a background process. Will automatically be closed when the tests finish.
16811763 *
0 commit comments