@@ -1676,6 +1676,76 @@ 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+ $ rows [] = [ substr ( $ command , 3 ), $ save ];
1706+ }
1707+
1708+ $ marker = 'WP_CLI_TEST_SAVED_VARIABLES: ' ;
1709+ $ code = '<?php
1710+ $wp_cli_test_saved = [];
1711+ foreach ( ' . var_export ( $ rows , true ) . ' as $wp_cli_test_row ) {
1712+ $wp_cli_test_command = strtr( $wp_cli_test_row[0], $wp_cli_test_saved );
1713+ $wp_cli_test_result = WP_CLI::runcommand( $wp_cli_test_command, [ "launch" => false, "return" => "all", "exit_error" => false ] );
1714+ if ( 0 !== $wp_cli_test_result->return_code ) {
1715+ 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" );
1716+ exit( 1 );
1717+ }
1718+ if ( "" !== $wp_cli_test_row[1] ) {
1719+ $wp_cli_test_saved[ "{" . $wp_cli_test_row[1] . "}" ] = trim( $wp_cli_test_result->stdout, "\n" );
1720+ }
1721+ }
1722+ echo "\n ' . $ marker . '" . json_encode( $wp_cli_test_saved ) . "\n";
1723+ ' ;
1724+
1725+ $ file = $ this ->variables ['RUN_DIR ' ] . '/ ' . uniqid ( 'wp-cli-test-commands- ' , true ) . '.php ' ;
1726+ file_put_contents ( $ file , $ code );
1727+
1728+ try {
1729+ $ result = $ this ->proc ( Utils \esc_cmd ( 'wp eval-file %s ' , $ file ) )->run ();
1730+ } finally {
1731+ unlink ( $ file );
1732+ }
1733+
1734+ if ( 0 !== $ result ->return_code ) {
1735+ throw new RuntimeException ( "Running the commands in a single process failed: \n{$ result ->stderr }\n{$ result ->stdout }" );
1736+ }
1737+
1738+ $ position = strrpos ( $ result ->stdout , $ marker );
1739+ $ saved = false !== $ position ? json_decode ( trim ( substr ( $ result ->stdout , $ position + strlen ( $ marker ) ) ), true ) : null ;
1740+ if ( ! is_array ( $ saved ) ) {
1741+ throw new RuntimeException ( "Could not read the saved variables from the output: \n{$ result ->stdout }" );
1742+ }
1743+
1744+ foreach ( $ saved as $ placeholder => $ value ) {
1745+ $ this ->variables [ trim ( $ placeholder , '{} ' ) ] = $ value ;
1746+ }
1747+ }
1748+
16791749 /**
16801750 * Start a background process. Will automatically be closed when the tests finish.
16811751 *
0 commit comments