Skip to content

Commit b8f16d6

Browse files
authored
Merge pull request #6070 from wp-cli/add-wp-cli-require
Add WP_CLI_REQUIRE env var for including extra php files
2 parents a39513b + 5322351 commit b8f16d6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

features/runcommand.feature

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,54 @@ Feature: Run a WP-CLI command
338338
"""
339339
The used path is: /bad/path/
340340
"""
341+
342+
Scenario: Check that required files are used from command arguments and ENV VAR
343+
Given a WP installation
344+
And a custom-cmd.php file:
345+
"""
346+
<?php
347+
class Custom_Command extends WP_CLI_Command {
348+
/**
349+
* Custom command to test passing command_args via runcommand options
350+
*
351+
* @when after_wp_load
352+
*/
353+
public function echo_test( $args ) {
354+
echo "test" . PHP_EOL;
355+
}
356+
}
357+
WP_CLI::add_command( 'custom-command', 'Custom_Command' );
358+
"""
359+
And a env.php file:
360+
"""
361+
<?php
362+
echo 'ENVIRONMENT REQUIRE' . PHP_EOL;
363+
"""
364+
And a env-2.php file:
365+
"""
366+
<?php
367+
echo 'ENVIRONMENT REQUIRE 2' . PHP_EOL;
368+
"""
369+
370+
When I run `WP_CLI_REQUIRE=env.php wp eval 'return null;' --skip-wordpress`
371+
Then STDOUT should be:
372+
"""
373+
ENVIRONMENT REQUIRE
374+
"""
375+
376+
When I run `WP_CLI_REQUIRE=env.php wp --require=custom-cmd.php custom-command echo_test`
377+
Then STDOUT should be:
378+
"""
379+
ENVIRONMENT REQUIRE
380+
test
381+
"""
382+
383+
When I run `WP_CLI_REQUIRE='env.php,env-2.php' wp --require=custom-cmd.php custom-command echo_test`
384+
Then STDOUT should be:
385+
"""
386+
ENVIRONMENT REQUIRE
387+
ENVIRONMENT REQUIRE 2
388+
test
389+
"""
390+
391+

php/WP_CLI/Configurator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ public function __construct( $path ) {
8484

8585
$this->config[ $key ] = $details['default'];
8686
}
87+
88+
$env_files = getenv( 'WP_CLI_REQUIRE' )
89+
? array_filter( array_map( 'trim', explode( ',', getenv( 'WP_CLI_REQUIRE' ) ) ) )
90+
: [];
91+
92+
if ( ! empty( $env_files ) ) {
93+
if ( ! isset( $this->config['require'] ) ) {
94+
$this->config['require'] = [];
95+
}
96+
$this->config['require'] = array_unique( array_merge( $env_files, $this->config['require'] ) );
97+
}
8798
}
8899

89100
/**

0 commit comments

Comments
 (0)