2016-11-25 12:54:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-08-17 23:24:08 +08:00
|
|
|
use Exception;
|
2019-02-27 23:44:50 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2016-11-25 12:54:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-30 16:31:39 +08:00
|
|
|
use Composer\Semver\Comparator;
|
2019-04-05 17:23:27 +08:00
|
|
|
use App\Services\PackageManager;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
class UpdateController extends Controller
|
|
|
|
{
|
2018-08-15 18:21:32 +08:00
|
|
|
protected $currentVersion;
|
|
|
|
protected $updateSource;
|
|
|
|
protected $guzzle;
|
2019-04-06 22:52:43 +08:00
|
|
|
protected $error;
|
|
|
|
protected $info = [];
|
2018-08-15 18:21:32 +08:00
|
|
|
|
|
|
|
public function __construct(\GuzzleHttp\Client $guzzle)
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2018-08-15 18:46:57 +08:00
|
|
|
$this->updateSource = config('app.update_source');
|
2016-11-25 12:54:20 +08:00
|
|
|
$this->currentVersion = config('app.version');
|
2018-08-15 18:21:32 +08:00
|
|
|
$this->guzzle = $guzzle;
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showUpdatePage()
|
|
|
|
{
|
|
|
|
$info = [
|
2019-04-06 22:52:43 +08:00
|
|
|
'latest' => Arr::get($this->getUpdateInfo(), 'latest'),
|
|
|
|
'current' => $this->currentVersion,
|
2016-11-25 12:54:20 +08:00
|
|
|
];
|
2019-04-06 22:52:43 +08:00
|
|
|
$error = $this->error;
|
|
|
|
$extra = ['canUpdate' => $this->canUpdate()];
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
return view('admin.update', compact('info', 'error', 'extra'));
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function checkUpdates()
|
|
|
|
{
|
2019-04-06 22:52:43 +08:00
|
|
|
return json(['available' => $this->canUpdate()]);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
public function download(Request $request, PackageManager $package)
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2019-04-06 22:52:43 +08:00
|
|
|
if (! $this->canUpdate()) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json([]);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
$path = storage_path('packages/bs_'.$this->info['latest'].'.zip');
|
2019-04-05 17:23:27 +08:00
|
|
|
switch ($request->get('action')) {
|
|
|
|
case 'download':
|
2017-08-05 16:00:14 +08:00
|
|
|
try {
|
2019-04-06 22:52:43 +08:00
|
|
|
$package->download($this->info['url'], $path)->extract(base_path());
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
return json(trans('admin.update.complete'), 0);
|
2018-08-17 23:24:08 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
report($e);
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
return json($e->getMessage(), 1);
|
2017-08-05 16:00:14 +08:00
|
|
|
}
|
2019-04-05 17:23:27 +08:00
|
|
|
case 'progress':
|
|
|
|
return $package->progress();
|
2016-11-25 12:54:20 +08:00
|
|
|
default:
|
2017-11-18 20:36:31 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
protected function getUpdateInfo()
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2019-07-05 14:48:12 +08:00
|
|
|
$acceptableSpec = 2;
|
|
|
|
if (app()->runningUnitTests() || ! $this->info) {
|
2016-11-25 12:54:20 +08:00
|
|
|
try {
|
2019-04-19 23:15:05 +08:00
|
|
|
$json = $this->guzzle->request(
|
|
|
|
'GET',
|
|
|
|
$this->updateSource,
|
2019-08-04 18:19:53 +08:00
|
|
|
['verify' => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath()]
|
2019-04-19 23:15:05 +08:00
|
|
|
)->getBody();
|
2019-04-06 22:52:43 +08:00
|
|
|
$info = json_decode($json, true);
|
|
|
|
if (Arr::get($info, 'spec') == $acceptableSpec) {
|
|
|
|
$this->info = $info;
|
|
|
|
} else {
|
2019-07-05 14:48:12 +08:00
|
|
|
$this->error = trans('admin.update.errors.spec');
|
2019-04-06 22:52:43 +08:00
|
|
|
}
|
2018-08-17 23:24:08 +08:00
|
|
|
} catch (Exception $e) {
|
2019-04-06 22:52:43 +08:00
|
|
|
$this->error = $e->getMessage();
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
return $this->info;
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
protected function canUpdate()
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2019-04-06 22:52:43 +08:00
|
|
|
$this->getUpdateInfo();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-07-05 14:48:12 +08:00
|
|
|
$php = Arr::get($this->info, 'php');
|
|
|
|
if (Comparator::lessThan(PHP_VERSION, $php)) {
|
|
|
|
$this->error = trans('admin.update.errors.php', ['version' => $php]);
|
2019-08-15 23:27:29 +08:00
|
|
|
|
2019-07-05 14:48:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-06 22:52:43 +08:00
|
|
|
return Comparator::greaterThan(Arr::get($this->info, 'latest'), $this->currentVersion);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|