Skip to content

Commit

Permalink
refactor(command): 'MakeResourceCommand' class refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shevelev-anatoliy committed Dec 19, 2024
1 parent 16590e4 commit ea8d0bf
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/Laravel/src/Commands/MakeResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand All @@ -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),
Expand All @@ -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(
Expand Down

0 comments on commit ea8d0bf

Please sign in to comment.