Skip to content

Commit

Permalink
New Features: filament:install, module:make:filament-cluster and modu…
Browse files Browse the repository at this point in the history
…le:make-filament-plugin commands

- Added the Filament Installation command to add filament support to a module
- Added a command to generate a Filament plugin in a module
- Added a command to generate a Filament cluster in a module
- Modified phpstan and removed database as it is not needed
  • Loading branch information
coolsam726 committed Apr 13, 2024
1 parent 2561e68 commit 2e437b3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 63 deletions.
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ parameters:
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ModuleMakeFilamentPluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function stubReplacements(): array
{
return [
'moduleStudlyName' => $this->getModule()->getStudlyName(),
'pluginId' => str($this->argument('name'))->replace('Plugin', '')->studly()->lower()->toString(),
];
}
}
15 changes: 6 additions & 9 deletions src/Commands/ModulesFilamentInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,13 @@ private function makeDirectory(string $dir): void
}
}

protected function createDefaultFilamentPlugin()
protected function createDefaultFilamentPlugin(): void
{
$plugin = $this->getModule()->appPath('Filament/Plugin.php');
if (file_exists($plugin)) {
$this->error('Filament Plugin already exists');
exit(1);
}

$this->copyStubToApp('filament-plugin', $plugin);
$this->info('Filament Plugin created successfully');
$module = $this->getModule();
$this->call('module:make:filament-plugin', [
'name' => $module->getStudlyName().'Plugin',
'module' => $module->getStudlyName(),
]);
}

protected function createDefaultFilamentCluster(): void
Expand Down
57 changes: 4 additions & 53 deletions src/Commands/stubs/filament-plugin.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,26 @@

namespace {{ namespace }};

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

class {{ class }} implements Plugin
{
use ModuleFilamentPlugin;

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,
);
}
return '{{ pluginId }}';
}

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;
}
}
61 changes: 61 additions & 0 deletions src/Concerns/ModuleFilamentPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Coolsam\Modules\Concerns;

use Filament\Panel;
use Nwidart\Modules\Facades\Module;

trait ModuleFilamentPlugin
{
abstract public function getModuleName(): string;

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 static function make(): static
{
return app(static::class);
}

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

return $plugin;
}
}

0 comments on commit 2e437b3

Please sign in to comment.