mirror of
https://github.com/pelican-dev/panel.git
synced 2025-11-08 14:49:27 +01:00
add plugin model
This commit is contained in:
parent
e0c4e47a6c
commit
0221569ee5
10
app/Contracts/Plugins/HasPluginSettings.php
Normal file
10
app/Contracts/Plugins/HasPluginSettings.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts\Plugins;
|
||||
|
||||
interface HasPluginSettings
|
||||
{
|
||||
public function getSettingsForm(): array;
|
||||
|
||||
public function saveSettings(array $data): void;
|
||||
}
|
||||
35
app/Enums/PluginStatus.php
Normal file
35
app/Enums/PluginStatus.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use Filament\Support\Contracts\HasLabel;
|
||||
|
||||
enum PluginStatus: string implements HasLabel
|
||||
{
|
||||
case Disabled = 'disabled';
|
||||
case Enabled = 'enabled';
|
||||
case Errored = 'errored';
|
||||
|
||||
public function icon(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Disabled => 'tabler-heart-off',
|
||||
self::Enabled => 'tabler-heart-check',
|
||||
self::Errored => 'tabler-heart-x',
|
||||
};
|
||||
}
|
||||
|
||||
public function color(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Disabled => 'gray',
|
||||
self::Enabled => 'success',
|
||||
self::Errored => 'danger',
|
||||
};
|
||||
}
|
||||
|
||||
public function getLabel(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
149
app/Models/Plugin.php
Normal file
149
app/Models/Plugin.php
Normal file
@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\PluginStatus;
|
||||
use Illuminate\Database\Eloquent\Model as IlluminateModel;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Sushi\Sushi;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
* @property string $name
|
||||
* @property string $author
|
||||
* @property string $version
|
||||
* @property string|null $description
|
||||
* @property string|null $url
|
||||
* @property string $class
|
||||
* @property string $namespace
|
||||
* @property PluginStatus $status
|
||||
* @property string|null $status_message
|
||||
* @property string $panel
|
||||
* @property string|null $panel_version
|
||||
* @property string $category
|
||||
*/
|
||||
class Plugin extends IlluminateModel
|
||||
{
|
||||
use Sushi;
|
||||
|
||||
protected $primaryKey = 'id';
|
||||
protected $keyType = 'string';
|
||||
public $incrementing = false;
|
||||
|
||||
public function getSchema(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'string',
|
||||
'name' => 'string',
|
||||
'author' => 'string',
|
||||
'version' => 'string',
|
||||
'description' => 'string',
|
||||
'url' => 'string',
|
||||
'namespace' => 'string',
|
||||
'class' => 'string',
|
||||
'status' => 'string',
|
||||
'status_message' => 'string',
|
||||
'panel' => 'string',
|
||||
'panel_version' => 'string',
|
||||
'category' => 'string',
|
||||
];
|
||||
}
|
||||
|
||||
public function getRows(): array
|
||||
{
|
||||
$fileSystem = app(Filesystem::class);
|
||||
|
||||
$plugins = [];
|
||||
|
||||
$directories = $fileSystem->directories(base_path('plugins/'));
|
||||
foreach ($directories as $directory) {
|
||||
$plugin = $fileSystem->basename($directory);
|
||||
$plugins[] = $fileSystem->json(plugin_path($plugin, 'plugin.json'), JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
protected function sushiShouldCache()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'status' => PluginStatus::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function shouldLoad(string $panelId): bool
|
||||
{
|
||||
return !$this->isDisabled() && ($this->panel === 'both' || $this->panel === $panelId);
|
||||
}
|
||||
|
||||
public function isDisabled(): bool
|
||||
{
|
||||
return $this->status === PluginStatus::Disabled;
|
||||
}
|
||||
|
||||
public function hasErrored(): bool
|
||||
{
|
||||
return $this->status === PluginStatus::Errored;
|
||||
}
|
||||
|
||||
public function isCompatible(): bool
|
||||
{
|
||||
if ($this->panel_version === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (config('app.version') === 'canary') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->panel_version === config('app.version');
|
||||
}
|
||||
|
||||
public function getFullClass(): string
|
||||
{
|
||||
return '\\' . $this->namespace . '\\' . $this->class;
|
||||
}
|
||||
|
||||
public function hasSettings(): bool
|
||||
{
|
||||
$class = $this->getFullClass();
|
||||
if (class_exists($class) && method_exists($class, 'get')) {
|
||||
$pluginObject = ($class)::get();
|
||||
|
||||
return method_exists($pluginObject, 'getSettingsForm') && method_exists($pluginObject, 'saveSettings');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getSettingsForm(): array
|
||||
{
|
||||
$class = $this->getFullClass();
|
||||
if (class_exists($class) && method_exists($class, 'get')) {
|
||||
$pluginObject = ($class)::get();
|
||||
|
||||
if (method_exists($pluginObject, 'getSettingsForm')) {
|
||||
return $pluginObject->getSettingsForm();
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function saveSettings(array $data): void
|
||||
{
|
||||
$class = $this->getFullClass();
|
||||
if (class_exists($class) && method_exists($class, 'get')) {
|
||||
$pluginObject = ($class)::get();
|
||||
|
||||
if (method_exists($pluginObject, 'saveSettings')) {
|
||||
$pluginObject->saveSettings($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -87,6 +87,13 @@ if (!function_exists('resolve_path')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('plugin_path')) {
|
||||
function plugin_path(string $plugin, string ...$paths): string
|
||||
{
|
||||
return str_replace('//', '', base_path('plugins/') . $plugin . '/' . implode('/', $paths));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('get_ip_from_hostname')) {
|
||||
function get_ip_from_hostname(string $hostname): string|bool
|
||||
{
|
||||
|
||||
2
composer.lock
generated
2
composer.lock
generated
@ -15297,4 +15297,4 @@
|
||||
"php": "8.2"
|
||||
},
|
||||
"plugin-api-version": "2.6.0"
|
||||
}
|
||||
}
|
||||
2
plugins/.gitignore
vendored
Normal file
2
plugins/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Loading…
x
Reference in New Issue
Block a user