@@ -28,9 +28,17 @@ class RepositoryCommand extends BaseCommand
28
28
*/
29
29
protected array $ stubs = [
30
30
'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 '
32
33
];
33
34
35
+ /**
36
+ * Boolean check to skip methods being executed
37
+ *
38
+ * @var bool
39
+ */
40
+ protected bool $ skip = false ;
41
+
34
42
public function __construct ()
35
43
{
36
44
parent ::__construct ();
@@ -39,34 +47,38 @@ public function __construct()
39
47
public function handle ()
40
48
{
41
49
$ 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
+ }
44
55
}
45
56
46
57
protected function checkModel ()
47
58
{
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 )];
49
63
50
- $ this ->model = str_replace ( ' / ' , '\\' , $ model) ;
64
+ $ this ->model = $ this -> appNamespace . "{ $ model}" ;
51
65
52
66
if ($ this ->laravel ->runningInConsole ()) {
53
67
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 ' );
55
69
56
70
if ($ this ->isResponsePositive ($ response )) {
57
71
Artisan::call ('make:model ' , [
58
72
'name ' => $ this ->model
59
73
]);
60
74
61
- $ this ->info ("Model [ {$ this ->model }] has been successfully created. " );
75
+ $ this ->info ("Model [ {$ this ->modelName }] has been successfully created. " );
62
76
} 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 ;
64
79
}
65
80
}
66
81
}
67
-
68
- $ modelInfo = explode ('\\' , $ this ->model );
69
- $ this ->modelName = $ modelInfo [array_key_last ($ modelInfo )];
70
82
}
71
83
72
84
/**
@@ -82,24 +94,24 @@ protected function createInterface()
82
94
$ content = $ this ->fileManager ->get ($ this ->stubs ['interface ' ]);
83
95
84
96
$ replacements = [
85
- '{{ namespace }} ' => "{$ this ->appNamespace }\ Repository\{$ this ->modelName } " ,
86
- '{{ model }} ' => $ this ->modelName
97
+ '% namespace% ' => "{$ this ->appNamespace }Repository \ \{$ this ->modelName }" ,
98
+ '% model% ' => $ this ->modelName
87
99
];
88
100
89
101
$ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
90
102
91
103
$ fileName = "{$ this ->modelName }RepositoryInterface " ;
92
104
$ fileDirectory = app ()->basePath () . "/App/Repository/ {$ this ->modelName }" ;
93
- $ filePath = "{$ fileDirectory }{$ fileName }.php " ;
105
+ $ filePath = "{$ fileDirectory }/ {$ fileName }.php " ;
94
106
95
107
if (!$ this ->fileManager ->exists ($ fileDirectory ))
96
108
$ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
97
109
98
- if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ fileDirectory )) {
110
+ if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ filePath )) {
99
111
$ response = $ this ->ask ("The interface [ {$ fileName }] has already exists. Do you want to overwrite it? " , 'Yes ' );
100
112
101
113
if (!$ this ->isResponsePositive ($ response )) {
102
- $ this ->line ("The interface [ {$ fileName }] will not be overwritten. " );
114
+ $ this ->info ("The interface [ {$ fileName }] will not be overwritten. " );
103
115
return ;
104
116
}
105
117
}
@@ -108,7 +120,7 @@ protected function createInterface()
108
120
109
121
$ this ->info ("The interface [ {$ fileName }] has been created " );
110
122
111
- return ["{$ this ->appNamespace }\ Repository\{$ this ->modelName }\{ $ fileName} " , $ fileName ];
123
+ return ["{$ this ->appNamespace }Repository \\ {$ this ->modelName }\ \{$ fileName }" , $ fileName ];
112
124
}
113
125
114
126
/**
@@ -123,21 +135,22 @@ protected function createInterface()
123
135
*/
124
136
protected function createRepository (string $ interface , string $ fileName )
125
137
{
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 ' ]);
127
140
128
141
$ 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 }"
134
147
];
135
148
136
149
$ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
137
150
138
151
$ fileName = "{$ this ->modelName }Repository " ;
139
152
$ fileDirectory = app ()->basePath () . "/App/Repository/ {$ this ->modelName }" ;
140
- $ filePath = "{$ fileDirectory }{$ fileName }.php " ;
153
+ $ filePath = "{$ fileDirectory }/ {$ fileName }.php " ;
141
154
142
155
if (!$ this ->fileManager ->exists ($ fileDirectory ))
143
156
$ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
@@ -153,7 +166,7 @@ protected function createRepository(string $interface, string $fileName)
153
166
154
167
$ this ->fileManager ->put ($ filePath , $ content );
155
168
156
- $ this ->info ("The repository [ {$ filePath }] has been created. " );
169
+ $ this ->info ("The repository [ {$ fileName }] has been created. " );
157
170
}
158
171
159
172
0 commit comments