refactor: improve getPanel method with isStrict parameter and fallback

- Introduced default isStrict parameter set to true for getPanel method.
- Extracted panel retrieval logic into protected findPanel method.
- findPanel method strictly adheres to isStrict parameter, returning null if panel is not found.
- Maintained fallback in getPanel method to return default panel if not found.
- Ensured commands with direct user input set isStrict to false.
- This approach balances strictness with user experience, preventing potential issues in methods like getRouteName.
This commit is contained in:
Marcelo Guerra
2024-06-04 22:56:55 -03:00
parent 47c6baa412
commit 92900a16cf
10 changed files with 36 additions and 12 deletions
@@ -45,7 +45,7 @@ class MakeClusterCommand extends Command
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
@@ -53,7 +53,7 @@ class MakePageCommand extends Command
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
@@ -62,7 +62,7 @@ class MakeRelationManagerCommand extends Command
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
@@ -85,7 +85,7 @@ class MakeResourceCommand extends Command
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
@@ -45,7 +45,7 @@ class MakeThemeCommand extends Command
$panel = $this->argument('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
+1 -1
View File
@@ -63,7 +63,7 @@ use Illuminate\Support\Facades\Facade;
* @method static array<string | int, NavigationGroup | string> getNavigationGroups()
* @method static array<NavigationItem> getNavigationItems()
* @method static array getPages()
* @method static Panel getPanel(?string $id = null)
* @method static Panel getPanel(?string $id = null, bool $isStrict = true)
* @method static array<string, Panel> getPanels()
* @method static Plugin getPlugin(string $id)
* @method static string | null getProfileUrl(array $parameters = [])
+2 -2
View File
@@ -279,9 +279,9 @@ class FilamentManager
return $this->getCurrentPanel()->getPages();
}
public function getPanel(?string $id = null): Panel
public function getPanel(?string $id = null, bool $isStrict = true): Panel
{
return app(PanelRegistry::class)->get($id);
return app(PanelRegistry::class)->get($id, $isStrict);
}
/**
+26 -2
View File
@@ -44,9 +44,33 @@ class PanelRegistry
);
}
public function get(?string $id = null): Panel
/**
* @throws NoDefaultPanelSetException
*/
public function get(?string $id = null, bool $isStrict = true): Panel
{
return $this->panels[$id] ?? $this->getDefault();
return $this->findPanel($id, $isStrict) ?? $this->getDefault();
}
protected function findPanel(?string $id = null, bool $isStrict = true): ?Panel
{
if ($id === null) {
return null;
}
if ($isStrict) {
return $this->panels[$id] ?? null;
}
$sanitizedPanels = [];
foreach ($this->panels as $key => $panel) {
$sanitizedKey = strtolower(str_replace(['-', '_'], '', $key));
$sanitizedPanels[$sanitizedKey] = $panel;
}
$sanitizedId = strtolower(str_replace(['-', '_'], '', $id));
return $sanitizedPanels[$sanitizedId] ?? null;
}
/**
@@ -50,7 +50,7 @@ class MakeSettingsPageCommand extends Command
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
@@ -81,7 +81,7 @@ class MakeWidgetCommand extends Command
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel);
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {