| Current Path : /var/www/html/dynaweb-starter/plugin/dynaweb-widget/src/ |
| Current File : /var/www/html/dynaweb-starter/plugin/dynaweb-widget/src/DynawebWidgetPlugin.php |
<?php
namespace Dynaweb\DynawebWidget;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Dynaweb\DynawebWidget\Filament\Widgets\DynawebWidget;
use Dynaweb\DynawebWidget\Filament\Resources\DashboardResource;
use Dynaweb\DynawebWidget\Repositories\DashboardRepository;
use Dynaweb\DynawebWidget\Classes\CreateFileDashboard;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Route;
use Filament\Support\Facades\FilamentView;
use Illuminate\Support\Facades\Blade;
class DynawebWidgetPlugin implements Plugin
{
public $fly;
public function getId(): string
{
return 'dynaweb-widget';
}
protected function generateNameFromClass($class)
{
$namespace = str_replace(
['/', '\\'],
'.',
trim(trim(config('livewire.class_namespace')), '\\')
);
$class = str_replace(
['/', '\\'],
'.',
trim(trim($class, '/'), '\\')
);
$namespace = collect(explode('.', $namespace))
->map(fn ($i) => \Illuminate\Support\Str::kebab($i))
->implode('.');
$fullName = str(collect(explode('.', $class))
->map(fn ($i) => \Illuminate\Support\Str::kebab($i))
->implode('.'));
if ($fullName->startsWith('.')) {
$fullName = $fullName->substr(1);
}
// If using an index component in a sub folder, remove the '.index' so the name is the subfolder name...
if ($fullName->endsWith('.index')) {
$fullName = $fullName->replaceLast('.index', '');
}
if ($fullName->startsWith($namespace)) {
return (string) $fullName->substr(strlen($namespace) + 1);
}
return (string) $fullName;
}
public function loadWidgetClass()
{
$dynawebWidget = CreateFileDashboard::loadDashboard();
$class = [];
if(!empty($dynawebWidget)) {
foreach($dynawebWidget as $widget) {
/** base class */
$class[] = "Dynaweb\DynawebWidget\Filament\Widgets\\$widget";
}
}
return $class;
}
public function register(Panel $panel): void
{
try {
foreach($this->loadWidgetClass() as $class) {
/** register component for livewire */
app(\Livewire\Livewire::component($this->generateNameFromClass($class), $class));
}
} catch (\Throwable $t) {
$this->displayError($t, $widget);
}
$panel->resources([DashboardResource::class]);
}
public function displayError($t, $widget)
{
$error = $t->getMessage();
/*** display error relating dashboard */
FilamentView::registerRenderHook(
'panels::page.header-widgets.after',
fn (): string => Blade::render(
<<<HTML
<p class="mb-5 bg-danger-500/10 border-1 p-3 rounded text-danger-700">Error dashboard $widget:- <br>$error <br> Please check your dashboard code. </p>
HTML
),
);
}
public function boot(Panel $panel): void
{
//
}
public static function make(): static
{
return app(static::class);
}
public static function get(): static
{
/** @var static $plugin */
$plugin = filament(app(static::class)->getId());
return $plugin;
}
}