From 309875b01ceaa4c567c1033b43b80c97913c7c65 Mon Sep 17 00:00:00 2001 From: mpeshev Date: Fri, 9 Aug 2013 02:24:14 +0300 Subject: [PATCH] Add requests support in composer and for core command --- composer.json | 8 +++++--- php/commands/core.php | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index a6172777..0153f1c6 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "php": ">=5.3.2", "wp-cli/php-cli-tools": "0.9.3", "mustache/mustache": "~2.4", - "rhumsaa/array_column": "~1.1" + "rhumsaa/array_column": "~1.1", + "rmccue/requests": ">=1.0" }, "suggest": { "d11wtq/boris": "Enhanced `wp shell` functionality" @@ -21,6 +22,7 @@ "behat/behat": "~2.4" }, "autoload": { - "psr-0": { "WP_CLI": "php" } - } + "psr-0": { "WP_CLI": "php", "Requests": "library/" } + }, + "minimum-stability": "dev" } diff --git a/php/commands/core.php b/php/commands/core.php index 5d21f820..999d55be 100644 --- a/php/commands/core.php +++ b/php/commands/core.php @@ -60,16 +60,40 @@ class Core_Command extends WP_CLI_Command { // We need to use a temporary file because piping from cURL to tar is flaky // on MinGW (and probably in other environments too). $temp = tempnam( sys_get_temp_dir(), "wp_" ); - $cmd = "curl -f $silent %s > $temp && tar xz --strip-components=1 --directory=%s -f $temp && rm $temp"; - WP_CLI::launch( Utils\esc_cmd( $cmd, $download_url, ABSPATH ) ); + + $headers = array('Accept' => 'application/json'); + $options = array( + 'verify' => false, + 'timeout' => 30, + 'filename' => $temp + ); + + try { + $request = Requests::get( $download_url, $headers, $options ); + } catch( Requests_Exception $ex ) { + WP_CLI::error( $ex->getMessage() ); + } + + $cmd = "tar xz --strip-components=1 --directory=%s -f $temp && rm $temp"; + + WP_CLI::launch( sprintf( $cmd, ABSPATH ) ); WP_CLI::success( 'WordPress downloaded.' ); } private static function _read( $url ) { - exec( 'curl -s ' . escapeshellarg( $url ), $lines, $r ); - if ( $r ) exit( $r ); - return implode( "\n", $lines ); + $headers = array('Accept' => 'application/json'); + $options = array(); + + $r = false; + try { + $request = Requests::get( $url, $headers, $options ); + $r = $request->body; + } catch( Requests_Exception $ex ) { + WP_CLI::error( $ex->getMessage() ); + } + + return $r; } private function get_download_offer( $locale ) { @@ -155,11 +179,11 @@ class Core_Command extends WP_CLI_Command { } // TODO: adapt more resilient code from wp-admin/setup-config.php + $assoc_args['keys-and-salts'] = self::_read( 'https://api.wordpress.org/secret-key/1.1/salt/' ); $out = Utils\mustache_render( 'wp-config.mustache', $assoc_args ); - file_put_contents( ABSPATH . 'wp-config.php', $out ); WP_CLI::success( 'Generated wp-config.php file.' ); -- 2.11.0