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

90 lines
3.2 KiB
PHP
Raw Normal View History

2016-12-17 19:52:59 +08:00
<?php
namespace App\Http\Controllers;
use App\Services\Plugin;
2016-12-17 19:52:59 +08:00
use Illuminate\Http\Request;
use App\Services\PluginManager;
class PluginController extends Controller
{
2019-08-13 18:42:17 +08:00
public function config(PluginManager $plugins, $name)
2017-01-17 21:41:20 +08:00
{
2019-08-13 18:42:17 +08:00
$plugin = $plugins->get($name);
2017-01-17 21:41:20 +08:00
if ($plugin && $plugin->isEnabled() && $plugin->hasConfigView()) {
return $plugin->getConfigView();
} else {
2017-11-16 10:09:58 +08:00
return abort(404, trans('admin.plugins.operations.no-config-notice'));
2017-01-17 21:41:20 +08:00
}
}
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
public function manage(Request $request, PluginManager $plugins)
{
2019-08-13 18:42:17 +08:00
$name = $request->input('name');
$plugin = $plugins->get($name);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
if ($plugin) {
2018-02-16 17:31:04 +08:00
// Pass the plugin title through the translator.
2017-01-17 21:41:20 +08:00
$plugin->title = trans($plugin->title);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
switch ($request->get('action')) {
case 'enable':
2019-08-13 18:42:17 +08:00
$requirements = $plugins->getUnsatisfied($plugin);
if ($requirements->isNotEmpty()) {
$reason = $requirements->map(function ($detail, $name) {
$constraint = $detail['constraint'];
if (! $detail['version']) {
2019-08-13 18:42:17 +08:00
return trans('admin.plugins.operations.unsatisfied.disabled', compact('name'));
} else {
2019-08-13 18:42:17 +08:00
return trans('admin.plugins.operations.unsatisfied.version', compact('name', 'constraint'));
}
2019-08-13 18:42:17 +08:00
})->values()->all();
2019-04-23 19:14:41 +08:00
return json(trans('admin.plugins.operations.unsatisfied.notice'), 1, compact('reason'));
}
2017-01-17 21:41:20 +08:00
$plugins->enable($name);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
return json(trans('admin.plugins.operations.enabled', ['plugin' => $plugin->title]), 0);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
case 'disable':
$plugins->disable($name);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
return json(trans('admin.plugins.operations.disabled', ['plugin' => $plugin->title]), 0);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
case 'delete':
2019-08-13 18:42:17 +08:00
$plugins->delete($name);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
return json(trans('admin.plugins.operations.deleted'), 0);
2016-12-17 19:52:59 +08:00
2017-01-17 21:41:20 +08:00
default:
2017-11-16 10:09:58 +08:00
return json(trans('admin.invalid-action'), 1);
2016-12-17 19:52:59 +08:00
}
2017-11-16 10:09:58 +08:00
} else {
return json(trans('admin.plugins.operations.not-found'), 1);
2016-12-17 19:52:59 +08:00
}
}
2016-12-17 19:52:59 +08:00
public function getPluginData(PluginManager $plugins)
{
2019-08-13 18:42:17 +08:00
return $plugins->all()
->map(function ($plugin) use ($plugins) {
2018-08-19 17:39:33 +08:00
return [
'name' => $plugin->name,
'title' => trans($plugin->title ?: 'EMPTY'),
'author' => $plugin->author,
'description' => trans($plugin->description ?: 'EMPTY'),
'version' => $plugin->version,
'url' => $plugin->url,
'enabled' => $plugin->isEnabled(),
'config' => $plugin->hasConfigView(),
2019-08-13 18:42:17 +08:00
'dependencies' => [
'all' => $plugin->require,
'unsatisfied' => $plugins->getUnsatisfied($plugin),
],
2018-08-19 17:39:33 +08:00
];
})
2018-08-19 17:39:33 +08:00
->values();
2016-12-17 19:52:59 +08:00
}
}