From 0b4792e67eaa3f9f459530752e290f801bbe7401 Mon Sep 17 00:00:00 2001 From: scribu Date: Sat, 23 Nov 2013 12:02:05 +0200 Subject: [PATCH] move YAML loading to Configurator --- php/WP_CLI/Configurator.php | 51 +++++++++++++++++++++++++++++++++++++++++---- php/WP_CLI/Runner.php | 50 +++++++------------------------------------- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 2bfeacf2..283e0eb8 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -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; + } + } } diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index 707b371f..76e716f5 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -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(); -- 2.11.0