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

100 lines
3.4 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
{
2018-08-19 11:39:14 +08:00
public function config($name)
2017-01-17 21:41:20 +08:00
{
$plugin = plugin($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)
{
$plugin = plugin($name = $request->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':
if (! $plugins->isRequirementsSatisfied($plugin)) {
$reason = [];
foreach ($plugins->getUnsatisfiedRequirements($plugin) as $name => $detail) {
$constraint = $detail['constraint'];
if (! $detail['version']) {
$reason[] = trans('admin.plugins.operations.unsatisfied.disabled', compact('name'));
} else {
$reason[] = trans('admin.plugins.operations.unsatisfied.version', compact('name', 'constraint'));
}
}
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':
$plugins->uninstall($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)
{
2018-08-19 17:39:33 +08:00
return $plugins->getPlugins()
->map(function ($plugin) {
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(),
'dependencies' => $this->getPluginDependencies($plugin),
];
})
2018-08-19 17:39:33 +08:00
->values();
2016-12-17 19:52:59 +08:00
}
protected function getPluginDependencies(Plugin $plugin)
{
$plugins = app('plugins');
return [
'isRequirementsSatisfied' => $plugins->isRequirementsSatisfied($plugin),
'requirements' => $plugin->getRequirements(),
'unsatisfiedRequirements' => $plugins->getUnsatisfiedRequirements($plugin),
];
}
2016-12-17 19:52:59 +08:00
}