OSDN Git Service

move YAML loading to Configurator
authorscribu <mail@scribu.net>
Sat, 23 Nov 2013 10:02:05 +0000 (12:02 +0200)
committerscribu <mail@scribu.net>
Sat, 23 Nov 2013 11:10:01 +0000 (13:10 +0200)
php/WP_CLI/Configurator.php
php/WP_CLI/Runner.php

index 2bfeacf..283e0eb 100644 (file)
@@ -84,15 +84,21 @@ class Configurator {
                return array( $regular_args, $assoc_args, $runtime_config );
        }
 
-       function merge_config( $config, $type ) {
+       function merge_yml( $path ) {
+               $this->merge_config( self::load_yml( $path ), 'file' );
+       }
+
+       function merge_array( $config ) {
+               $this->merge_config( $config, 'runtime' );
+       }
+
+       private function merge_config( $config, $type ) {
                foreach ( $this->spec as $key => $details ) {
                        if ( false !== $details[ $type ] && isset( $config[ $key ] ) ) {
                                $value = $config[ $key ];
 
                                if ( $details['multiple'] ) {
-                                       if ( !is_array( $value ) ) {
-                                               $value = array( $value );
-                                       }
+                                       self::arrayify( $value );
                                        $this->config[ $key ] = array_merge( $this->config[ $key ], $value );
                                } else {
                                        $this->config[ $key ] = $value;
@@ -100,5 +106,42 @@ class Configurator {
                        }
                }
        }
+
+       /**
+        * Load values from a YAML file.
+        */
+       private static function load_yml( $yml_file ) {
+               if ( !$yml_file )
+                       return array();
+
+               $config = spyc_load_file( $yml_file );
+
+               // Make sure config-file-relative paths are made absolute.
+               $yml_file_dir = dirname( $yml_file );
+
+               if ( isset( $config['path'] ) )
+                       self::absolutize( $config['path'], $yml_file_dir );
+
+               if ( isset( $config['require'] ) ) {
+                       self::arrayify( $config['require'] );
+                       foreach ( $config['require'] as &$path ) {
+                               self::absolutize( $path, $yml_file_dir );
+                       }
+               }
+
+               return $config;
+       }
+
+       private static function arrayify( &$val ) {
+               if ( !is_array( $val ) ) {
+                       $val = array( $val );
+               }
+       }
+
+       private static function absolutize( &$path, $base ) {
+               if ( !empty( $path ) && !\WP_CLI\Utils\is_path_absolute( $path ) ) {
+                       $path = $base . DIRECTORY_SEPARATOR . $path;
+               }
+       }
 }
 
index 707b371..76e716f 100644 (file)
@@ -357,7 +357,9 @@ class Runner {
        }
 
        private function init_config() {
-               list( $args, $assoc_args, $runtime_config ) = \WP_CLI::get_configurator()->parse_args(
+               $configurator = \WP_CLI::get_configurator();
+
+               list( $args, $assoc_args, $runtime_config ) = $configurator->parse_args(
                        array_slice( $GLOBALS['argv'], 1 ) );
 
                list( $this->arguments, $this->assoc_args ) = self::back_compat_conversions(
@@ -366,11 +368,10 @@ class Runner {
                $this->global_config_path = self::get_global_config_path();
                $this->project_config_path = self::get_project_config_path();
 
-               $configurator = \WP_CLI::get_configurator();
-               foreach ( array( $this->global_config_path, $this->project_config_path ) as $config_path ) {
-                       $configurator->merge_config( self::load_config( $config_path ), 'file' );
-               }
-               $configurator->merge_config( $runtime_config, 'runtime' );
+               $configurator->merge_yml( $this->global_config_path );
+               $configurator->merge_yml( $this->project_config_path );
+               $configurator->merge_array( $runtime_config );
+
                $this->config = $configurator->to_array();
 
                if ( !isset( $this->config['path'] ) ) {
@@ -378,43 +379,6 @@ class Runner {
                }
        }
 
-       /**
-        * Load values from a YML file.
-        */
-       private static function load_config( $yml_file ) {
-               if ( !$yml_file )
-                       return array();
-
-               $config = spyc_load_file( $yml_file );
-
-               // Make sure config-file-relative paths are made absolute.
-               $yml_file_dir = dirname( $yml_file );
-
-               if ( isset( $config['path'] ) )
-                       self::absolutize( $config['path'], $yml_file_dir );
-
-               if ( isset( $config['require'] ) ) {
-                       self::arrayify( $config['require'] );
-                       foreach ( $config['require'] as &$path ) {
-                               self::absolutize( $path, $yml_file_dir );
-                       }
-               }
-
-               return $config;
-       }
-
-       private static function arrayify( &$val ) {
-               if ( !is_array( $val ) ) {
-                       $val = array( $val );
-               }
-       }
-
-       private static function absolutize( &$path, $base ) {
-               if ( !empty( $path ) && !\WP_CLI\Utils\is_path_absolute( $path ) ) {
-                       $path = $base . DIRECTORY_SEPARATOR . $path;
-               }
-       }
-
        public function before_wp_load() {
                $this->init_config();
                $this->init_colorization();