mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
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:
@@ -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) {
|
||||
|
||||
@@ -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 = [])
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user