OSDN Git Service

merge split_special() into parse_args()
authorscribu <mail@scribu.net>
Wed, 12 Jun 2013 02:50:43 +0000 (05:50 +0300)
committerscribu <mail@scribu.net>
Wed, 12 Jun 2013 02:56:31 +0000 (05:56 +0300)
this gives us two clear variables to work with: $runtime_config and $local_config

php/WP_CLI/Configurator.php
php/WP_CLI/Runner.php

index a7078c5..47e919f 100644 (file)
@@ -31,7 +31,7 @@ class Configurator {
        }
 
        /**
-        * Splits a list of arguments into positional and associative.
+        * Splits a list of arguments into positional, associative and config.
         *
         * @param string
         * @return array
@@ -50,7 +50,35 @@ class Configurator {
                        }
                }
 
-               return array( $regular_args, $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 ] );
+                               }
+                       }
+               }
+
+               return array( $regular_args, $assoc_args, $runtime_config );
+       }
+
+       private static function handle_boolean_param( &$assoc_args, &$config, $param ) {
+               $subkeys = array(
+                       "$param" => true,
+                       "no-$param" => false
+               );
+
+               foreach ( $subkeys as $key => $value ) {
+                       if ( isset( $assoc_args[ $key ] ) ) {
+                               $config[ $param ] = $value;
+                       }
+
+                       unset( $assoc_args[ $key ] );
+               }
        }
 
        /**
@@ -81,36 +109,5 @@ class Configurator {
 
                return $sanitized_config;
        }
-
-       /**
-        * Extract values from an associative array, according to the spec.
-        */
-       function split_special( &$assoc_args, &$config ) {
-               foreach ( $this->spec as $key => $details ) {
-                       if ( true === $details['runtime'] ) {
-                               self::handle_boolean_param( $assoc_args, $config, $key );
-                       } elseif ( false !== $details['runtime'] ) {
-                               if ( isset( $assoc_args[ $key ] ) ) {
-                                       $config[ $key ] = $assoc_args[ $key ];
-                                       unset( $assoc_args[ $key ] );
-                               }
-                       }
-               }
-       }
-
-       private static function handle_boolean_param( &$assoc_args, &$config, $param ) {
-               $subkeys = array(
-                       "$param" => true,
-                       "no-$param" => false
-               );
-
-               foreach ( $subkeys as $key => $value ) {
-                       if ( isset( $assoc_args[ $key ] ) ) {
-                               $config[ $param ] = $value;
-                       }
-
-                       unset( $assoc_args[ $key ] );
-               }
-       }
 }
 
index 93510c9..c33edc7 100644 (file)
@@ -16,27 +16,27 @@ class Runner {
                return $this->$key;
        }
 
-       private static function get_config_path( &$assoc_args ) {
-               if ( isset( $assoc_args['config'] ) && file_exists( $assoc_args['config'] ) ) {
-                       $path = $assoc_args['config'];
-                       unset( $assoc_args['config'] );
-                       return $path;
+       private static function get_config_path( $runtime_config ) {
+               if ( isset( $runtime_config['config'] ) && file_exists( $runtime_config['config'] ) ) {
+                       return $runtime_config['config'];
                }
 
                $config_files = array(
                        'wp-cli.local.yml',
                        'wp-cli.yml'
                );
-               // Stop looking upward when we find we have emerged from a subdirectory install into a parent install
-               $stop_check = function ( $dir ) {
+
+               // Stop looking upward when we find we have emerged from a subdirectory
+               // install into a parent install
+               $path = Utils\find_file_upward( $config_files, getcwd(), function ( $dir ) {
                        static $wp_load_count = 0;
                        $wp_load_path = $dir . DIRECTORY_SEPARATOR . 'wp-load.php';
                        if ( file_exists( $wp_load_path ) ) {
                                $wp_load_count += 1;
                        }
                        return $wp_load_count > 1;
-               };
-               $path = Utils\find_file_upward( $config_files, getcwd(), $stop_check );
+               } );
+
                if ( $path ) {
                        return $path;
                }
@@ -154,9 +154,7 @@ class Runner {
        }
 
        // Transparently convert old syntaxes
-       private static function back_compat_conversions( $r ) {
-               list( $args, $assoc_args ) = $r;
-
+       private static function back_compat_conversions( $args, $assoc_args ) {
                // foo --help  ->  help foo
                if ( isset( $assoc_args['help'] ) ) {
                        array_unshift( $args, 'help' );
@@ -228,14 +226,17 @@ class Runner {
        }
 
        public function before_wp_load() {
+               list( $args, $assoc_args, $runtime_config ) = \WP_CLI::$configurator->parse_args(
+                       array_slice( $GLOBALS['argv'], 1 ) );
+
                list( $this->arguments, $this->assoc_args ) = self::back_compat_conversions(
-                       \WP_CLI::$configurator->parse_args( array_slice( $GLOBALS['argv'], 1 ) ) );
+                       $args, $assoc_args );
 
-               $this->config_path = self::get_config_path( $this->assoc_args );
+               $this->config_path = self::get_config_path( $runtime_config );
 
-               $this->config = \WP_CLI::$configurator->load_config( $this->config_path );
+               $local_config = \WP_CLI::$configurator->load_config( $this->config_path );
 
-               \WP_CLI::$configurator->split_special( $this->assoc_args, $this->config );
+               $this->config = array_merge( $local_config, $runtime_config );
 
                if ( !isset( $this->config['path'] ) ) {
                        $this->config['path'] = dirname( Utils\find_file_upward( 'wp-load.php' ) );