From: scribu Date: Wed, 12 Jun 2013 03:39:01 +0000 (+0300) Subject: support passing --require multiple times X-Git-Tag: v0.11.0~114^2~2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=a1c78df3df41d6a01e6598b4e8e09a05c677c7ca;p=wvm%2Fwvm.git support passing --require multiple times --- diff --git a/features/flags.feature b/features/flags.feature index 95a201a7..e99f9599 100644 --- a/features/flags.feature +++ b/features/flags.feature @@ -61,23 +61,53 @@ Feature: Global flags Scenario: Using --require Given a WP install And a custom-cmd.php file: - """ - false, 'synopsis' => '', 'default' => null, + 'multiple' => false, ); foreach ( $this->spec as &$option ) { @@ -37,28 +38,34 @@ class Configurator { * @return array */ function parse_args( $arguments ) { - $regular_args = array(); - $assoc_args = array(); + $regular_args = $mixed_args = array(); foreach ( $arguments as $arg ) { if ( preg_match( '|^--([^=]+)$|', $arg, $matches ) ) { - $assoc_args[ $matches[1] ] = true; + $mixed_args[] = array( $matches[1], true ); } elseif ( preg_match( '|^--([^=]+)=(.+)|', $arg, $matches ) ) { - $assoc_args[ $matches[1] ] = $matches[2]; + $mixed_args[] = array( $matches[1], $matches[2] ); } else { $regular_args[] = $arg; } } - $runtime_config = array(); + $assoc_args = $runtime_config = array(); - foreach ( $this->spec as $key => $details ) { - if ( true === $details['runtime'] ) { - self::handle_boolean_param( $assoc_args, $runtime_config, $key ); - } elseif ( false !== $details['runtime'] ) { - if ( isset( $assoc_args[ $key ] ) ) { - $runtime_config[ $key ] = $assoc_args[ $key ]; - unset( $assoc_args[ $key ] ); + foreach ( $mixed_args as $tmp ) { + list( $key, $value ) = $tmp; + + $enabled = isset( $this->spec[ $key ] ) ? $this->spec[ $key ]['runtime'] : false; + + if ( false === $enabled ) { + $assoc_args[ $key ] = $value; + } elseif ( true === $enabled ) { + self::handle_boolean_param( $mixed_args, $runtime_config, $key ); + } else { + if ( $this->spec[ $key ]['multiple'] ) { + $runtime_config[ $key ][] = $value; + } else { + $runtime_config[ $key ] = $value; } } } @@ -95,16 +102,16 @@ class Configurator { $sanitized_config = array(); foreach ( $this->spec as $key => $details ) { - if ( $details['file'] && isset( $config[ $key ] ) ) - $sanitized_config[ $key ] = $config[ $key ]; - else - $sanitized_config[ $key ] = $details['default']; - } + if ( $details['file'] && isset( $config[ $key ] ) ) { + $value = $config[ $key ]; + if ( $details['multiple'] && !is_array( $value ) ) { + $value = array( $value ); + } + } else { + $value = $details['default']; + } - // When invoking from a subdirectory in the project, - // make sure a config-relative 'path' is made absolute - if ( ! empty( $sanitized_config['path'] ) && ! \WP_CLI\Utils\is_path_absolute( $sanitized_config['path'] ) ) { - $sanitized_config['path'] = dirname( $path ) . DIRECTORY_SEPARATOR . $sanitized_config['path']; + $sanitized_config[ $key ] = $value; } return $sanitized_config; diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index c33edc72..13def081 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -236,7 +236,21 @@ class Runner { $local_config = \WP_CLI::$configurator->load_config( $this->config_path ); - $this->config = array_merge( $local_config, $runtime_config ); + // When invoking from a subdirectory in the project, + // make sure a config-relative 'path' is made absolute + if ( !empty( $local_config['path'] ) && !\WP_CLI\Utils\is_path_absolute( $local_config['path'] ) ) { + $local_config['path'] = dirname( $this->config_path ) . DIRECTORY_SEPARATOR . $local_config['path']; + } + + $this->config = $local_config; + + foreach ( $runtime_config as $key => $value ) { + if ( isset( $this->config[ $key ] ) && is_array( $this->config[ $key ] ) ) { + $this->config[ $key ] = array_merge( $this->config[ $key ], $value ); + } else { + $this->config[ $key ] = $value; + } + } if ( !isset( $this->config['path'] ) ) { $this->config['path'] = dirname( Utils\find_file_upward( 'wp-load.php' ) ); @@ -307,8 +321,11 @@ class Runner { // Handle --user parameter self::set_user( $this->config ); - if ( isset( $this->config['require'] ) ) - require $this->config['require']; + if ( isset( $this->config['require'] ) ) { + foreach ( $this->config['require'] as $path ) { + require $path; + } + } // Handle --completions parameter if ( isset( $this->assoc_args['completions'] ) ) { diff --git a/php/config-spec.php b/php/config-spec.php index 41332d9a..a711db49 100644 --- a/php/config-spec.php +++ b/php/config-spec.php @@ -33,6 +33,8 @@ return array( 'runtime' => '=', 'file' => '', 'desc' => 'Load given PHP file before running the command', + 'multiple' => true, + 'default' => array(), ), 'disabled_commands' => array(