133 lines
5.3 KiB
PHP
133 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Api\Plugins\ApiPluginInterface;
|
|
use App\Filament\Resources\PluginResource\Plugin;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Concerns\InteractsWithTable;
|
|
use Filament\Tables\Contracts\HasTable;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\File;
|
|
use App\Models\ApiProvider;
|
|
use Filament\Tables\Actions\Action;
|
|
|
|
class ListPlugins extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-puzzle-piece';
|
|
|
|
protected static string $view = 'filament.pages.list-plugins';
|
|
|
|
protected static ?string $navigationGroup = 'Settings';
|
|
|
|
protected static ?string $title = 'Plugins';
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
$plugins = new Collection();
|
|
$path = app_path('Api/Plugins');
|
|
|
|
if (File::exists($path)) {
|
|
$files = File::files($path);
|
|
foreach ($files as $file) {
|
|
$filename = $file->getFilenameWithoutExtension();
|
|
$class = 'App\\Api\\Plugins\\' . $filename;
|
|
|
|
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
|
|
try {
|
|
$apiProvider = ApiProvider::where('plugin', $filename)->first();
|
|
if(!$apiProvider) continue;
|
|
$instance = new $class($apiProvider);
|
|
$plugins->add(new Plugin([
|
|
'id' => $instance->getIdentifier(),
|
|
'name' => $instance->getName(),
|
|
'identifier' => $instance->getIdentifier(),
|
|
'enabled' => $instance->isEnabled(),
|
|
'file_path' => $file->getPathname(),
|
|
]));
|
|
} catch (\Exception $e) {
|
|
// Log error or handle gracefully if a plugin cannot be instantiated
|
|
// For now, we'll just skip it
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $table
|
|
->query(Plugin::query()->setCollection($plugins))
|
|
->columns([
|
|
TextColumn::make('name')
|
|
->label('Name')
|
|
->searchable()
|
|
->sortable(),
|
|
TextColumn::make('identifier')
|
|
->label('Identifier'),
|
|
IconColumn::make('enabled')
|
|
->label('Enabled')
|
|
->boolean(),
|
|
TextColumn::make('file_path')
|
|
->label('File Path')
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->actions([
|
|
Action::make('toggle_enabled')
|
|
->label(fn ($record) => $record->enabled ? 'Disable' : 'Enable')
|
|
->icon(fn ($record) => $record->enabled ? 'heroicon-o-x-circle' : 'heroicon-o-check-circle')
|
|
->action(function ($record) {
|
|
try {
|
|
$apiProvider = ApiProvider::where('plugin', $record->identifier)->first();
|
|
if(!$apiProvider) throw new \Exception('ApiProvider not found');
|
|
$pluginClass = 'App\\Api\\Plugins\\' . $record->identifier;
|
|
$plugin = new $pluginClass($apiProvider);
|
|
if ($record->enabled) {
|
|
$plugin->disable();
|
|
} else {
|
|
$plugin->enable();
|
|
}
|
|
Notification::make()
|
|
->title('Plugin status updated')
|
|
->success()
|
|
->send();
|
|
} catch (\Exception $e) {
|
|
Notification::make()
|
|
->title('Error updating plugin status')
|
|
->body($e->getMessage())
|
|
->danger()
|
|
->send();
|
|
}
|
|
}),
|
|
|
|
Action::make('delete')
|
|
->label('Delete')
|
|
->icon('heroicon-o-trash')
|
|
->color('danger')
|
|
->requiresConfirmation()
|
|
->action(function ($record) {
|
|
try {
|
|
File::delete($record->file_path);
|
|
Notification::make()
|
|
->title('Plugin deleted successfully')
|
|
->success()
|
|
->send();
|
|
} catch (\Exception $e) {
|
|
Notification::make()
|
|
->title('Error deleting plugin')
|
|
->body($e->getMessage())
|
|
->danger()
|
|
->send();
|
|
}
|
|
}),
|
|
])
|
|
->bulkActions([
|
|
// No bulk actions for now
|
|
]);
|
|
}
|
|
|
|
// Removed getTableRecords() as data is now provided via query()
|
|
} |