diff --git a/src/Laravel/src/Commands/MakeResourceCommand.php b/src/Laravel/src/Commands/MakeResourceCommand.php index ccb887908..9b6b06e62 100644 --- a/src/Laravel/src/Commands/MakeResourceCommand.php +++ b/src/Laravel/src/Commands/MakeResourceCommand.php @@ -31,21 +31,18 @@ public function handle(): int ) ); - $dir = $name->ucfirst() - ->remove('Resource', false) - ->value(); - $name = $name->ucfirst() - ->replace(['resource', 'Resource'], '') + ->remove('resource', false) ->value(); $model = $this->qualifyModel($this->option('model') ?? $name); $title = $this->option('title') ?? str($name)->singular()->plural()->value(); + $moonshineDir = $this->getDirectory(); - $resource = $this->getDirectory() . "/Resources/{$name}Resource.php"; + $resource = "$moonshineDir/Resources/{$name}Resource.php"; - if (! is_dir($this->getDirectory() . "/Resources")) { - $this->makeDir($this->getDirectory() . "/Resources"); + if (! is_dir("$moonshineDir/Resources")) { + $this->makeDir("$moonshineDir/Resources"); } $stub = select('Resource type', [ @@ -54,7 +51,7 @@ public function handle(): int 'Resource' => 'Empty resource', ], 'ModelResourceDefault'); - $replaceData = [ + $replace = [ '{namespace}' => moonshineConfig()->getNamespace('\Resources'), '{model-namespace}' => $model, '{model}' => class_basename($model), @@ -66,35 +63,31 @@ public function handle(): int $testStub = $this->option('pest') ? 'pest' : 'test'; $testPath = base_path('tests/Feature/') . $name . 'ResourceTest.php'; - $this->copyStub($testStub, $testPath, $replaceData); + $this->copyStub($testStub, $testPath, $replace); info('Test file was created'); } if ($stub === 'ModelResourceWithPages') { - $pageDir = "Pages/$dir"; - $this->call(MakePageCommand::class, [ 'className' => $name, '--crud' => true, '--without-register' => true, ]); - $pageNamespace = static fn (string $name): string => moonshineConfig()->getNamespace( - str_replace('/', '\\', "\\$pageDir\\$dir$name") - ); - - $replaceData = [ - '{indexPage}' => "{$dir}IndexPage", - '{formPage}' => "{$dir}FormPage", - '{detailPage}' => "{$dir}DetailPage", - '{index-page-namespace}' => $pageNamespace('IndexPage'), - '{form-page-namespace}' => $pageNamespace('FormPage'), - '{detail-page-namespace}' => $pageNamespace('DetailPage'), - ] + $replaceData; + $pageNamespace = moonshineConfig()->getNamespace("\Pages\\$name\\$name"); + + $replace += [ + '{indexPage}' => "{$name}IndexPage", + '{formPage}' => "{$name}FormPage", + '{detailPage}' => "{$name}DetailPage", + '{index-page-namespace}' => "{$pageNamespace}IndexPage", + '{form-page-namespace}' => "{$pageNamespace}FormPage", + '{detail-page-namespace}' => "{$pageNamespace}DetailPage", + ]; } - $this->copyStub($stub, $resource, $replaceData); + $this->copyStub($stub, $resource, $replace); info( "{$name}Resource file was created: " . str_replace(