Skip to content

Commit 6d6c917

Browse files
committed
MakePageCommand refactoring.
1 parent 86f513b commit 6d6c917

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

src/Console/MakePageCommand.php

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,78 @@ class MakePageCommand extends Command
1919

2020
protected string $resourceName;
2121

22+
protected Filesystem $filesystem;
23+
24+
public function __construct(Filesystem $filesystem)
25+
{
26+
parent::__construct();
27+
$this->filesystem = $filesystem;
28+
}
29+
2230
public function handle(): int
31+
{
32+
$this->initializeNames();
33+
$this->comment('Creating Pages...');
34+
35+
$this->createPage('Index');
36+
$this->createPage('Form');
37+
38+
return self::SUCCESS;
39+
}
40+
41+
private function initializeNames(): void
2342
{
2443
$this->moduleName = Str::studly($this->argument('moduleName'));
2544
$this->resourceName = Str::studly($this->argument('resourceName'));
45+
}
2646

27-
$this->comment('Creating Pages...');
28-
$this->createIndexPage();
29-
$this->createFormPage();
47+
private function createPage(string $type): void
48+
{
49+
$stub = $this->getStubContent($type);
50+
$replacements = $this->getReplacements($type);
3051

31-
return self::SUCCESS;
52+
$processedStub = $this->processStub($stub, $replacements);
53+
$this->saveStub($processedStub, $type);
3254
}
3355

34-
private function createIndexPage(): void
56+
private function getStubContent(string $type): string
3557
{
36-
$stub = file_get_contents(__DIR__.'/../../stubs/page-stub/Index.stub');
58+
return file_get_contents(__DIR__."/../../stubs/page-stub/{$type}.stub");
59+
}
3760

61+
private function getReplacements(string $type): array
62+
{
3863
$resourceNamePascalCase = $this->resourceName;
3964
$resourceNameCamelCase = Str::camel($this->resourceName);
4065

41-
$stub = str_replace('{{ ResourceName }}', $resourceNamePascalCase, $stub);
42-
$stub = str_replace('{{ ResourceNamePascalPlural }}', Str::plural($resourceNamePascalCase), $stub);
43-
$stub = str_replace('{{ resourceName }}', $resourceNameCamelCase, $stub);
44-
$stub = str_replace('{{ resourceNameCamelPlural }}', Str::plural($resourceNameCamelCase), $stub);
45-
46-
(new Filesystem)->ensureDirectoryExists(resource_path("js/Pages/{$this->moduleName}/"));
66+
$replacements = [
67+
'{{ ResourceName }}' => $resourceNamePascalCase,
68+
'{{ ResourceNamePascalPlural }}' => Str::plural($resourceNamePascalCase),
69+
'{{ resourceName }}' => $resourceNameCamelCase,
70+
];
4771

48-
$path = resource_path("js/Pages/{$this->moduleName}/{$this->resourceName}Index.vue");
72+
if ($type === 'Index') {
73+
$replacements['{{ resourceNameCamelPlural }}'] = Str::plural($resourceNameCamelCase);
74+
}
4975

50-
file_put_contents($path, $stub);
76+
return $replacements;
5177
}
5278

53-
private function createFormPage(): void
79+
private function processStub(string $stub, array $replacements): string
5480
{
55-
$stub = file_get_contents(__DIR__.'/../../stubs/page-stub/Form.stub');
56-
57-
$resourceNamePascalCase = $this->resourceName;
58-
59-
$stub = str_replace('{{ ResourceName }}', $resourceNamePascalCase, $stub);
60-
$stub = str_replace('{{ ResourceNamePascalPlural }}', Str::plural($resourceNamePascalCase), $stub);
61-
$stub = str_replace('{{ resourceName }}', Str::camel($this->resourceName), $stub);
62-
63-
(new Filesystem)->ensureDirectoryExists(resource_path("js/Pages/{$this->moduleName}/"));
81+
return str_replace(
82+
array_keys($replacements),
83+
array_values($replacements),
84+
$stub
85+
);
86+
}
6487

65-
$path = resource_path("js/Pages/{$this->moduleName}/{$this->resourceName}Form.vue");
88+
private function saveStub(string $processedStub, string $type): void
89+
{
90+
$directory = resource_path("js/Pages/{$this->moduleName}/");
91+
$this->filesystem->ensureDirectoryExists($directory);
6692

67-
file_put_contents($path, $stub);
93+
$path = $directory."{$this->resourceName}{$type}.vue";
94+
file_put_contents($path, $processedStub);
6895
}
6996
}

0 commit comments

Comments
 (0)