-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,230 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace App\Filament\Pages; | ||
|
||
use Filament\Pages\Page; | ||
use Filament\Notifications\Notification; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
|
||
class Plugins extends Page | ||
{ | ||
protected static ?string $navigationIcon = 'phosphor-plugs-duotone'; | ||
|
||
protected static string $view = 'filament.pages.plugins'; | ||
|
||
protected static ?int $navigationSort = 9; | ||
|
||
public $plugins = []; | ||
|
||
public function mount() | ||
{ | ||
$this->refreshPlugins(); | ||
} | ||
|
||
private function refreshPlugins() | ||
{ | ||
$this->plugins = $this->getPluginsFromFolder(); | ||
} | ||
|
||
private function getPluginsFromFolder() | ||
{ | ||
$plugins = []; | ||
$plugins_folder = resource_path('plugins'); | ||
|
||
if (!file_exists($plugins_folder)) { | ||
mkdir($plugins_folder); | ||
} | ||
|
||
$scandirectory = scandir($plugins_folder); | ||
|
||
foreach ($scandirectory as $folder) { | ||
if ($folder === '.' || $folder === '..') continue; | ||
|
||
$studlyFolderName = Str::studly($folder); | ||
$pluginFile = $plugins_folder . '/' . $folder . '/' . $studlyFolderName . 'Plugin.php'; | ||
|
||
if (file_exists($pluginFile)) { | ||
$pluginClass = "Wave\\Plugins\\{$studlyFolderName}\\{$studlyFolderName}Plugin"; | ||
if (class_exists($pluginClass) && method_exists($pluginClass, 'getPluginInfo')) { | ||
$plugin = new $pluginClass(app()); | ||
$info = $plugin->getPluginInfo(); | ||
$info['folder'] = $folder; | ||
$info['active'] = $this->isPluginActive($folder); | ||
$plugins[$folder] = $info; | ||
} | ||
} | ||
} | ||
|
||
return $plugins; | ||
} | ||
|
||
private function isPluginActive($folder) | ||
{ | ||
$installedPlugins = $this->getInstalledPlugins(); | ||
return in_array($folder, $installedPlugins); | ||
} | ||
|
||
private function getInstalledPlugins() | ||
{ | ||
$path = resource_path('plugins/installed.json'); | ||
return File::exists($path) ? File::json($path) : []; | ||
} | ||
|
||
private function updateInstalledPlugins($plugins) | ||
{ | ||
$json = json_encode($plugins); | ||
file_put_contents(resource_path('plugins/installed.json'), $json); | ||
} | ||
|
||
public function activate($pluginFolder) | ||
{ | ||
$installedPlugins = $this->getInstalledPlugins(); | ||
if (!in_array($pluginFolder, $installedPlugins)) { | ||
$installedPlugins[] = $pluginFolder; | ||
$this->updateInstalledPlugins($installedPlugins); | ||
|
||
Notification::make() | ||
->title('Successfully activated ' . $pluginFolder . ' plugin') | ||
->success() | ||
->send(); | ||
} | ||
|
||
$this->refreshPlugins(); | ||
} | ||
|
||
public function deactivate($pluginFolder) | ||
{ | ||
$installedPlugins = $this->getInstalledPlugins(); | ||
$installedPlugins = array_diff($installedPlugins, [$pluginFolder]); | ||
$this->updateInstalledPlugins($installedPlugins); | ||
|
||
Notification::make() | ||
->title('Successfully deactivated ' . $pluginFolder . ' plugin') | ||
->success() | ||
->send(); | ||
|
||
$this->refreshPlugins(); | ||
} | ||
|
||
public function deletePlugin($pluginFolder) | ||
{ | ||
$this->deactivate($pluginFolder); | ||
|
||
$pluginPath = resource_path('plugins') . '/' . $pluginFolder; | ||
if (file_exists($pluginPath)) { | ||
File::deleteDirectory($pluginPath); | ||
} | ||
|
||
Notification::make() | ||
->title('Successfully deleted ' . $pluginFolder . ' plugin') | ||
->success() | ||
->send(); | ||
|
||
$this->refreshPlugins(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.