blessing-skin-server/app/Http/Controllers/UpdateController.php
2019-09-12 19:31:48 +08:00

112 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
use App\Services\PackageManager;
use Illuminate\Filesystem\Filesystem;
class UpdateController extends Controller
{
protected $currentVersion;
protected $updateSource;
protected $guzzle;
protected $error;
protected $info = [];
public function __construct(\GuzzleHttp\Client $guzzle)
{
$this->updateSource = config('app.update_source');
$this->currentVersion = config('app.version');
$this->guzzle = $guzzle;
}
public function showUpdatePage()
{
$info = [
'latest' => Arr::get($this->getUpdateInfo(), 'latest'),
'current' => $this->currentVersion,
];
$error = $this->error;
$extra = ['canUpdate' => $this->canUpdate()];
return view('admin.update', compact('info', 'error', 'extra'));
}
public function checkUpdates()
{
return json(['available' => $this->canUpdate()]);
}
public function download(Request $request, PackageManager $package, Filesystem $filesystem)
{
if (! $this->canUpdate()) {
return json([]);
}
$path = storage_path('packages/bs_'.$this->info['latest'].'.zip');
switch ($request->get('action')) {
case 'download':
try {
$package->download($this->info['url'], $path)->extract(base_path());
// Delete options cache. This allows us to update the version info which is recorded as an option.
$filesystem->delete(storage_path('options/cache.php'));
return json(trans('admin.update.complete'), 0);
} catch (Exception $e) {
report($e);
return json($e->getMessage(), 1);
}
case 'progress':
return $package->progress();
default:
return json(trans('general.illegal-parameters'), 1);
}
}
protected function getUpdateInfo()
{
$acceptableSpec = 2;
if (app()->runningUnitTests() || ! $this->info) {
try {
$json = $this->guzzle->request(
'GET',
$this->updateSource,
['verify' => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath()]
)->getBody();
$info = json_decode($json, true);
if (Arr::get($info, 'spec') == $acceptableSpec) {
$this->info = $info;
} else {
$this->error = trans('admin.update.errors.spec');
}
} catch (Exception $e) {
$this->error = $e->getMessage();
}
}
return $this->info;
}
protected function canUpdate()
{
$this->getUpdateInfo();
$php = Arr::get($this->info, 'php');
preg_match('/(\d+\.\d+\.\d+)/', PHP_VERSION, $matches);
$version = $matches[1];
if (Comparator::lessThan($version, $php)) {
$this->error = trans('admin.update.errors.php', ['version' => $php]);
return false;
}
return Comparator::greaterThan(Arr::get($this->info, 'latest'), $this->currentVersion);
}
}