Skip to content

Commit 3bcecfc

Browse files
authored
Feature: Support Generation of Filament classes for custom Module classes (#147)
- Refactored the code to support proper generation of resources, clusters and plugins for custom module paths
2 parents 1bf3aaa + 6abc4db commit 3bcecfc

File tree

6 files changed

+38
-17
lines changed

6 files changed

+38
-17
lines changed

src/Commands/ModuleFilamentInstallCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private function makeDirectory(string $dir): void
144144
protected function createDefaultFilamentPlugin(): void
145145
{
146146
$module = $this->getModule();
147-
$this->call('module:make:filament-plugin', [
147+
$this->call('module:filament:plugin', [
148148
'name' => $module->getStudlyName() . 'Plugin',
149149
'module' => $module->getStudlyName(),
150150
]);
@@ -153,7 +153,7 @@ protected function createDefaultFilamentPlugin(): void
153153
protected function createDefaultFilamentCluster(): void
154154
{
155155
$module = $this->getModule();
156-
$this->call('module:make:filament-cluster', [
156+
$this->call('module:filament:cluster', [
157157
'name' => $module->getStudlyName(),
158158
'module' => $module->getStudlyName(),
159159
'--panel' => filament()->getDefaultPanel()->getId(),

src/Commands/ModuleMakeFilamentPluginCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Coolsam\Modules\Concerns\GeneratesModularFiles;
66
use Illuminate\Console\GeneratorCommand;
77

8+
use function Laravel\Prompts\select;
9+
810
class ModuleMakeFilamentPluginCommand extends GeneratorCommand
911
{
1012
use GeneratesModularFiles;
@@ -37,4 +39,19 @@ protected function stubReplacements(): array
3739
'pluginId' => str($this->argument('name'))->replace('Plugin', '')->studly()->lower()->toString(),
3840
];
3941
}
42+
43+
public function handle(): ?bool
44+
{
45+
$this->ensureModule();
46+
47+
return parent::handle(); // TODO: Change the autogenerated stub
48+
}
49+
50+
public function ensureModule()
51+
{
52+
if (! $this->argument('module')) {
53+
$module = select('Please select the module to create the plugin in:', \Nwidart\Modules\Facades\Module::allEnabled());
54+
$this->input->setArgument('module', $module);
55+
}
56+
}
4057
}

src/Commands/ModuleMakeFilamentResourceCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public function ensureModelNamespace(): void
7171
} else {
7272
$modelName = select('Please select the model within this module for the resource:', $this->possibleFqnModels());
7373
}
74-
$modelNamespace = $this->rootNamespace() . '\\Models';
75-
// Ask to select model namespace
74+
75+
$modelClass = class_basename($modelName);
76+
$modelNamespace = str(trim($modelName, '\\'))->rtrim("\\{$modelClass}")->toString();
7677

7778
if (! $modelName) {
7879
$this->error('No model namespace selected. Aborting resource creation.');
7980
exit(1);
8081
}
81-
82-
$modelName = class_basename($modelName);
82+
$modelName = $modelClass;
8383

8484
$this->input->setOption('model-namespace', $modelNamespace);
8585
$this->input->setArgument('model', $modelName);
@@ -96,6 +96,11 @@ public function ensurePanel()
9696
$this->panel = $defaultPanel;
9797
} else {
9898
$modulePanels = FilamentModules::getModulePanels($this->getModule());
99+
if (count($modulePanels) === 0) {
100+
$this->panel = $defaultPanel;
101+
102+
return;
103+
}
99104
$options = [
100105
$defaultPanel->getId(),
101106
...collect($modulePanels)->map(fn ($panel) => $panel->getId())->values()->all(),

src/Concerns/GeneratesModularFiles.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ protected function rootNamespace(): string
4444

4545
protected function getPath($name): string
4646
{
47-
$appFolder = str(config('modules.paths.app_folder', 'app'));
47+
$appFolder = trim(config('modules.paths.app_folder', 'app/'), '/\\');
4848
$name = Str::replaceFirst($this->rootNamespace(), $appFolder, $name);
4949

5050
return $this->getModule()->getExtraPath(str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php');
5151
}
5252

5353
protected function possibleModels()
5454
{
55-
$modelPath = $this->getModule()->appPath('Models');
55+
$modelPath = $this->getModule()->appPath(config('modules.paths.generator.model.path', 'Models'));
5656

5757
return collect(Finder::create()->files()->depth(0)->in($modelPath))
5858
->map(fn ($file) => $file->getBasename('.php'))
@@ -64,7 +64,7 @@ protected function possibleModels()
6464
public function possibleFqnModels(): array
6565
{
6666
return collect($this->possibleModels())
67-
->map(fn ($model) => $this->getModule()->appNamespace("Models\\{$model}"))
67+
->map(fn ($model) => str($this->getModule()->appNamespace('Models'))->trim('\\')->append("\\{$model}")->toString())
6868
->all();
6969
}
7070

src/ModulesPlugin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ protected function getModulePlugins(): array
8585
}
8686
// get a glob of all Filament plugins
8787
$basePath = str(config('modules.paths.modules', 'Modules'));
88-
$appFolder = str(config('modules.paths.app_folder', 'app'));
89-
$pattern = $basePath . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . $appFolder . DIRECTORY_SEPARATOR . 'Filament' . DIRECTORY_SEPARATOR . '*Plugin.php';
88+
$appFolder = trim(config('modules.paths.app_folder', 'app'), '/\\');
89+
$appPath = $appFolder . DIRECTORY_SEPARATOR;
90+
$pattern = str($basePath . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . $appPath . 'Filament' . DIRECTORY_SEPARATOR . '*Plugin.php')->replace('//', '/')->toString();
9091
$pluginPaths = glob($pattern);
9192

9293
return collect($pluginPaths)->map(fn ($path) => FilamentModules::convertPathToNamespace($path))->toArray();

src/ModulesServiceProvider.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,21 @@ protected function registerModuleMacros(): void
212212
$relativeNamespace = trim($relativeNamespace, '\\');
213213
$studlyName = $this->getStudlyName();
214214

215-
return trim("{$base}\\{$studlyName}\\{$relativeNamespace}", '\\');
215+
return str($base)->append('\\')->append($studlyName)->append('\\')->append($relativeNamespace)->replace('\\\\', '\\')->toString();
216+
// return trim("{$base}\\{$studlyName}\\{$relativeNamespace}", '\\');
216217
});
217218

218219
Module::macro('getTitle', function () {
219220
return str($this->getStudlyName())->kebab()->title()->replace('-', ' ')->toString();
220221
});
221222

222223
Module::macro('appNamespace', function (string $relativeNamespace = '') {
223-
$prefix = str(config('modules.paths.app_folder', 'app'))->ltrim(DIRECTORY_SEPARATOR, '\\')->studly();
224+
$prefix = str(config('modules.paths.app_folder', 'app'))->ltrim(DIRECTORY_SEPARATOR, '\\')->studly()->toString();
224225
$relativeNamespace = trim($relativeNamespace, '\\');
225-
if ($prefix != '') {
226+
if (filled($prefix)) {
226227
$relativeNamespace = str_replace($prefix . '\\', '', $relativeNamespace);
227228
$relativeNamespace = str_replace($prefix, '', $relativeNamespace);
228229
}
229-
$relativeNamespace = trim($relativeNamespace, '\\');
230-
$relativeNamespace = '\\' . $relativeNamespace;
231-
232230
return $this->namespace($relativeNamespace);
233231
});
234232
Module::macro('appPath', function (string $relativePath = '') {

0 commit comments

Comments
 (0)