blessing-skin-server/app/Http/Controllers/UpdateController.php

135 lines
4.2 KiB
PHP
Raw Normal View History

<?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;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
2019-08-26 11:01:49 +08:00
use Illuminate\Filesystem\Filesystem;
2019-12-10 23:40:32 +08:00
use Symfony\Component\Finder\SplFileInfo;
use Illuminate\Contracts\Console\Kernel as Artisan;
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)
{
$this->updateSource = config('app.update_source');
$this->currentVersion = config('app.version');
2018-08-15 18:21:32 +08:00
$this->guzzle = $guzzle;
}
public function showUpdatePage()
{
$info = [
2019-04-06 22:52:43 +08:00
'latest' => Arr::get($this->getUpdateInfo(), 'latest'),
'current' => $this->currentVersion,
];
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'));
}
public function checkUpdates()
{
2019-04-06 22:52:43 +08:00
return json(['available' => $this->canUpdate()]);
}
2019-08-26 11:01:49 +08:00
public function download(Request $request, PackageManager $package, Filesystem $filesystem)
{
2019-04-06 22:52:43 +08:00
if (! $this->canUpdate()) {
2018-08-18 09:48:39 +08:00
return json([]);
}
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':
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-08-26 11:01:49 +08:00
// Delete options cache. This allows us to update the version info which is recorded as an option.
$filesystem->delete(storage_path('options/cache.php'));
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);
}
2019-04-05 17:23:27 +08:00
case 'progress':
return $package->progress();
default:
2017-11-18 20:36:31 +08:00
return json(trans('general.illegal-parameters'), 1);
}
}
2019-12-10 23:40:32 +08:00
public function update(Filesystem $filesystem, Artisan $artisan)
{
collect($filesystem->files(database_path('update_scripts')))
->filter(function (SplFileInfo $file) {
$name = $file->getFilenameWithoutExtension();
return preg_match('/^\d+\.\d+\.\d+$/', $name) > 0
&& Comparator::greaterThanOrEqualTo($name, option('version'));
})
->each(function (SplFileInfo $file) use ($filesystem) {
$filesystem->getRequire($file->getPathname());
});
option(['version' => config('app.version')]);
$artisan->call('migrate', ['--force' => true]);
$artisan->call('view:clear');
$filesystem->put(storage_path('install.lock'), '');
return view('setup.updates.success');
}
2019-04-06 22:52:43 +08:00
protected function getUpdateInfo()
{
2019-07-05 14:48:12 +08:00
$acceptableSpec = 2;
if (app()->runningUnitTests() || ! $this->info) {
try {
2019-04-19 23:15:05 +08:00
$json = $this->guzzle->request(
'GET',
$this->updateSource,
['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();
}
}
2019-04-19 19:36:36 +08:00
2019-04-06 22:52:43 +08:00
return $this->info;
}
2019-04-06 22:52:43 +08:00
protected function canUpdate()
{
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');
2019-09-12 19:31:48 +08:00
preg_match('/(\d+\.\d+\.\d+)/', PHP_VERSION, $matches);
$version = $matches[1];
if (Comparator::lessThan($version, $php)) {
2019-07-05 14:48:12 +08:00
$this->error = trans('admin.update.errors.php', ['version' => $php]);
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);
}
}