Files
laralog/app/Filament/Pages/Plugins.php
T
ak 4f59afb7ce feat: 插件依赖强制校验 + 文章后台表单/表格注入 hook
- PluginManager: requires 依赖链校验(enable 需依赖已启用,disable 阻止有启用依赖者的插件,boot 跳过依赖未满足插件),all() 输出依赖状态供后台展示
- Plugins 页:卡片显示依赖链徽章,依赖未满足时禁用启用按钮,启停失败弹错误通知
- PostResource: form()/table() 增加 filament.post_form / filament.post_table 注入点
- membership 插件:注入「付费设置」Section(meta.price 单篇价格 + meta.members_only 会员专享)与表格价格/会员列
2026-08-12 00:18:40 +08:00

119 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Pages;
use App\Blog\Services\MarketplaceClient;
use App\Blog\Services\PackageInstaller;
use App\Blog\Support\PluginManager;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Facades\File;
use Livewire\WithFileUploads;
class Plugins extends Page
{
use WithFileUploads;
protected static \UnitEnum|string|null $navigationGroup = '管理';
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-puzzle-piece';
protected static ?string $navigationLabel = '插件管理';
protected static ?string $title = '插件管理';
protected string $view = 'filament.pages.plugins';
public $zip;
public array $marketItems = [];
public function getPluginsProperty(): array
{
return app(PluginManager::class)->all();
}
public function enablePlugin(string $key): void
{
try {
app(PluginManager::class)->enable($key);
Notification::make()->title('已启用:'.$key)->success()->send();
} catch (\Throwable $e) {
Notification::make()->title('启用失败')->body($e->getMessage())->danger()->send();
}
}
public function disablePlugin(string $key): void
{
try {
app(PluginManager::class)->disable($key);
Notification::make()->title('已停用:'.$key)->warning()->send();
} catch (\Throwable $e) {
Notification::make()->title('停用失败')->body($e->getMessage())->danger()->send();
}
}
public function uninstallPlugin(string $key): void
{
try {
app(PackageInstaller::class)->uninstallPlugin($key);
Notification::make()->title('已卸载:'.$key)->success()->send();
} catch (\Throwable $e) {
Notification::make()->title('卸载失败')->body($e->getMessage())->danger()->send();
}
}
public function uploadZip(): void
{
$this->validate([
'zip' => ['required', 'file', 'mimes:zip'],
]);
try {
$result = app(PackageInstaller::class)->installZip($this->zip->getRealPath());
Notification::make()
->title('安装成功:'.($result['type'] === 'plugin' ? '插件' : '主题').' '.$result['key'])
->success()
->send();
} catch (\Throwable $e) {
Notification::make()->title('安装失败')->body($e->getMessage())->danger()->send();
}
$this->zip = null;
}
public function loadMarket(): void
{
try {
$this->marketItems = app(MarketplaceClient::class)->items();
Notification::make()->title('市场已刷新:'.count($this->marketItems).' 个包')->success()->send();
} catch (\Throwable $e) {
Notification::make()->title('市场加载失败')->body($e->getMessage())->danger()->send();
}
}
public function installFromMarket(string $index): void
{
$item = $this->marketItems[(int) $index] ?? null;
if (! $item) {
return;
}
try {
$result = app(PackageInstaller::class)->installFromMarket($item);
Notification::make()->title('安装成功:'.$result['key'])->success()->send();
} catch (\Throwable $e) {
Notification::make()->title('安装失败')->body($e->getMessage())->danger()->send();
}
}
public function getMarketConfiguredProperty(): bool
{
return (bool) config('market.url');
}
}