2016-12-17 19:52:59 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-06-29 16:37:39 +08:00
|
|
|
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) {
|
2018-06-29 20:54:40 +08:00
|
|
|
$constraint = $detail['constraint'];
|
2018-06-29 16:37:39 +08:00
|
|
|
if (! $detail['version']) {
|
2019-08-13 18:42:17 +08:00
|
|
|
return trans('admin.plugins.operations.unsatisfied.disabled', compact('name'));
|
2018-06-29 16:37:39 +08:00
|
|
|
} else {
|
2019-08-13 18:42:17 +08:00
|
|
|
return trans('admin.plugins.operations.unsatisfied.version', compact('name', 'constraint'));
|
2018-06-29 16:37:39 +08:00
|
|
|
}
|
2019-08-13 18:42:17 +08:00
|
|
|
})->values()->all();
|
2018-06-29 16:37:39 +08:00
|
|
|
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('admin.plugins.operations.unsatisfied.notice'), 1, compact('reason'));
|
2018-06-29 16:37:39 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-01-02 12:19:34 +08:00
|
|
|
}
|
2016-12-17 19:52:59 +08:00
|
|
|
|
2017-01-02 12:19:34 +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
|
|
|
];
|
2017-01-02 12:19:34 +08:00
|
|
|
})
|
2018-08-19 17:39:33 +08:00
|
|
|
->values();
|
2016-12-17 19:52:59 +08:00
|
|
|
}
|
|
|
|
}
|