OSDN Git Service

separate YAML parsing from config merging
authorscribu <mail@scribu.net>
Thu, 24 Oct 2013 00:14:50 +0000 (03:14 +0300)
committerscribu <mail@scribu.net>
Thu, 24 Oct 2013 00:14:56 +0000 (03:14 +0300)
php/WP_CLI/Configurator.php
php/WP_CLI/Runner.php

index 7be52c1..791c6c5 100644 (file)
@@ -84,47 +84,21 @@ class Configurator {
                return array( $regular_args, $assoc_args, $runtime_config );
        }
 
-       /**
-        * Load values from a YML file and sanitize them according to the spec.
-        */
-       function load_config( $yml_file ) {
-               if ( $yml_file )
-                       $config = spyc_load_file( $yml_file );
-               else
-                       $config = array();
-
+       function merge_config( $config ) {
                foreach ( $this->spec as $key => $details ) {
                        if ( $details['file'] && isset( $config[ $key ] ) ) {
                                $value = $config[ $key ];
+
                                if ( $details['multiple'] ) {
                                        if ( !is_array( $value ) ) {
                                                $value = array( $value );
                                        }
-
                                        $this->config[ $key ] = array_merge( $this->config[ $key ], $value );
                                } else {
                                        $this->config[ $key ] = $value;
                                }
                        }
                }
-
-               // Make sure config-file-relative paths are made absolute.
-               $yml_file_dir = dirname( $yml_file );
-
-               if ( isset( $this->config['path'] ) )
-                       self::absolutize( $this->config['path'], $yml_file_dir );
-
-               if ( isset( $this->config['require'] ) ) {
-                       foreach ( $this->config['require'] as &$path ) {
-                               self::absolutize( $path, $yml_file_dir );
-                       }
-               }
-       }
-
-       private static function absolutize( &$path, $base ) {
-               if ( !empty( $path ) && !\WP_CLI\Utils\is_path_absolute( $path ) ) {
-                       $path = $base . DIRECTORY_SEPARATOR . $path;
-               }
        }
 }
 
index e70b429..498815b 100644 (file)
@@ -372,20 +372,44 @@ class Runner {
                $this->project_config_path = self::get_project_config_path();
 
                $configurator = \WP_CLI::get_configurator();
-               $configurator->load_config( $this->global_config_path );
-               $configurator->load_config( $this->project_config_path );
+               foreach ( array( $this->global_config_path, $this->project_config_path ) as $config_path ) {
+                       $configurator->merge_config( self::load_config( $config_path ) );
+               }
+               $configurator->merge_config( $runtime_config );
                $this->config = $configurator->to_array();
 
-               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' ) );
+               }
+       }
+
+       /**
+        * 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'] ) ) {
+                       foreach ( $config['require'] as &$path ) {
+                               self::absolutize( $path, $yml_file_dir );
                        }
                }
 
-               if ( !isset( $this->config['path'] ) ) {
-                       $this->config['path'] = dirname( Utils\find_file_upward( 'wp-load.php' ) );
+               return $config;
+       }
+
+       private static function absolutize( &$path, $base ) {
+               if ( !empty( $path ) && !\WP_CLI\Utils\is_path_absolute( $path ) ) {
+                       $path = $base . DIRECTORY_SEPARATOR . $path;
                }
        }