Skip to content

Commit

Permalink
fix issue RealMrHex#7 where pages get created in App/Filament namespa…
Browse files Browse the repository at this point in the history
…ce and folder. And PanelProvider has App/Filament discovery
  • Loading branch information
Eelco committed Jul 28, 2024
1 parent 485b8a0 commit d3453d7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 58 deletions.
132 changes: 77 additions & 55 deletions src/Commands/MakeModularPageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,66 +109,88 @@ public function handle(): int
$panel = Filament::getPanel($panel);
}

if (!$panel)
{
if (!$panel) {
$panels = Filament::getPanels();

/** @var Panel $panel */
$panel = (count($panels) > 1) ? $panels[select(
label : 'Which panel would you like to create this in?',
options: array_map(
fn(Panel $panel): string => $panel->getId(),
$panels,
),
default: Filament::getDefaultPanel()->getId()
)] : Arr::first($panels);
}
$panelOptions = array_map(
fn (Panel $panel): string => $panel->getId(),
$panels,

$panelId = (string)str($panel->getId())->studly();
);

if (empty($resource))
{
$pageDirectories = $panel->getPageDirectories();
$pageNamespaces = $panel->getPageNamespaces();

$pageModuleDirectory = $module->getPath() . "/Filament/$panelId/Pages/";
$moduleNamespace = config('modules.namespace') . "\\{$module->getName()}\Filament\\$panelId\Pages";

$namespace = (count($pageNamespaces) > 1)
?
select(
label : 'Which namespace would you like to create this in?',
options: $pageNamespaces
)
:
(Arr::first($pageNamespaces) ?? $moduleNamespace);
$path = (count($pageDirectories) > 1)
?
$pageDirectories[array_search($namespace, $pageNamespaces)]
:
(Arr::first($pageDirectories) ?? $pageModuleDirectory);
$selectedPanel = select(
label: 'Do you want to create this page for a panel?',
options: [true => 'Yes', false => 'No'],
default: false,
);

if ($selectedPanel) {
/** @var Panel $panel */
$panel = (count($panels) > 1) ? $panels[select(
label: 'In which panel would you like to create this in?',
options: array_map(
fn (Panel $panel): string => $panel->getId(),
$panels,
),
default: Filament::getDefaultPanel()->getId()
)] : Arr::first($panels);

$panelId = (string)str($panel->getId())->studly();
}
}
else
{
$resourceDirectories = $panel->getResourceDirectories();
$resourceNamespaces = $panel->getResourceNamespaces();

$resourceModuleDirectory = $module->getPath() . "/Filament/$panelId/Resources/";
$moduleNamespace = config('modules.namespace') . "\\{$module->getName()}\Filament\\$panelId\Resources";

$resourceNamespace = (count($resourceNamespaces) > 1)
?
select(
label : 'Which namespace would you like to create this in?',
options: $resourceNamespaces
)
:
(Arr::first($resourceNamespaces) ?? $moduleNamespace);
$resourcePath = (count($resourceDirectories) > 1)
?
$resourceDirectories[array_search($resourceNamespace, $resourceNamespaces)]
:
(Arr::first($resourceDirectories) ?? $resourceModuleDirectory);

if (empty($resource)) {
if ($selectedPanel) {
$pageDirectories = $panel->getPageDirectories();
$pageNamespaces = $panel->getPageNamespaces();

$pageModuleDirectory = $module->getPath() . "/Filament/$panelId/Pages/";
$moduleNamespace = config('modules.namespace') . "\\{$module->getName()}\Filament\\$panelId\Pages";

$namespace = (count($pageNamespaces) > 1)
?
select(
label: 'Which namespace would you like to create this in?',
options: $pageNamespaces
)
: (Arr::first($pageNamespaces) ?? $moduleNamespace);
$path = (count($pageDirectories) > 1)
?
$pageDirectories[array_search($namespace, $pageNamespaces)]
: (Arr::first($pageDirectories) ?? $pageModuleDirectory);
} else {
$pageModuleDirectory = $module->getPath() . "/Filament/Pages/";
$moduleNamespace = config('modules.namespace') . "\\{$module->getName()}\Filament\\Pages";

$namespace = $moduleNamespace;
$path = $pageModuleDirectory;
}
} else {
if ($selectedPanel) {
$resourceDirectories = $panel->getResourceDirectories();
$resourceNamespaces = $panel->getResourceNamespaces();

$resourceModuleDirectory = $module->getPath() . "/Filament/$panelId/Resources/";
$moduleNamespace = config('modules.namespace') . "\\{$module->getName()}\Filament\\$panelId\Resources";

$resourceNamespace = (count($resourceNamespaces) > 1)
?
select(
label: 'Which namespace would you like to create this in?',
options: $resourceNamespaces
)
: (Arr::first($resourceNamespaces) ?? $moduleNamespace);
$resourcePath = (count($resourceDirectories) > 1)
?
$resourceDirectories[array_search($resourceNamespace, $resourceNamespaces)]
: (Arr::first($resourceDirectories) ?? $resourceModuleDirectory);
} else {
$resourceModuleDirectory = $module->getPath() . "/Filament/Resources/";
$moduleNamespace = config('modules.namespace') . "\\{$module->getName()}\Filament\\Resources";

$resourceNamespace = $moduleNamespace;
$resourcePath = $resourceModuleDirectory;
}
}


Expand Down
1 change: 1 addition & 0 deletions src/Commands/MakeModularPanelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function handle(): int
'class' => $class,
'directory' => str($id)->studly(),
'id' => $id,
'module' => $module_name,
'namespace' => $moduleNamespace,
]);

Expand Down
8 changes: 5 additions & 3 deletions stubs/PanelProvider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class {{ class }} extends PanelProvider
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: app_path('Filament/{{ directory }}/Resources'), for: 'App\\Filament\\{{ directory }}\\Resources')
->discoverPages(in: app_path('Filament/{{ directory }}/Pages'), for: 'App\\Filament\\{{ directory }}\\Pages')
->login()
->discoverClusters(in:module_path('{{ module }}') . '/Filament/{{ directory }}/Clusters', for: 'Modules\\{{ module }}\\Filament\\{{ directory }}\\Clusters')
->discoverResources(in: module_path('{{ module }}') . '/Filament/{{ directory }}/Resources', for: 'Modules\\{{ module }}\\Filament\\{{ directory }}\\Resources')
->discoverPages(in: module_path('{{ module }}') . '/Filament/{{ directory }}/Pages', for: 'Modules\\{{ module }}\\Filament\\{{ directory }}\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/{{ directory }}/Widgets'), for: 'App\\Filament\\{{ directory }}\\Widgets')
->discoverWidgets(in: module_path('{{ module }}') . '/Filament/{{ directory }}/Widgets', for: 'Modules\\{{ module }}\\Filament\\{{ directory }}\\Widgets')
->widgets([
Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class,
Expand Down

0 comments on commit d3453d7

Please sign in to comment.