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

128 lines
3.8 KiB
PHP
Raw Normal View History

2018-08-19 11:39:14 +08:00
<?php
namespace App\Http\Controllers;
use Exception;
use ZipArchive;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Arr;
2018-08-19 11:39:14 +08:00
use Illuminate\Http\Request;
use App\Services\PluginManager;
use Composer\Semver\Comparator;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
2018-08-19 11:39:14 +08:00
class MarketController extends Controller
{
/**
* Guzzle HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $guzzle;
/**
* Cache for plugins registry.
*
* @var array
*/
protected $registryCache;
public function __construct(\GuzzleHttp\Client $guzzle)
{
$this->guzzle = $guzzle;
}
public function marketData()
{
$plugins = collect($this->getAllAvailablePlugins())->map(function ($item) {
$plugin = plugin($item['name']);
$manager = app('plugins');
if ($plugin) {
$item['enabled'] = $plugin->isEnabled();
$item['installed'] = $plugin->version;
$item['update_available'] = Comparator::greaterThan($item['version'], $item['installed']);
} else {
$item['installed'] = false;
}
2019-02-27 23:44:50 +08:00
$requirements = Arr::get($item, 'require', []);
2018-08-19 11:39:14 +08:00
unset($item['require']);
$item['dependencies'] = [
'isRequirementsSatisfied' => $manager->isRequirementsSatisfied($requirements),
'requirements' => $requirements,
'unsatisfiedRequirements' => $manager->getUnsatisfiedRequirements($requirements),
2018-08-19 11:39:14 +08:00
];
return $item;
});
return $plugins;
}
public function checkUpdates()
{
$pluginsHaveUpdate = collect($this->getAllAvailablePlugins())->filter(function ($item) {
$plugin = plugin($item['name']);
2018-08-19 11:39:14 +08:00
return $plugin && Comparator::greaterThan($item['version'], $plugin->version);
});
return json([
'available' => $pluginsHaveUpdate->isNotEmpty(),
'plugins' => $pluginsHaveUpdate->values()->all(),
2018-08-19 11:39:14 +08:00
]);
}
2019-04-05 17:23:27 +08:00
public function download(Request $request, PluginManager $manager, PackageManager $package)
2018-08-19 11:39:14 +08:00
{
$name = $request->get('name');
$metadata = $this->getPluginMetadata($name);
if (! $metadata) {
return json(trans('admin.plugins.market.non-existent', ['plugin' => $name]), 1);
}
$url = $metadata['dist']['url'];
2019-02-27 23:44:50 +08:00
$filename = Arr::last(explode('/', $url));
2019-04-05 17:23:27 +08:00
$pluginsDir = $manager->getPluginsDir();
$path = storage_path("packages/$filename");
2018-08-19 11:39:14 +08:00
try {
2019-04-05 17:23:27 +08:00
$package->download($url, $path, $metadata['dist']['shasum'])->extract($pluginsDir);
2018-08-19 11:39:14 +08:00
} catch (Exception $e) {
2019-04-05 17:23:27 +08:00
return json($e->getMessage(), 1);
2018-08-19 11:39:14 +08:00
}
return json(trans('admin.plugins.market.install-success'), 0);
}
protected function getPluginMetadata($name)
{
return collect($this->getAllAvailablePlugins())->where('name', $name)->first();
}
protected function getAllAvailablePlugins()
{
2019-04-05 17:48:36 +08:00
$registryVersion = 1;
2018-08-19 11:39:14 +08:00
if (app()->environment('testing') || ! $this->registryCache) {
try {
$pluginsJson = $this->guzzle->request(
2019-04-01 16:12:11 +08:00
'GET', config('plugins.registry')
2018-08-19 11:39:14 +08:00
)->getBody();
} catch (Exception $e) {
throw new Exception(trans('admin.plugins.market.connection-error', [
'error' => htmlentities($e->getMessage()),
2018-08-19 11:39:14 +08:00
]));
}
$this->registryCache = json_decode($pluginsJson, true);
2019-04-05 17:48:36 +08:00
$received = Arr::get($this->registryCache, 'version');
if (is_int($received) && $received != $registryVersion) {
throw new Exception("Only version $registryVersion of market registry is accepted.");
}
2018-08-19 11:39:14 +08:00
}
2019-02-27 23:44:50 +08:00
return Arr::get($this->registryCache, 'packages', []);
2018-08-19 11:39:14 +08:00
}
}