Skip to content

Commit

Permalink
Merge pull request #2496 from wp-cli/2069-config-inheritance
Browse files Browse the repository at this point in the history
Support config deep merging and inheritance
  • Loading branch information
danielbachhuber committed Feb 24, 2016
2 parents a1627b6 + d957dac commit 0f5fae1
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
108 changes: 108 additions & 0 deletions features/config.feature
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,114 @@ Feature: Have a config file
Error: Required file 'foo.php' doesn't exist (from runtime argument).
"""

Scenario: Config inheritance from project to global
Given an empty directory
And a test-cmd.php file:
"""
<?php
$command = function( $_, $assoc_args ) {
echo json_encode( $assoc_args );
};
WP_CLI::add_command( 'test-cmd', $command, array( 'when' => 'before_wp_load' ) );
"""
And a config.yml file:
"""
test-cmd:
foo: bar
apple: banana
apple: banana
"""
And a wp-cli.yml file:
"""
_:
merge: true
test-cmd:
bar: burrito
apple: apple
apple: apple
"""

When I run `wp --require=test-cmd.php test-cmd`
Then STDOUT should be JSON containing:
"""
{"bar":"burrito","apple":"apple"}
"""
When I run `WP_CLI_CONFIG_PATH=config.yml wp --require=test-cmd.php test-cmd`
Then STDOUT should be JSON containing:
"""
{"foo":"bar","apple":"apple","bar":"burrito"}
"""

Given a wp-cli.yml file:
"""
_:
merge: false
test-cmd:
bar: burrito
apple: apple
apple: apple
"""
When I run `WP_CLI_CONFIG_PATH=config.yml wp --require=test-cmd.php test-cmd`
Then STDOUT should be JSON containing:
"""
{"bar":"burrito","apple":"apple"}
"""

Scenario: Config inheritance from local to project
Given an empty directory
And a test-cmd.php file:
"""
<?php
$command = function( $_, $assoc_args ) {
echo json_encode( $assoc_args );
};
WP_CLI::add_command( 'test-cmd', $command, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
test-cmd:
foo: bar
apple: banana
apple: banana
"""

When I run `wp --require=test-cmd.php test-cmd`
Then STDOUT should be JSON containing:
"""
{"foo":"bar","apple":"banana"}
"""

Given a wp-cli.local.yml file:
"""
_:
inherit: wp-cli.yml
merge: true
test-cmd:
bar: burrito
apple: apple
apple: apple
"""

When I run `wp --require=test-cmd.php test-cmd`
Then STDOUT should be JSON containing:
"""
{"foo":"bar","apple":"apple","bar":"burrito"}
"""

Given a wp-cli.local.yml file:
"""
test-cmd:
bar: burrito
apple: apple
apple: apple
"""

When I run `wp --require=test-cmd.php test-cmd`
Then STDOUT should be JSON containing:
"""
{"bar":"burrito","apple":"apple"}
"""

@require-wp-3.9
Scenario: WordPress install with local dev DOMAIN_CURRENT_SITE
Given a WP multisite install
Expand Down
15 changes: 13 additions & 2 deletions php/WP_CLI/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@ private function unmix_assoc_args( $mixed_args ) {
* @param string $path Path to YAML file.
*/
public function merge_yml( $path ) {
foreach ( self::load_yml( $path ) as $key => $value ) {
$yaml = self::load_yml( $path );
if ( ! empty( $yaml['_']['inherit'] ) ) {
$this->merge_yml( $yaml['_']['inherit'] );
}
foreach ( $yaml as $key => $value ) {
if ( !isset( $this->spec[ $key ] ) || false === $this->spec[ $key ]['file'] ) {
$this->extra_config[ $key ] = $value;
if ( isset( $this->extra_config[ $key ] )
&& ! empty( $yaml['_']['merge'] )
&& is_array( $this->extra_config[ $key ] )
&& is_array( $value ) ) {
$this->extra_config[ $key ] = array_merge( $this->extra_config[ $key ], $value );
} else {
$this->extra_config[ $key ] = $value;
}
} elseif ( $this->spec[ $key ]['multiple'] ) {
self::arrayify( $value );
$this->config[ $key ] = array_merge( $this->config[ $key ], $value );
Expand Down

0 comments on commit 0f5fae1

Please sign in to comment.