Skip to content

Commit

Permalink
Merge pull request savannabits#39 from savannabits/3.x-dev
Browse files Browse the repository at this point in the history
3.x dev
  • Loading branch information
coolsam726 authored Jul 24, 2023
2 parents d82e8d9 + bbe2096 commit 6b0c6b3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
49 changes: 28 additions & 21 deletions src/Commands/ModuleMakePanelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class ModuleMakePanelCommand extends Command
protected $description = 'Create a new Filament panel in a module';

protected $signature = 'module:make-filament-panel {id?} {module?} {--F|force}';

protected ?Module $module = null;

private $basePath = 'Providers/Filament';

public function handle(): int
Expand All @@ -29,68 +31,73 @@ public function handle(): int
->studly()
->append('PanelProvider');
$module = $this->argument('module');
if (!$module) {
if (! $module) {
try {
$usedNow = app('modules')->getUsedNow();
} catch (\Throwable $exception) {
$usedNow = null;
}
$module = Str::of($this->askRequired('Module Name (e.g. `Sales`)', 'module',$usedNow))->toString();
$module = Str::of($this->askRequired('Module Name (e.g. `Sales`)', 'module', $usedNow))->toString();
}
$this->module = app('modules')->findOrFail($module);

$path = str($this->module->getExtraPath("{$this->basePath}/$class"))
->replace('\\', '/')
->append('.php')->toString();

if (!$this->option('force') && $this->checkForCollision([$path])) {
if (! $this->option('force') && $this->checkForCollision([$path])) {
return static::INVALID;
}
$namespace = Str::of($this->basePath)
->replace('/','\\')
->replace('/', '\\')
->prepend('\\')
->prepend($this->getModuleNamespace());

$moduleJson = static::readModuleJson($this->module->getName());
// $panelPath = Str::of($this->module->getLowerName());
$panelPath = $id->prepend("/")->prepend($this->module->getLowerName());
// $panelPath = Str::of($this->module->getLowerName());
$panelPath = $id->prepend('/')->prepend($this->module->getLowerName());

if (collect($moduleJson['providers'])->filter(
fn($provider) => Str::of($provider)->contains('PanelProvider') && !Str::of($provider)->contains($class)
)->count()) {
$panelPath = $id->prepend("/")->prepend($this->module->getLowerName());
fn ($provider) => Str::of($provider)->contains('PanelProvider') && ! Str::of($provider)->contains($class)
)->count()) {
$panelPath = $id->prepend('/')->prepend($this->module->getLowerName());
}

$this->copyStubToApp('PanelProvider', $path, [
'namespace' => $namespace,
'Module' => $this->module->getStudlyName(),
'module_namespace' => $this->getModuleNamespace(),
'class' => $class,
'directory' => $id->studly()->toString(),
'panel_path' => $panelPath->toString(),
'id' => $id->prepend("::")->prepend($this->module->getLowerName())->lcfirst(),
'namespace' => $namespace,
'Module' => $this->module->getStudlyName(),
'module_namespace' => $this->getModuleNamespace(),
'class' => $class,
'directory' => $id->studly()->toString(),
'panel_path' => $panelPath->toString(),
'id' => $id->prepend('::')->prepend($this->module->getLowerName())->lcfirst(),
]);

$providers = collect($moduleJson['providers']);
$provider = "$namespace\\$class";
if (!$providers->contains($provider)) {
if (! $providers->contains($provider)) {
$moduleJson['providers'][] = $provider;
static::writeToModuleJson($this->module->getName(),$moduleJson);
static::writeToModuleJson($this->module->getName(), $moduleJson);
}
$this->components->info("Successfully created {$class}!");

return static::SUCCESS;
}

public function getModuleNamespace(): string
{
return $this->laravel['modules']->config('namespace').'\\'.$this->module->getName();
}
public static function readModuleJson(string $moduleName) {
return json_decode(file_get_contents(module_path($moduleName,'module.json')),true);

public static function readModuleJson(string $moduleName)
{
return json_decode(file_get_contents(module_path($moduleName, 'module.json')), true);
}

public static function writeToModuleJson($moduleName, array $data): bool|int
{
$jsonString = json_encode($data, JSON_PRETTY_PRINT);
return file_put_contents(module_path($moduleName,'module.json'),$jsonString);

return file_put_contents(module_path($moduleName, 'module.json'), $jsonString);
}
}
4 changes: 2 additions & 2 deletions src/Extensions/LaravelModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Coolsam\FilamentModules\Extensions;

use Filament\FilamentServiceProvider;
use Illuminate\Support\Facades\Log;
use Nwidart\Modules\LaravelModulesServiceProvider as BaseModulesServiceProvider;

Expand All @@ -12,8 +11,9 @@ public function register(): void
{
$this->registerPanels();
parent::register();
Log::info("Registered Modules");
Log::info('Registered Modules');
}

public function registerPanels(): void
{
// Override this to do anything during registration
Expand Down
1 change: 1 addition & 0 deletions src/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return 'modules';
Expand Down
2 changes: 2 additions & 0 deletions src/ModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public function configurePackage(Package $package): void
ModuleMakePanelCommand::class,
]);
}

public function register()
{
$this->app->register(LaravelModulesServiceProvider::class);

return parent::register();
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Coolsam\FilamentModules\Tests;

use Coolsam\FilamentModules\ModulesServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Coolsam\FilamentModules\ModulesServiceProvider;

class TestCase extends Orchestra
{
Expand Down

0 comments on commit 6b0c6b3

Please sign in to comment.