Skip to content

Commit

Permalink
New Feature: Generate a Filament Plugin for a specified Module
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsam726 committed Apr 12, 2024
1 parent 128fad9 commit 2561e68
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Coolsam\Modules\Concerns\GeneratesModularFiles;
use Illuminate\Console\GeneratorCommand;

class ModulesMakeClusterCommand extends GeneratorCommand
class ModuleMakeFilamentClusterCommand extends GeneratorCommand
{
use GeneratesModularFiles;

protected $name = 'module:make:filament-cluster';

protected $description = 'Create a new Filament cluster class in the module';

protected $type = 'Cluster';
protected $type = 'Filament Cluster';

protected function getRelativeNamespace(): string
{
Expand All @@ -22,7 +22,7 @@ protected function getRelativeNamespace(): string

protected function getStub(): string
{
return $this->resolveStubPath('/stubs/cluster.stub');
return $this->resolveStubPath('/stubs/filament-cluster.stub');
}

protected function stubReplacements(): array
Expand Down
34 changes: 34 additions & 0 deletions src/Commands/ModuleMakeFilamentPluginCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Coolsam\Modules\Commands;

use Coolsam\Modules\Concerns\GeneratesModularFiles;
use Illuminate\Console\GeneratorCommand;

class ModuleMakeFilamentPluginCommand extends GeneratorCommand
{
use GeneratesModularFiles;

protected $name = 'module:make:filament-plugin';

protected $description = 'Create a new Filament Plugin class in the module';

protected $type = 'Filament Plugin';

protected function getRelativeNamespace(): string
{
return 'App\\Filament';
}

protected function getStub(): string
{
return $this->resolveStubPath('/stubs/filament-plugin.stub');
}

protected function stubReplacements(): array
{
return [
'moduleStudlyName' => $this->getModule()->getStudlyName(),
];
}
}
8 changes: 6 additions & 2 deletions src/Commands/ModulesFilamentInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ protected function createDefaultFilamentPlugin()
$this->info('Filament Plugin created successfully');
}

protected function createDefaultFilamentCluster()
protected function createDefaultFilamentCluster(): void
{
// TODO: Implement
$module = $this->getModule();
$this->call('module:make:filament-cluster', [
'name' => $module->getStudlyName(),
'module' => $module->getStudlyName(),
]);
}
}
File renamed without changes.
76 changes: 76 additions & 0 deletions src/Commands/stubs/filament-plugin.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace {{ namespace }};

use Filament\Contracts\Plugin;
use Filament\Panel;
use Illuminate\Support\Str;
use Nwidart\Modules\Facades\Module;

class {{ class }} implements Plugin
{
public function getModuleName(): string
{
return '{{ moduleStudlyName }}';
}

public function getId(): string
{
return Str::of($this->getModuleName())->lower()->append('-module')->toString();
}

public function getModule(): \Nwidart\Modules\Module
{
return Module::findOrFail($this->getModuleName());
}

public function register(Panel $panel): void
{
$module = $this->getModule();
$useClusters = config('filament-modules.clusters.enabled', false);
$panel->discoverPages(
in: $module->appPath('Filament'.DIRECTORY_SEPARATOR.'Pages'),
for: $module->appNamespace('\\Filament\\Pages')
);
$panel->discoverResources(
in: $module->appPath('Filament'.DIRECTORY_SEPARATOR.'Resources'),
for: $module->appNamespace('\\Filament\\Resources')
);
$panel->discoverWidgets(
in: $module->appPath('Filament'.DIRECTORY_SEPARATOR.'Widgets'),
for: $module->appNamespace('\\Filament\\Widgets')
);

$panel->discoverLivewireComponents(
in: $module->appPath('Livewire'),
for: $module->appNamespace('\\Livewire')
);

if ($useClusters) {
$path = $module->appPath('Filament'.DIRECTORY_SEPARATOR.'Clusters');
$namespace = $module->appNamespace('\\Filament\\Clusters');
$panel->discoverClusters(
in: $path,
for: $namespace,
);
}
}

public function boot(Panel $panel): void
{
// TODO: Implement boot() method.
}

public static function make(): static
{
return app(static::class);
}

public static function get(): static
{
/** @var static $plugin */
$plugin = filament(app(static::class)->getId());

return $plugin;
}
}
3 changes: 2 additions & 1 deletion src/Concerns/GeneratesModularFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ protected function promptForMissingArgumentsUsing(): array
'Scope' => 'E.g. TrendingScope',
'Seeder' => 'E.g. UserSeeder',
'Test' => 'E.g. UserTest',
'Cluster' => 'E.g Settings',
'Filament Cluster' => 'E.g Settings',
'Filament Plugin' => 'e.g AccessControlPlugin',
default => '',
},
],
Expand Down
3 changes: 2 additions & 1 deletion src/ModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ protected function getCommands(): array
{
return [
Commands\ModulesFilamentInstallCommand::class,
Commands\ModulesMakeClusterCommand::class,
Commands\ModuleMakeFilamentClusterCommand::class,
Commands\ModuleMakeFilamentPluginCommand::class,
];
}

Expand Down

0 comments on commit 2561e68

Please sign in to comment.