33namespace Modular \Modular \Console ;
44
55use Illuminate \Console \Command ;
6+ use Illuminate \Filesystem \Filesystem ;
67use Illuminate \Support \Str ;
78use Modular \Modular \Console \InstallerTraits \ModuleExists ;
89
@@ -18,10 +19,17 @@ class MakeControllerCommand extends Command
1819
1920 protected string $ resourceName ;
2021
22+ protected Filesystem $ filesystem ;
23+
24+ public function __construct (Filesystem $ filesystem )
25+ {
26+ parent ::__construct ();
27+ $ this ->filesystem = $ filesystem ;
28+ }
29+
2130 public function handle (): int
2231 {
23- $ this ->moduleName = Str::studly ($ this ->argument ('moduleName ' ));
24- $ this ->resourceName = Str::studly ($ this ->argument ('resourceName ' ));
32+ $ this ->initializeNames ();
2533
2634 if (! $ this ->moduleExists ()) {
2735 return self ::FAILURE ;
@@ -33,19 +41,53 @@ public function handle(): int
3341 return self ::SUCCESS ;
3442 }
3543
44+ private function initializeNames (): void
45+ {
46+ $ this ->moduleName = Str::studly ($ this ->argument ('moduleName ' ));
47+ $ this ->resourceName = Str::studly ($ this ->argument ('resourceName ' ));
48+ }
49+
3650 private function createModuleController (): void
3751 {
38- $ stub = file_get_contents (__DIR__ .'/../../stubs/module-stub/modules/Http/Controllers/ModuleController.stub ' );
52+ $ stub = $ this ->getStubContent ();
53+ $ replacements = $ this ->getReplacements ();
3954
55+ $ processedStub = $ this ->processStub ($ stub , $ replacements );
56+ $ this ->saveStub ($ processedStub );
57+ }
58+
59+ private function getStubContent (): string
60+ {
61+ return file_get_contents (__DIR__ .'/../../stubs/module-stub/modules/Http/Controllers/ModuleController.stub ' );
62+ }
63+
64+ private function getReplacements (): array
65+ {
4066 $ resourceNameCamelCase = Str::camel ($ this ->resourceName );
4167
42- $ stub = str_replace ('{{ ModuleName }} ' , $ this ->moduleName , $ stub );
43- $ stub = str_replace ('{{ ResourceName }} ' , $ this ->resourceName , $ stub );
44- $ stub = str_replace ('{{ resourceName }} ' , $ resourceNameCamelCase , $ stub );
45- $ stub = str_replace ('{{ resourceNameCamelPlural }} ' , Str::plural ($ resourceNameCamelCase ), $ stub );
68+ return [
69+ '{{ ModuleName }} ' => $ this ->moduleName ,
70+ '{{ ResourceName }} ' => $ this ->resourceName ,
71+ '{{ resourceName }} ' => $ resourceNameCamelCase ,
72+ '{{ resourceNameCamelPlural }} ' => Str::plural ($ resourceNameCamelCase ),
73+ ];
74+ }
4675
47- $ path = base_path ("modules/ {$ this ->moduleName }/Http/Controllers/ {$ this ->resourceName }Controller.php " );
76+ private function processStub (string $ stub , array $ replacements ): string
77+ {
78+ return str_replace (
79+ array_keys ($ replacements ),
80+ array_values ($ replacements ),
81+ $ stub
82+ );
83+ }
84+
85+ private function saveStub (string $ processedStub ): void
86+ {
87+ $ directory = base_path ("modules/ {$ this ->moduleName }/Http/Controllers/ " );
88+ $ this ->filesystem ->ensureDirectoryExists ($ directory );
4889
49- file_put_contents ($ path , $ stub );
90+ $ path = $ directory ."{$ this ->resourceName }Controller.php " ;
91+ file_put_contents ($ path , $ processedStub );
5092 }
5193}
0 commit comments