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 App\Services\PluginManager;
|
2019-12-14 11:10:37 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-12-17 19:52:59 +08:00
|
|
|
|
|
|
|
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);
|
2019-12-05 19:28:12 +08:00
|
|
|
if ($plugin && $plugin->isEnabled()) {
|
|
|
|
if ($plugin->hasConfigClass()) {
|
|
|
|
return app()->call($plugin->getConfigClass().'@render');
|
|
|
|
} elseif ($plugin->hasConfigView()) {
|
|
|
|
return $plugin->getConfigView();
|
|
|
|
} else {
|
|
|
|
return abort(404, trans('admin.plugins.operations.no-config-notice'));
|
|
|
|
}
|
2017-01-17 21:41:20 +08:00
|
|
|
} 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
|
|
|
|
2019-12-08 23:58:44 +08:00
|
|
|
public function readme(PluginManager $plugins, $name)
|
|
|
|
{
|
|
|
|
$plugin = $plugins->get($name);
|
|
|
|
if (empty($plugin)) {
|
|
|
|
return abort(404, trans('admin.plugins.operations.no-readme-notice'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$readmePath = $plugin->getReadme();
|
|
|
|
if (empty($readmePath)) {
|
|
|
|
return abort(404, trans('admin.plugins.operations.no-readme-notice'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $plugin->title;
|
|
|
|
$path = $plugin->getPath().'/'.$readmePath;
|
|
|
|
$content = resolve('parsedown')->text(file_get_contents($path));
|
|
|
|
|
|
|
|
return view('admin.plugin.readme', compact('content', 'title'));
|
|
|
|
}
|
|
|
|
|
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-17 10:57:38 +08:00
|
|
|
$result = $plugins->enable($name);
|
|
|
|
|
|
|
|
if ($result === true) {
|
|
|
|
return json(trans('admin.plugins.operations.enabled', ['plugin' => $plugin->title]), 0);
|
|
|
|
} else {
|
2019-12-14 11:10:37 +08:00
|
|
|
$reason = $plugins->formatUnresolved($result['unsatisfied'], $result['conflicts']);
|
2019-08-21 11:48:42 +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
|
|
|
}
|
|
|
|
|
2019-12-14 11:10:37 +08:00
|
|
|
// no break
|
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(),
|
2019-12-08 23:58:44 +08:00
|
|
|
'readme' => (bool) $plugin->getReadme(),
|
2019-12-05 19:28:12 +08:00
|
|
|
'config' => $plugin->hasConfig(),
|
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
|
|
|
}
|
|
|
|
}
|