From c11c8262ba87800fd9bba1941d12bf7c5353127e Mon Sep 17 00:00:00 2001 From: LT Date: Sun, 5 Jan 2025 21:50:39 +0300 Subject: [PATCH 1/3] feat: BaseDir/BaseNamespace Issue #1230 --- src/Laravel/src/Commands/InstallCommand.php | 2 +- src/Laravel/src/Commands/MakeApplyCommand.php | 2 +- .../src/Commands/MakeComponentCommand.php | 8 +-- .../src/Commands/MakeControllerCommand.php | 2 +- src/Laravel/src/Commands/MakeFieldCommand.php | 8 +-- .../src/Commands/MakeHandlerCommand.php | 2 +- .../src/Commands/MakeLayoutCommand.php | 8 +-- src/Laravel/src/Commands/MakePageCommand.php | 10 ++-- .../src/Commands/MakeResourceCommand.php | 10 ++-- .../src/Commands/MakeTypeCastCommand.php | 2 +- src/Laravel/src/Commands/MoonShineCommand.php | 54 +++++++++++++++---- src/Laravel/src/Commands/PublishCommand.php | 8 +-- .../MoonShineConfigurator.php | 12 +++-- 13 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/Laravel/src/Commands/InstallCommand.php b/src/Laravel/src/Commands/InstallCommand.php index 52af90f66..5ba2d42af 100644 --- a/src/Laravel/src/Commands/InstallCommand.php +++ b/src/Laravel/src/Commands/InstallCommand.php @@ -277,7 +277,7 @@ protected function initDashboard(): void $this->replaceInConfig( 'dashboard', - moonshineConfig()->getNamespace('\Pages\Dashboard') . "::class" + $this->getNamespace('\Pages\Dashboard') . "::class" ); $this->components->task('Dashboard created'); diff --git a/src/Laravel/src/Commands/MakeApplyCommand.php b/src/Laravel/src/Commands/MakeApplyCommand.php index 468b7f26c..aa9609d62 100644 --- a/src/Laravel/src/Commands/MakeApplyCommand.php +++ b/src/Laravel/src/Commands/MakeApplyCommand.php @@ -9,7 +9,7 @@ #[AsCommand(name: 'moonshine:apply')] class MakeApplyCommand extends MoonShineCommand { - protected $signature = 'moonshine:apply {className?}'; + protected $signature = 'moonshine:apply {className?} {--base-dir=} {--base-namespace=}'; protected $description = 'Create apply for Field'; diff --git a/src/Laravel/src/Commands/MakeComponentCommand.php b/src/Laravel/src/Commands/MakeComponentCommand.php index 3995a965b..6fda9740e 100644 --- a/src/Laravel/src/Commands/MakeComponentCommand.php +++ b/src/Laravel/src/Commands/MakeComponentCommand.php @@ -14,7 +14,7 @@ #[AsCommand(name: 'moonshine:component')] class MakeComponentCommand extends MoonShineCommand { - protected $signature = 'moonshine:component {className?}'; + protected $signature = 'moonshine:component {className?} {--base-dir=} {--base-namespace=}'; protected $description = 'Create component'; @@ -32,11 +32,7 @@ public function handle(): int $view = $this->makeViewFromStub('admin.components', $stubsPath->name, $stubsPath->dir); - $stubsPath->prependDir( - $this->getDirectory('Components') - )->prependNamespace( - moonshineConfig()->getNamespace('Components') - ); + $stubsPath = $this->qualifyStubsDir($stubsPath, 'Components'); $this->makeDir($stubsPath->dir); diff --git a/src/Laravel/src/Commands/MakeControllerCommand.php b/src/Laravel/src/Commands/MakeControllerCommand.php index 300bda62e..cab6ce650 100644 --- a/src/Laravel/src/Commands/MakeControllerCommand.php +++ b/src/Laravel/src/Commands/MakeControllerCommand.php @@ -9,7 +9,7 @@ #[AsCommand(name: 'moonshine:controller')] class MakeControllerCommand extends MoonShineCommand { - protected $signature = 'moonshine:controller {className?}'; + protected $signature = 'moonshine:controller {className?} {--base-dir=} {--base-namespace=}'; protected $description = 'Create controller'; diff --git a/src/Laravel/src/Commands/MakeFieldCommand.php b/src/Laravel/src/Commands/MakeFieldCommand.php index 33d6a9a0d..09acd1831 100644 --- a/src/Laravel/src/Commands/MakeFieldCommand.php +++ b/src/Laravel/src/Commands/MakeFieldCommand.php @@ -17,7 +17,7 @@ #[AsCommand(name: 'moonshine:field')] class MakeFieldCommand extends MoonShineCommand { - protected $signature = 'moonshine:field {className?}'; + protected $signature = 'moonshine:field {className?} {--base-dir=} {--base-namespace=}'; protected $description = 'Create field'; @@ -36,11 +36,7 @@ public function handle(): int $view = $this->makeViewFromStub('admin.fields', $stubsPath->name, $stubsPath->dir); - $stubsPath->prependDir( - $this->getDirectory('Fields'), - )->prependNamespace( - moonshineConfig()->getNamespace('Fields'), - ); + $stubsPath = $this->qualifyStubsDir($stubsPath, 'Fields'); $extends = select('Extends', $this->findExtends(), Field::class); diff --git a/src/Laravel/src/Commands/MakeHandlerCommand.php b/src/Laravel/src/Commands/MakeHandlerCommand.php index 7eb6b8cf7..947221b07 100644 --- a/src/Laravel/src/Commands/MakeHandlerCommand.php +++ b/src/Laravel/src/Commands/MakeHandlerCommand.php @@ -9,7 +9,7 @@ #[AsCommand(name: 'moonshine:handler')] class MakeHandlerCommand extends MoonShineCommand { - protected $signature = 'moonshine:handler {className?}'; + protected $signature = 'moonshine:handler {className?} {--base-dir=} {--base-namespace=}'; protected $description = 'Create handler class'; diff --git a/src/Laravel/src/Commands/MakeLayoutCommand.php b/src/Laravel/src/Commands/MakeLayoutCommand.php index 703df330b..1046626b0 100644 --- a/src/Laravel/src/Commands/MakeLayoutCommand.php +++ b/src/Laravel/src/Commands/MakeLayoutCommand.php @@ -14,7 +14,7 @@ #[AsCommand(name: 'moonshine:layout')] class MakeLayoutCommand extends MoonShineCommand { - protected $signature = 'moonshine:layout {className?} {--compact} {--full} {--default} {--dir=}'; + protected $signature = 'moonshine:layout {className?} {--compact} {--full} {--default} {--dir=} {--base-dir=} {--base-namespace=}'; protected $description = 'Create layout'; @@ -32,11 +32,7 @@ public function handle(): int $dir = $this->option('dir') ?: 'Layouts'; - $stubsPath->prependDir( - $this->getDirectory($dir), - )->prependNamespace( - moonshineConfig()->getNamespace($dir), - ); + $stubsPath = $this->qualifyStubsDir($stubsPath, $dir); $this->makeDir($stubsPath->dir); diff --git a/src/Laravel/src/Commands/MakePageCommand.php b/src/Laravel/src/Commands/MakePageCommand.php index cf1021762..cf2aa79fa 100644 --- a/src/Laravel/src/Commands/MakePageCommand.php +++ b/src/Laravel/src/Commands/MakePageCommand.php @@ -14,7 +14,7 @@ #[AsCommand(name: 'moonshine:page')] class MakePageCommand extends MoonShineCommand { - protected $signature = 'moonshine:page {className?} {--force} {--without-register} {--crud} {--dir=} {--extends=}'; + protected $signature = 'moonshine:page {className?} {--force} {--without-register} {--crud} {--dir=} {--extends=} {--base-dir=} {--base-namespace=}'; protected $description = 'Create page'; @@ -34,11 +34,7 @@ public function handle(): int $dir = $this->option('dir') ?: 'Pages'; - $stubsPath->prependDir( - $this->getDirectory($dir), - )->prependNamespace( - moonshineConfig()->getNamespace($dir), - ); + $stubsPath = $this->qualifyStubsDir($stubsPath, $dir); if (! $this->option('force') && ! $this->option('extends') && ! $this->option('crud')) { $types = [ @@ -70,7 +66,7 @@ public function handle(): int $stubsPath->prependDir( $this->getDirectory("$dir/$name"), )->prependNamespace( - moonshineConfig()->getNamespace("$dir\\$name"), + $this->getNamespace("$dir\\$name"), ); $this->makePage($stubsPath, 'CrudPage', $type); diff --git a/src/Laravel/src/Commands/MakeResourceCommand.php b/src/Laravel/src/Commands/MakeResourceCommand.php index ccb58cf63..a20c8554b 100644 --- a/src/Laravel/src/Commands/MakeResourceCommand.php +++ b/src/Laravel/src/Commands/MakeResourceCommand.php @@ -14,7 +14,7 @@ #[AsCommand(name: 'moonshine:resource')] class MakeResourceCommand extends MoonShineCommand { - protected $signature = 'moonshine:resource {className?} {--type=} {--m|model=} {--t|title=} {--test} {--pest} {--p|policy}'; + protected $signature = 'moonshine:resource {className?} {--type=} {--m|model=} {--t|title=} {--test} {--pest} {--p|policy} {--base-dir=} {--base-namespace=}'; protected $description = 'Create resource'; @@ -42,11 +42,7 @@ public function handle(): int ->remove('resource', false) ->value(); - $stubsPath->prependDir( - $this->getDirectory('Resources'), - )->prependNamespace( - moonshineConfig()->getNamespace('Resources'), - ); + $stubsPath = $this->qualifyStubsDir($stubsPath, 'Resources'); $this->makeDir($stubsPath->dir); @@ -95,7 +91,7 @@ public function handle(): int '--without-register' => true, ]); - $pageNamespace = moonshineConfig()->getNamespace("\Pages\\$name\\$name"); + $pageNamespace = $this->getNamespace("\Pages\\$name\\$name"); $replace += [ '{indexPage}' => "{$name}IndexPage", diff --git a/src/Laravel/src/Commands/MakeTypeCastCommand.php b/src/Laravel/src/Commands/MakeTypeCastCommand.php index 3e736a697..243ede1ea 100644 --- a/src/Laravel/src/Commands/MakeTypeCastCommand.php +++ b/src/Laravel/src/Commands/MakeTypeCastCommand.php @@ -9,7 +9,7 @@ #[AsCommand(name: 'moonshine:type-cast')] class MakeTypeCastCommand extends MoonShineCommand { - protected $signature = 'moonshine:type-cast {className?}'; + protected $signature = 'moonshine:type-cast {className?} {--base-dir=} {--base-namespace=}'; protected $description = 'Create type cast class'; diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index 0e9f5769e..a22eae6ff 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -16,9 +16,14 @@ abstract class MoonShineCommand extends Command { protected string $stubsDir = __DIR__ . '/../../stubs'; - protected function getDirectory(string $path = ''): string + protected function getDirectory(string $path = '', ?string $base = null): string { - return moonshineConfig()->getDir($path); + return moonshineConfig()->getDir($path, $base); + } + + protected function getNamespace(string $path = '', ?string $base = null): string + { + return moonshineConfig()->getNamespace($path, $base); } protected function getRelativePath(string $path): string @@ -154,16 +159,10 @@ protected function fastCreateFromStub(string $stub, string $dir): void { $className = $this->argument('className') ?? text( 'Class name', - required: true + required: true, ); - $stubsPath = new StubsPath($className, 'php'); - - $stubsPath->prependDir( - $this->getDirectory($dir) - )->prependNamespace( - moonshineConfig()->getNamespace($dir) - ); + $stubsPath = $this->qualifyStubsDir(new StubsPath($className, 'php'), $dir); $this->makeDir($stubsPath->dir); @@ -175,10 +174,43 @@ protected function fastCreateFromStub(string $stub, string $dir): void $this->wasCreatedInfo($stubsPath); } + protected function qualifyStubsDir(StubsPath $stubsPath, string $dir, ?string $namespace = null): StubsPath + { + $baseDir = $this->hasOption('base-dir') ? $this->option('base-dir') : null; + $baseNamespace = $this->hasOption('base-namespace') ? $this->option('base-namespace') : null; + + $toNamespace = static fn(string $str): string => str($str) + ->trim('\\') + ->trim('/') + ->replace('/', '\\') + ->explode('\\') + ->map(static fn(string $segment) => ucfirst($segment)) + ->implode('\\'); + + if($baseDir !== null && $baseNamespace === null) { + $baseNamespace = $toNamespace($baseDir); + } + + if($namespace === null) { + $namespace = $toNamespace($dir); + } + + $baseDir = $baseDir ? trim($baseDir, '/') : $baseDir; + $baseNamespace = $baseNamespace ? $toNamespace($baseNamespace) : $baseNamespace; + + return $stubsPath->prependDir( + $this->getDirectory($dir, $baseDir), + )->prependNamespace( + $this->getNamespace($namespace, $baseNamespace), + ); + } + protected function wasCreatedInfo(StubsPath $stubsPath): void { + $path = $this->getRelativePath($stubsPath->getPath()); + outro( - "$stubsPath->name was created: " . $this->getRelativePath($stubsPath->getPath()), + "$stubsPath->name was created: $path", ); } } diff --git a/src/Laravel/src/Commands/PublishCommand.php b/src/Laravel/src/Commands/PublishCommand.php index e27a722df..797377dab 100644 --- a/src/Laravel/src/Commands/PublishCommand.php +++ b/src/Laravel/src/Commands/PublishCommand.php @@ -157,7 +157,7 @@ private function publishSystemForm(string $className, string $configKey): void $this->replaceInConfig( $configKey, - moonshineConfig()->getNamespace('\Forms\\' . $className) . "::class", + $this->getNamespace('\Forms\\' . $className) . "::class", $className ); } @@ -203,7 +203,7 @@ private function publishSystemPage(string $className, string $configKey): void $this->replaceInConfig( $configKey, - moonshineConfig()->getNamespace('\Pages\\' . $className) . "::class", + $this->getNamespace('\Pages\\' . $className) . "::class", $className ); } @@ -214,8 +214,8 @@ private function publishSystemPage(string $className, string $configKey): void private function copySystemClass(string $name, string $dir): array { $classPath = "src/$dir/$name.php"; - $fullClassPath = moonshineConfig()->getDir("/$dir/$name.php"); - $targetNamespace = moonshineConfig()->getNamespace("\\$dir"); + $fullClassPath = $this->getDirectory("/$dir/$name.php"); + $targetNamespace = $this->getNamespace("\\$dir"); (new Filesystem())->put( $fullClassPath, diff --git a/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php b/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php index 2cf481965..901e72e54 100644 --- a/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php +++ b/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php @@ -40,14 +40,18 @@ public function dir(string $dir, string $namespace): self ->set('namespace', $namespace); } - public function getDir(string $path = ''): string + public function getDir(string $path = '', ?string $base = null): string { - return $this->get('dir') . '/' . trim($path, '/'); + $base = $base ?? $this->get('dir'); + + return $base . '/' . trim($path, '/'); } - public function getNamespace(string $path = ''): string + public function getNamespace(string $path = '', ?string $base = null): string { - return $this->get('namespace') . '\\' . trim($path, '\\'); + $base = $base ?? $this->get('namespace'); + + return $base . '\\' . trim($path, '\\'); } /** From ff9c95c7b91987bd7cb4d079df68f3e4438edf7a Mon Sep 17 00:00:00 2001 From: lee-to Date: Sun, 5 Jan 2025 18:51:29 +0000 Subject: [PATCH 2/3] [rector] Rector fixes --- src/Laravel/src/Commands/MoonShineCommand.php | 2 +- src/Laravel/src/DependencyInjection/MoonShineConfigurator.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index a22eae6ff..eeddae133 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -184,7 +184,7 @@ protected function qualifyStubsDir(StubsPath $stubsPath, string $dir, ?string $n ->trim('/') ->replace('/', '\\') ->explode('\\') - ->map(static fn(string $segment) => ucfirst($segment)) + ->map(static fn(string $segment): string => ucfirst($segment)) ->implode('\\'); if($baseDir !== null && $baseNamespace === null) { diff --git a/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php b/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php index 901e72e54..e574b94af 100644 --- a/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php +++ b/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php @@ -42,14 +42,14 @@ public function dir(string $dir, string $namespace): self public function getDir(string $path = '', ?string $base = null): string { - $base = $base ?? $this->get('dir'); + $base ??= $this->get('dir'); return $base . '/' . trim($path, '/'); } public function getNamespace(string $path = '', ?string $base = null): string { - $base = $base ?? $this->get('namespace'); + $base ??= $this->get('namespace'); return $base . '\\' . trim($path, '\\'); } From d795960935a131c0da608d9749a2376252d91a6e Mon Sep 17 00:00:00 2001 From: lee-to Date: Sun, 5 Jan 2025 18:51:53 +0000 Subject: [PATCH 3/3] Fix styling --- src/Laravel/src/Commands/MoonShineCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index eeddae133..7d95bf2bc 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -179,19 +179,19 @@ protected function qualifyStubsDir(StubsPath $stubsPath, string $dir, ?string $n $baseDir = $this->hasOption('base-dir') ? $this->option('base-dir') : null; $baseNamespace = $this->hasOption('base-namespace') ? $this->option('base-namespace') : null; - $toNamespace = static fn(string $str): string => str($str) + $toNamespace = static fn (string $str): string => str($str) ->trim('\\') ->trim('/') ->replace('/', '\\') ->explode('\\') - ->map(static fn(string $segment): string => ucfirst($segment)) + ->map(static fn (string $segment): string => ucfirst($segment)) ->implode('\\'); - if($baseDir !== null && $baseNamespace === null) { + if ($baseDir !== null && $baseNamespace === null) { $baseNamespace = $toNamespace($baseDir); } - if($namespace === null) { + if ($namespace === null) { $namespace = $toNamespace($dir); }