Replace Guzzle with Artax

This commit is contained in:
Timothy Warren 2017-02-09 13:44:56 -05:00
parent 0e9c257ab7
commit c5b51054df

View File

@ -16,8 +16,9 @@
namespace Aviat\EasyMin; namespace Aviat\EasyMin;
use GuzzleHttp\Client; use function Amp\wait;
use GuzzleHttp\Psr7\Request; use Amp\Artax\{Client, FormBody, Request};
use Aviat\Ion\Json;
// Include guzzle // Include guzzle
require_once('../vendor/autoload.php'); require_once('../vendor/autoload.php');
@ -97,14 +98,21 @@ class JSMin extends BaseMin {
*/ */
protected function closure_call(array $options) protected function closure_call(array $options)
{ {
$client = new Client(); $formFields = http_build_query($options);
$response = $client->post('http://closure-compiler.appspot.com/compile', [
'headers' => [ $request = (new Request)
->setMethod('POST')
->setUri('http://closure-compiler.appspot.com/compile')
->setAllHeaders([
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'gzip',
'Content-type' => 'application/x-www-form-urlencoded' 'Content-type' => 'application/x-www-form-urlencoded'
], ])
'form_params' => $options ->setBody($formFields);
]);
$response = wait((new Client)->request($request, [
Client::OP_AUTO_ENCODING => false
]));
return $response; return $response;
} }
@ -118,13 +126,14 @@ class JSMin extends BaseMin {
protected function check_minify_errors($options) protected function check_minify_errors($options)
{ {
$error_res = $this->closure_call($options); $error_res = $this->closure_call($options);
$error_json = (string)$error_res->getBody(); $error_json = $error_res->getBody();
$error_obj = json_decode($error_json) ?: (object)[]; $error_obj = Json::decode($error_json) ?: (object)[];
// Show error if exists // Show error if exists
if ( ! empty($error_obj->errors) || ! empty($error_obj->serverErrors)) if ( ! empty($error_obj->errors) || ! empty($error_obj->serverErrors))
{ {
$error_json = json_encode($error_obj, JSON_PRETTY_PRINT); $error_json = Json::encode($error_obj, JSON_PRETTY_PRINT);
header('Content-type: application/javascript'); header('Content-type: application/javascript');
echo "console.error(${error_json});"; echo "console.error(${error_json});";
die(); die();
@ -201,10 +210,11 @@ class JSMin extends BaseMin {
// Now actually retrieve the compiled code // Now actually retrieve the compiled code
$options['output_info'] = 'compiled_code'; $options['output_info'] = 'compiled_code';
$res = $this->closure_call($options); $res = $this->closure_call($options);
$json = (string)$res->getBody(); $json = $res->getBody();
$obj = json_decode($json); $obj = Json::decode($json);
return $obj->compiledCode; //return $obj;
return $obj['compiledCode'];
} }
/** /**