Skip to content

Commit 2ac4b47

Browse files
authored
fix resource generation command not parsing passed model class properly (#163)
When starting to use this package in a project I'm working on I noticed the model argument wasn't being used properly It would parse the FQDN of `Module\Foo\Bar` as `Bar\Bar` because it replaced the entire FQDN with the class basename In addition removing the string `Resource` from the basename seems to be somewhat counter intuitive, if wanted I could roll those changes back I also took the liberty of adding an early return and simplify the code I worked on a bit
2 parents 80c6ee3 + 557801b commit 2ac4b47

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

src/Commands/ModuleMakeFilamentResourceCommand.php

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -62,60 +62,55 @@ public function ensureModuleArgument(): void
6262

6363
public function ensureModelNamespace(): void
6464
{
65-
$modelNamespace = $this->input->getOption('model-namespace');
66-
if (! $modelNamespace) {
67-
// try to get from name
68-
$name = $this->input->getArgument('model');
69-
if ($name) {
70-
$modelName = str_replace('Resource', '', class_basename($name));
71-
} else {
72-
$modelName = select('Please select the model within this module for the resource:', $this->possibleFqnModels());
73-
}
65+
if ($this->input->hasOption('model-namespace')) {
66+
return;
67+
}
68+
// try to get from name
69+
$name = $this->input->getArgument('model')
70+
?: select('Please select the model within this module for the resource:', $this->possibleFqnModels());
7471

75-
$modelClass = class_basename($modelName);
76-
$modelNamespace = str(trim($modelName, '\\'))->beforeLast("\\{$modelClass}")->toString();
72+
$modelClass = class_basename($name);
73+
$modelNamespace = str(trim($name, '\\'))->beforeLast("\\{$modelClass}")->toString();
7774

78-
if (! $modelName) {
79-
$this->error('No model namespace selected. Aborting resource creation.');
80-
exit(1);
81-
}
82-
$modelName = $modelClass;
75+
if (! $modelNamespace) {
76+
$this->error('No model namespace selected. Aborting resource creation.');
77+
exit(1);
78+
}
8379

84-
$this->input->setOption('model-namespace', $modelNamespace);
85-
$this->input->setArgument('model', $modelName);
80+
$this->input->setOption('model-namespace', $modelNamespace);
81+
$this->input->setArgument('model', $modelClass);
8682

87-
$this->output->info("Using model namespace: {$modelNamespace}");
88-
$this->output->info("Using model name: {$modelName}");
89-
}
83+
$this->output->info("Using model namespace: {$modelNamespace}");
84+
$this->output->info("Using model name: {$modelClass}");
9085
}
9186

9287
public function ensurePanel()
9388
{
9489
$defaultPanel = filament()->getDefaultPanel();
9590
if (! FilamentModules::getMode()->shouldRegisterPanels()) {
9691
$this->panel = $defaultPanel;
97-
} else {
98-
$modulePanels = FilamentModules::getModulePanels($this->getModule());
99-
if (count($modulePanels) === 0) {
100-
$this->panel = $defaultPanel;
92+
return;
93+
}
94+
$modulePanels = FilamentModules::getModulePanels($this->getModule());
95+
if (count($modulePanels) === 0) {
96+
$this->panel = $defaultPanel;
10197

102-
return;
103-
}
104-
$options = [
105-
$defaultPanel->getId(),
106-
...collect($modulePanels)->map(fn ($panel) => $panel->getId())->values()->all(),
107-
];
108-
$panelId = select(
109-
label: 'Please select the Filament panel to create the resource in:',
110-
options: $options,
111-
default: $defaultPanel->getId(),
112-
);
113-
$this->input->setOption('panel', $panelId);
114-
$this->panel = filament()->getPanel($panelId, isStrict: false);
115-
if (! $this->panel) {
116-
$this->error("Panel [{$panelId}] not found. Aborting resource creation.");
117-
exit(1);
118-
}
98+
return;
99+
}
100+
$options = [
101+
$defaultPanel->getId(),
102+
...collect($modulePanels)->map(fn ($panel) => $panel->getId())->values()->all(),
103+
];
104+
$panelId = select(
105+
label: 'Please select the Filament panel to create the resource in:',
106+
options: $options,
107+
default: $defaultPanel->getId(),
108+
);
109+
$this->input->setOption('panel', $panelId);
110+
$this->panel = filament()->getPanel($panelId, isStrict: false);
111+
if (! $this->panel) {
112+
$this->error("Panel [{$panelId}] not found. Aborting resource creation.");
113+
exit(1);
119114
}
120115
}
121116

0 commit comments

Comments
 (0)