@@ -28,9 +28,17 @@ class RepositoryCommand extends BaseCommand
2828 */
2929 protected array $ stubs = [
3030 'interface ' => __DIR__ . '/stubs/repository-interface.stub ' ,
31- 'repository ' => __DIR__ . '/stubs/repository.stub '
31+ 'repository ' => __DIR__ . '/stubs/repository.stub ' ,
32+ 'cache-repository ' => __DIR__ .'/stubs/cache-repository.stub '
3233 ];
3334
35+ /**
36+ * Boolean check to skip methods being executed
37+ *
38+ * @var bool
39+ */
40+ protected bool $ skip = false ;
41+
3442 public function __construct ()
3543 {
3644 parent ::__construct ();
@@ -39,34 +47,38 @@ public function __construct()
3947 public function handle ()
4048 {
4149 $ this ->checkModel ();
42- list ($ interface , $ interfaceName ) = $ this ->createInterface ();
43- $ this ->createRepository ($ interface , $ interfaceName );
50+
51+ if (!$ this ->skip ) {
52+ list ($ interface , $ interfaceName ) = $ this ->createInterface ();
53+ $ this ->createRepository ($ interface , $ interfaceName );
54+ }
4455 }
4556
4657 protected function checkModel ()
4758 {
48- $ model = $ this ->appNamespace .$ this ->getSingularName ($ this ->argument ('model ' ));
59+ $ model = $ this ->getSingularName ($ this ->argument ('model ' ));
60+
61+ $ modelParts = explode ('\\' , $ model );
62+ $ this ->modelName = $ modelParts [array_key_last ($ modelParts )];
4963
50- $ this ->model = str_replace ( ' / ' , '\\' , $ model) ;
64+ $ this ->model = $ this -> appNamespace . "{ $ model}" ;
5165
5266 if ($ this ->laravel ->runningInConsole ()) {
5367 if (!class_exists ($ this ->model )) {
54- $ response = $ this ->ask ("Model [ {$ this ->model }] does not exist. Would you like to create it? " , 'Yes ' );
68+ $ response = $ this ->ask ("Model [ {$ this ->modelName }] does not exist. Would you like to create it? " , 'Yes ' );
5569
5670 if ($ this ->isResponsePositive ($ response )) {
5771 Artisan::call ('make:model ' , [
5872 'name ' => $ this ->model
5973 ]);
6074
61- $ this ->info ("Model [ {$ this ->model }] has been successfully created. " );
75+ $ this ->info ("Model [ {$ this ->modelName }] has been successfully created. " );
6276 } else {
63- $ this ->info ("Model [ {$ this ->model }] will be skipped. " );
77+ $ this ->info ("Model [ {$ this ->modelName }] will be skipped. No repository class will be created. " );
78+ $ this ->skip = true ;
6479 }
6580 }
6681 }
67-
68- $ modelInfo = explode ('\\' , $ this ->model );
69- $ this ->modelName = $ modelInfo [array_key_last ($ modelInfo )];
7082 }
7183
7284 /**
@@ -82,24 +94,24 @@ protected function createInterface()
8294 $ content = $ this ->fileManager ->get ($ this ->stubs ['interface ' ]);
8395
8496 $ replacements = [
85- '{{ namespace }} ' => "{$ this ->appNamespace }\ Repository\{$ this ->modelName } " ,
86- '{{ model }} ' => $ this ->modelName
97+ '% namespace% ' => "{$ this ->appNamespace }Repository \ \{$ this ->modelName }" ,
98+ '% model% ' => $ this ->modelName
8799 ];
88100
89101 $ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
90102
91103 $ fileName = "{$ this ->modelName }RepositoryInterface " ;
92104 $ fileDirectory = app ()->basePath () . "/App/Repository/ {$ this ->modelName }" ;
93- $ filePath = "{$ fileDirectory }{$ fileName }.php " ;
105+ $ filePath = "{$ fileDirectory }/ {$ fileName }.php " ;
94106
95107 if (!$ this ->fileManager ->exists ($ fileDirectory ))
96108 $ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
97109
98- if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ fileDirectory )) {
110+ if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ filePath )) {
99111 $ response = $ this ->ask ("The interface [ {$ fileName }] has already exists. Do you want to overwrite it? " , 'Yes ' );
100112
101113 if (!$ this ->isResponsePositive ($ response )) {
102- $ this ->line ("The interface [ {$ fileName }] will not be overwritten. " );
114+ $ this ->info ("The interface [ {$ fileName }] will not be overwritten. " );
103115 return ;
104116 }
105117 }
@@ -108,7 +120,7 @@ protected function createInterface()
108120
109121 $ this ->info ("The interface [ {$ fileName }] has been created " );
110122
111- return ["{$ this ->appNamespace }\ Repository\{$ this ->modelName }\{ $ fileName} " , $ fileName ];
123+ return ["{$ this ->appNamespace }Repository \\ {$ this ->modelName }\ \{$ fileName }" , $ fileName ];
112124 }
113125
114126 /**
@@ -123,21 +135,22 @@ protected function createInterface()
123135 */
124136 protected function createRepository (string $ interface , string $ fileName )
125137 {
126- $ content = $ this ->fileManager ->get ($ this ->stubs ['repository ' ]);
138+ if ($ this ->hasOption ('cache ' )) $ content = $ this ->fileManager ->get ($ this ->stubs ['cache-repository ' ]);
139+ else $ content = $ this ->fileManager ->get ($ this ->stubs ['repository ' ]);
127140
128141 $ replacements = [
129- '{{ interfaceNamespace }} ' => "{ $ this -> appNamespace } {$ interface }" ,
130- '{{ interface }} ' => $ fileName ,
131- '{{ model }} ' => $ this ->model ,
132- '{{ modelName }} ' => $ this ->modelName ,
133- '{{ namespace }} ' => "{$ this ->appNamespace }\ Repository\{$ this ->modelName } "
142+ '% interfaceNamespace% ' => "{$ interface }" ,
143+ '% interface% ' => $ fileName ,
144+ '% model% ' => $ this ->model ,
145+ '% modelName% ' => $ this ->modelName ,
146+ '% namespace% ' => "{$ this ->appNamespace }Repository \ \{$ this ->modelName }"
134147 ];
135148
136149 $ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
137150
138151 $ fileName = "{$ this ->modelName }Repository " ;
139152 $ fileDirectory = app ()->basePath () . "/App/Repository/ {$ this ->modelName }" ;
140- $ filePath = "{$ fileDirectory }{$ fileName }.php " ;
153+ $ filePath = "{$ fileDirectory }/ {$ fileName }.php " ;
141154
142155 if (!$ this ->fileManager ->exists ($ fileDirectory ))
143156 $ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
@@ -153,7 +166,7 @@ protected function createRepository(string $interface, string $fileName)
153166
154167 $ this ->fileManager ->put ($ filePath , $ content );
155168
156- $ this ->info ("The repository [ {$ filePath }] has been created. " );
169+ $ this ->info ("The repository [ {$ fileName }] has been created. " );
157170 }
158171
159172
0 commit comments