From 82e78d548d3fb61dcbc69bf2901ba4e318dcf8de Mon Sep 17 00:00:00 2001 From: printempw Date: Wed, 15 Aug 2018 18:21:32 +0800 Subject: [PATCH] Use Guzzle to request update source --- app/Http/Controllers/UpdateController.php | 86 +++++++++++++++-------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/UpdateController.php b/app/Http/Controllers/UpdateController.php index 9aa2fd92..1d5e6218 100644 --- a/app/Http/Controllers/UpdateController.php +++ b/app/Http/Controllers/UpdateController.php @@ -2,9 +2,7 @@ namespace App\Http\Controllers; -use Arr; use Log; -use Utils; use File; use Cache; use Option; @@ -17,19 +15,58 @@ use Composer\Semver\Comparator; class UpdateController extends Controller { - public $currentVersion; + /** + * Current application version. + * + * @var string + */ + protected $currentVersion; - public $latestVersion; + /** + * Latest application version in update source. + * + * @var string + */ + protected $latestVersion; - public $updateSource; + /** + * Where to get information of new application versions. + * + * @var string + */ + protected $updateSource; + /** + * Updates information fetched from update source. + * + * @var array|null + */ protected $updateInfo; - public function __construct() + /** + * Guzzle HTTP client. + * + * @var \GuzzleHttp\Client + */ + protected $guzzle; + + /** + * Default request options for Guzzle HTTP client. + * + * @var array + */ + protected $guzzleConfig; + + public function __construct(\GuzzleHttp\Client $guzzle) { $this->updateSource = option('update_source'); - $this->currentVersion = config('app.version'); + + $this->guzzle = $guzzle; + $this->guzzleConfig = [ + 'headers' => ['User-Agent' => config('secure.user_agent')], + 'verify' => config('secure.certificates') + ]; } public function showUpdatePage() @@ -55,7 +92,7 @@ class UpdateController extends Controller ); if ($detail = $this->getReleaseInfo($info['latest_version'])) { - $info = array_merge($info, Arr::only($detail, [ + $info = array_merge($info, array_only($detail, [ 'release_note', 'release_url', 'release_time', @@ -67,20 +104,18 @@ class UpdateController extends Controller } if (! $info['new_version_available']) { - $info['release_time'] = Arr::get($this->getReleaseInfo($this->currentVersion), 'release_time'); + $info['release_time'] = array_get($this->getReleaseInfo($this->currentVersion), 'release_time'); } } - $update = Option::form('update', OptionForm::AUTO_DETECT, function($form) - { - $form->checkbox('check_update', OptionForm::AUTO_DETECT)->label(OptionForm::AUTO_DETECT); - $form->text('update_source', OptionForm::AUTO_DETECT) - ->description(OptionForm::AUTO_DETECT); - })->handle()->always(function($form) { + $update = Option::form('update', OptionForm::AUTO_DETECT, function ($form) { + $form->checkbox('check_update')->label(); + $form->text('update_source')->description(); + })->handle()->always(function ($form) { try { - $response = file_get_contents(option('update_source')); + $response = $this->guzzle->request('GET', option('update_source'), $this->guzzleConfig)->getBody(); } catch (Exception $e) { - $form->addMessage(trans('admin.update.errors.connection').$e->getMessage(), 'danger'); + $form->addMessage(trans('admin.update.errors.connection').e($e->getMessage()), 'danger'); } }); @@ -111,12 +146,6 @@ class UpdateController extends Controller $release_url = $this->getReleaseInfo($this->latestVersion)['release_url']; $tmp_path = Cache::get('tmp_path'); - $client = new \GuzzleHttp\Client(); - $guzzle_config = [ - 'headers' => ['User-Agent' => config('secure.user_agent')], - 'verify' => config('secure.certificates') - ]; - switch ($action) { case 'prepare-download': @@ -149,7 +178,7 @@ class UpdateController extends Controller Log::info('[UpdateWizard] Start downloading update package'); try { - $client->request('GET', $release_url, array_merge($guzzle_config, [ + $this->guzzle->request('GET', $release_url, array_merge($this->guzzleConfig, [ 'sink' => $tmp_path, 'progress' => function ($total, $downloaded) { if ($total == 0) return; @@ -240,7 +269,7 @@ class UpdateController extends Controller : $this->updateSource; try { - $response = file_get_contents($url); + $response = $this->guzzle->request('GET', $url, $this->guzzleConfig)->getBody(); } catch (Exception $e) { Log::error("[CheckingUpdate] Failed to get update information: ".$e->getMessage()); } @@ -248,13 +277,12 @@ class UpdateController extends Controller if (isset($response)) { $this->updateInfo = json_decode($response, true); } - } - $this->latestVersion = Arr::get($this->updateInfo, 'latest_version', $this->currentVersion); + $this->latestVersion = array_get($this->updateInfo, 'latest_version', $this->currentVersion); if (! is_null($key)) { - return Arr::get($this->updateInfo, $key); + return array_get($this->updateInfo, $key); } return $this->updateInfo; @@ -262,7 +290,7 @@ class UpdateController extends Controller protected function getReleaseInfo($version) { - return Arr::get($this->getUpdateInfo('releases'), $version); + return array_get($this->getUpdateInfo('releases'), $version); } }