2
2
3
3
namespace Eghamat24 \DatabaseRepository \Commands ;
4
4
5
+ use Eghamat24 \DatabaseRepository \Models \Enums \DataTypeEnum ;
6
+ use Illuminate \Support \Collection ;
5
7
use Illuminate \Support \Str ;
6
8
use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
7
- use Illuminate \Console \Command ;
8
9
9
10
class MakeInterfaceRepository extends BaseCommand
10
11
{
12
+ use CustomMySqlQueries;
13
+
11
14
/**
12
15
* The name and signature of the console command.
13
16
*
@@ -26,89 +29,194 @@ class MakeInterfaceRepository extends BaseCommand
26
29
*/
27
30
protected $ description = 'Create a new interface for repository ' ;
28
31
29
- use CustomMySqlQueries;
32
+
33
+ public function handle (): void
34
+ {
35
+ $ this ->setArguments ();
36
+ $ filenameWithPath = $ this ->relativeInterfacePath . $ this ->interfaceName . '.php ' ;
37
+
38
+ $ this ->checkAndPrepare ($ filenameWithPath );
39
+
40
+ [
41
+ 'basedContent ' => $ baseContent ,
42
+ 'getOneStub ' => $ getOneStub ,
43
+ 'getAllStub ' => $ getAllStub ,
44
+ 'createFunctionStub ' => $ createFunctionStub ,
45
+ 'updateFunctionStub ' => $ updateFunctionStub ,
46
+ 'deleteAndUndeleteStub ' => $ deleteAndUndeleteStub
47
+ ] = $ this ->getStubContents ($ this ->interfaceRepositoryStubsPath );
48
+
49
+ $ baseContent = $ this ->writeFunctionOnBaseContent (
50
+ $ baseContent , $ this ->writeGetOneFunction ($ getOneStub , 'id ' , DataTypeEnum::INTEGER_TYPE )
51
+ );
52
+
53
+ $ baseContent = $ this ->writeFunctionOnBaseContent (
54
+ $ baseContent , $ this ->writeGetAllFunction ($ getAllStub , 'id ' , DataTypeEnum::INTEGER_TYPE )
55
+ );
56
+
57
+ $ columns = $ this ->getColumnsOf ($ this ->tableName );
58
+ $ indexes = $ this ->extractIndexes ($ this ->tableName );
59
+ $ baseContent = $ this ->writeGetFunctionByIndexColumnOnBaseContent ($ indexes , $ columns , $ baseContent , $ getOneStub , $ getAllStub );
60
+ $ baseContent = $ this ->writeGetFunctionByForeignKeyOnBaseContent ($ baseContent , $ getOneStub , $ getAllStub );
61
+
62
+ $ allColumns = $ columns ->pluck ('COLUMN_NAME ' )->toArray ();
63
+ $ baseContent = $ this ->setTimestampsColumnOnBaseContent (
64
+ $ allColumns , $ baseContent , $ createFunctionStub , $ updateFunctionStub , $ deleteAndUndeleteStub
65
+ );
66
+
67
+ $ baseContent = $ this ->replaceDataOnInterfaceContent ($ baseContent );
68
+
69
+ $ this ->finalized ($ filenameWithPath , $ this ->entityName , $ baseContent );
70
+ }
71
+
72
+ private function writeFunctionOnBaseContent ($ baseContent , string $ writeFunction ): string |array
73
+ {
74
+ return substr_replace ($ baseContent , $ writeFunction , -2 , 0 );
75
+ }
76
+
77
+ private function writeFunction (string $ stub , string $ columnName , string $ attributeType , array $ placeHolders ): array |string
78
+ {
79
+ $ replaceValues = \array_map (function ($ placeholder ) use ($ columnName , $ attributeType ) {
80
+
81
+ return match ($ placeholder ) {
82
+ '{{ FunctionName }} ' => ucfirst (Str::camel ($ columnName )),
83
+ '{{ ColumnName }} ' => $ columnName ,
84
+ '{{ AttributeType }} ' => $ attributeType ,
85
+ '{{ AttributeName }} ' => Str::camel ($ columnName ),
86
+ '{{ FunctionNamePlural }} ' => ucfirst (Str::plural (Str::camel ($ columnName ))),
87
+ '{{ AttributeNamePlural }} ' => Str::plural (Str::camel ($ columnName )),
88
+ default => $ placeholder ,
89
+ };
90
+ }, $ placeHolders );
91
+
92
+ return str_replace ($ placeHolders , $ replaceValues , $ stub );
93
+ }
30
94
31
95
private function writeGetOneFunction (string $ getOneStub , string $ columnName , string $ attributeType ): string
32
96
{
33
- return str_replace (['{{ FunctionName }} ' , '{{ ColumnName }} ' , '{{ AttributeType }} ' , '{{ AttributeName }} ' ],
34
- [ucfirst (Str::camel ($ columnName )), $ columnName , $ attributeType , Str::camel ($ columnName )],
35
- $ getOneStub );
97
+ $ placeHolders = ['{{ FunctionName }} ' , '{{ ColumnName }} ' , '{{ AttributeType }} ' , '{{ AttributeName }} ' ];
98
+ return $ this ->writeFunction ($ getOneStub , $ columnName , $ attributeType , $ placeHolders );
36
99
}
37
100
38
- private function writeGetAllFunction (string $ getOneStub , string $ columnName , string $ attributeType ): string
101
+ private function writeGetAllFunction (string $ getAllStub , string $ columnName , string $ attributeType ): string
39
102
{
40
- return str_replace (['{{ FunctionNamePlural }} ' , '{{ AttributeType }} ' , '{{ AttributeNamePlural }} ' ],
41
- [ucfirst (Str::plural (Str::camel ($ columnName ))), $ attributeType , Str::plural (Str::camel ($ columnName ))],
42
- $ getOneStub );
103
+ $ placeHolders = ['{{ FunctionNamePlural }} ' , '{{ AttributeType }} ' , '{{ AttributeNamePlural }} ' ];
104
+ return $ this ->writeFunction ($ getAllStub , $ columnName , $ attributeType , $ placeHolders );
43
105
}
44
106
45
- /**
46
- * Execute the console command.
47
- *
48
- * @return int
49
- */
50
- public function handle (): void
107
+ private function getColumnsOf (string $ tableName ): Collection
51
108
{
52
- $ this ->setArguments ( );
53
- $ filenameWithPath = $ this ->relativeInterfacePath . $ this -> interfaceName . ' .php ' ;
109
+ $ columns = $ this ->getAllColumnsInTable ( $ tableName );
110
+ $ this ->checkEmpty ( $ columns , $ tableName ) ;
54
111
55
- $ this ->checkDelete ($ filenameWithPath , $ this ->interfaceName , "Interface " );
112
+ return $ columns ;
113
+ }
114
+
115
+ private function checkAndPrepare (string $ filenameWithPath ): void
116
+ {
117
+ $ this ->checkDelete ($ filenameWithPath , $ this ->interfaceName , 'Interface ' );
56
118
$ this ->checkDirectory ($ this ->relativeInterfacePath );
57
- $ this ->checkClassExist ($ this ->repositoryNamespace , $ this ->interfaceName , "Interface " );
119
+ $ this ->checkClassExist ($ this ->repositoryNamespace , $ this ->interfaceName , 'Interface ' );
120
+ }
58
121
59
- $ columns = $ this ->getAllColumnsInTable ($ this ->tableName );
60
- $ this ->checkEmpty ($ columns , $ this ->tableName );
122
+ private function setTimestampsColumnOnBaseContent (
123
+ array $ allColumns ,
124
+ array |string $ baseContent ,
125
+ bool |string $ createFunctionStub ,
126
+ bool |string $ updateFunctionStub ,
127
+ bool |string $ deleteAndUndeleteStub ): string |array
128
+ {
129
+ if (in_array ('created_at ' , $ allColumns , true )) {
130
+ $ baseContent = substr_replace ($ baseContent , $ createFunctionStub , -2 , 0 );
131
+ }
132
+
133
+ if (in_array ('updated_at ' , $ allColumns , true )) {
134
+ $ baseContent = substr_replace ($ baseContent , $ updateFunctionStub , -2 , 0 );
135
+ }
136
+
137
+ if (in_array ('deleted_at ' , $ allColumns , true )) {
138
+ $ baseContent = substr_replace ($ baseContent , $ deleteAndUndeleteStub , -2 , 0 );
139
+ }
61
140
62
- if ($ this ->detectForeignKeys ) {
63
- $ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
141
+ return $ baseContent ;
142
+ }
143
+
144
+ private function getStubContents (string $ basePath ): array
145
+ {
146
+ $ stubs = [
147
+ 'basedContent ' => 'class.stub ' ,
148
+ 'getOneStub ' => 'getOneBy.stub ' ,
149
+ 'getAllStub ' => 'getAllBy.stub ' ,
150
+ 'createFunctionStub ' => 'create.stub ' ,
151
+ 'updateFunctionStub ' => 'update.stub ' ,
152
+ 'deleteAndUndeleteStub ' => 'deleteAndUndelete.stub ' ,
153
+ ];
154
+
155
+ $ stubsContent = [];
156
+
157
+ foreach ($ stubs as $ name => $ endWith ) {
158
+ $ stubsContent [$ name ] = file_get_contents ($ basePath . $ endWith );
64
159
}
65
160
66
- $ baseContent = file_get_contents ($ this ->interfaceRepositoryStubsPath . 'class.stub ' );
67
- $ getOneStub = file_get_contents ($ this ->interfaceRepositoryStubsPath . 'getOneBy.stub ' );
68
- $ getAllStub = file_get_contents ($ this ->interfaceRepositoryStubsPath . 'getAllBy.stub ' );
69
- $ createFunctionStub = file_get_contents ($ this ->interfaceRepositoryStubsPath . 'create.stub ' );
70
- $ updateFunctionStub = file_get_contents ($ this ->interfaceRepositoryStubsPath . 'update.stub ' );
71
- $ deleteAndUndeleteStub = file_get_contents ($ this ->interfaceRepositoryStubsPath . 'deleteAndUndelete.stub ' );
161
+ return $ stubsContent ;
162
+ }
72
163
73
- $ baseContent = substr_replace ($ baseContent , $ this ->writeGetOneFunction ($ getOneStub , 'id ' , 'int ' ), -2 , 0 );
74
- $ baseContent = substr_replace ($ baseContent , $ this ->writeGetAllFunction ($ getAllStub , 'id ' , 'int ' ), -2 , 0 );
75
- $ columnsInfo = $ this ->getAllColumnsInTable ($ this ->tableName );
164
+ private function replaceDataOnInterfaceContent (array |string $ baseContent ): string |array
165
+ {
166
+ $ placeHolders = [
167
+ '{{ EntityName }} ' => $ this ->entityName ,
168
+ '{{ EntityNamespace }} ' => $ this ->entityNamespace ,
169
+ '{{ EntityVariableName }} ' => $ this ->entityVariableName ,
170
+ '{{ InterfaceRepositoryName }} ' => $ this ->interfaceName ,
171
+ '{{ RepositoryNamespace }} ' => $ this ->repositoryNamespace
172
+ ];
173
+
174
+ return \str_replace (\array_keys ($ placeHolders ), \array_values ($ placeHolders ), $ baseContent );
175
+ }
76
176
77
- $ indexes = $ this ->extractIndexes ($ this ->tableName );
177
+ private function writeGetFunctionByIndexColumnOnBaseContent (Collection $ indexes , Collection $ columns , mixed $ baseContent , $ getOneStub , $ getAllStub ): mixed
178
+ {
78
179
foreach ($ indexes as $ index ) {
79
- $ columnInfo = collect ($ columnsInfo )->where ('COLUMN_NAME ' , $ index ->COLUMN_NAME )->first ();
80
- $ baseContent = substr_replace ($ baseContent , $ this ->writeGetOneFunction ($ getOneStub , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE )), -2 , 0 );
180
+ $ columnInfo = collect ($ columns )->where ('COLUMN_NAME ' , $ index ->COLUMN_NAME )->first ();
181
+
182
+ $ baseContent = $ this ->writeFunctionOnBaseContent ($ baseContent ,
183
+ $ this ->writeGetOneFunction (
184
+ $ getOneStub , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE )
185
+ )
186
+ );
81
187
82
188
if ($ index ->Non_unique == 1 ) {
83
- $ baseContent = substr_replace ($ baseContent , $ this ->writeGetOneFunction ($ getAllStub , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE )), -2 , 0 );
84
- }
85
- }
86
189
87
- if ($ this ->detectForeignKeys ) {
88
- foreach ($ foreignKeys as $ _foreignKey ) {
89
- $ baseContent = substr_replace ($ baseContent , $ this ->writeGetOneFunction ($ getOneStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName ), -2 , 0 );
90
- $ baseContent = substr_replace ($ baseContent , $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName ), -2 , 0 );
190
+ $ baseContent = $ this ->writeFunctionOnBaseContent ($ baseContent ,
191
+ $ this ->writeGetOneFunction (
192
+ $ getAllStub , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE )
193
+ )
194
+ );
91
195
}
92
196
}
93
197
94
- $ allColumns = $ columns ->pluck ('COLUMN_NAME ' )->toArray ();
198
+ return $ baseContent ;
199
+ }
95
200
96
- if (in_array ('created_at ' , $ allColumns , true )) {
97
- $ baseContent = substr_replace ($ baseContent , $ createFunctionStub , -2 , 0 );
201
+ public function writeGetFunctionByForeignKeyOnBaseContent (array |string $ baseContent , $ getOneStub , $ getAllStub ): string |array
202
+ {
203
+ if (empty ($ this ->detectForeignKeys )) {
204
+ return $ baseContent ;
98
205
}
99
206
100
- if (in_array ('updated_at ' , $ allColumns , true )) {
101
- $ baseContent = substr_replace ($ baseContent , $ updateFunctionStub , -2 , 0 );
102
- }
207
+ $ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
103
208
104
- if (in_array ('deleted_at ' , $ allColumns , true )) {
105
- $ baseContent = substr_replace ($ baseContent , $ deleteAndUndeleteStub , -2 , 0 );
106
- }
209
+ foreach ($ foreignKeys as $ _foreignKey ) {
107
210
108
- $ baseContent = str_replace ([ ' {{ EntityName }} ' , ' {{ EntityNamespace }} ' , ' {{ EntityVariableName }} ' , ' {{ InterfaceRepositoryName }} ' , ' {{ RepositoryNamespace }} ' ],
109
- [ $ this -> entityName , $ this -> entityNamespace , $ this ->entityVariableName , $ this -> interfaceName , $ this ->repositoryNamespace ],
110
- $ baseContent );
211
+ $ baseContent = $ this -> writeFunctionOnBaseContent (
212
+ $ baseContent , $ this ->writeGetOneFunction ( $ getOneStub , $ _foreignKey -> COLUMN_NAME , $ this ->entityName )
213
+ );
111
214
112
- $ this ->finalized ($ filenameWithPath , $ this ->entityName , $ baseContent );
215
+ $ baseContent = $ this ->writeFunctionOnBaseContent (
216
+ $ baseContent , $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName )
217
+ );
218
+ }
219
+
220
+ return $ baseContent ;
113
221
}
114
222
}
0 commit comments