2
2
3
3
namespace Eghamat24 \DatabaseRepository \Creators ;
4
4
5
+ use Eghamat24 \DatabaseRepository \Models \Enums \DataTypeEnum ;
5
6
use Illuminate \Support \Collection ;
6
7
use Illuminate \Support \Str ;
7
8
use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
@@ -38,8 +39,8 @@ public function createUses(): array
38
39
return [
39
40
"use $ this ->entityNamespace \\$ this ->entityName ; " ,
40
41
"use $ this ->factoryNamespace \\$ this ->factoryName ; " ,
41
- " use Illuminate\Support\Collection; " ,
42
- " use Eghamat24\DatabaseRepository\Models\Repositories\MySqlRepository; "
42
+ ' use Illuminate\Support\Collection; ' ,
43
+ ' use Eghamat24\DatabaseRepository\Models\Repositories\MySqlRepository; '
43
44
];
44
45
}
45
46
@@ -50,7 +51,7 @@ public function getClassName(): string
50
51
51
52
public function getExtendSection (): string
52
53
{
53
- return " extends MySqlRepository implements " . $ this ->interfaceName ;
54
+ return ' extends MySqlRepository implements ' . $ this ->interfaceName ;
54
55
}
55
56
56
57
public function createAttributes (): array
@@ -61,96 +62,80 @@ public function createAttributes(): array
61
62
public function createFunctions (): array
62
63
{
63
64
64
- $ baseContent = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'class.stub ' );
65
- $ constructContent = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'construct.stub ' );
66
- $ getOneStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'getOneBy.stub ' );
67
- $ getAllStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'getAllBy.stub ' );
68
- $ createFunctionStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'create.stub ' );
69
- $ updateFunctionStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'update.stub ' );
70
- $ deleteStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'delete.stub ' );
71
- $ undeleteStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'undelete.stub ' );
72
- $ getterStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'getter.stub ' );
73
- $ setterStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'setter.stub ' );
74
- $ timeFieldStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'timeField.stub ' );
65
+ $ stubList = [
66
+ 'baseContent ' => 'class.stub ' ,
67
+ 'constructContent ' => 'construct.stub ' ,
68
+ 'getOneStub ' => 'getOneBy.stub ' ,
69
+ 'getAllStub ' => 'getAllBy.stub ' ,
70
+ 'createFunctionStub ' => 'create.stub ' ,
71
+ 'updateFunctionStub ' => 'update.stub ' ,
72
+ 'deleteStub ' => 'delete.stub ' ,
73
+ 'undeleteStub ' => 'undelete.stub ' ,
74
+ 'getterStub ' => 'getter.stub ' ,
75
+ 'setterStub ' => 'setter.stub ' ,
76
+ 'timeFieldStub ' => 'timeField.stub ' ,
77
+ ];
78
+
79
+ $ stubContent = [];
80
+ foreach ($ stubList as $ stubKey => $ stubName ) {
81
+ $ stubContent [$ stubKey ] = file_get_contents ($ this ->mysqlRepositoryStubsPath . $ stubName );
82
+ }
75
83
76
- $ functions = [];
77
- // Initialize MySql Repository
78
84
$ hasSoftDelete = in_array ('deleted_at ' , $ this ->columns ->pluck ('COLUMN_NAME ' )->toArray (), true );
79
- $ functions ['__construct ' ] = $ this ->getConstruct ($ this ->tableName , $ this ->factoryName , $ hasSoftDelete , $ constructContent );
80
- $ functions ['getOneById ' ] = $ this ->writeGetOneFunction ($ getOneStub , 'id ' , 'int ' );
81
- $ functions ['getAllByIds ' ] = $ this ->writeGetAllFunction ($ getAllStub , 'id ' , 'int ' );
85
+
86
+ $ functions = [];
87
+ $ functions ['__construct ' ] = $ this ->getConstruct ($ this ->tableName , $ this ->factoryName , $ hasSoftDelete , $ stubContent ['constructContent ' ]);
88
+ $ functions ['getOneById ' ] = $ this ->writeGetOneFunction ($ stubContent ['getOneStub ' ], 'id ' , DataTypeEnum::INTEGER_TYPE );
89
+ $ functions ['getAllByIds ' ] = $ this ->writeGetAllFunction ($ stubContent ['getAllStub ' ], 'id ' , DataTypeEnum::INTEGER_TYPE );
82
90
$ columnsInfo = $ this ->getAllColumnsInTable ($ this ->tableName );
83
91
84
92
$ indexes = $ this ->extractIndexes ($ this ->tableName );
85
93
foreach ($ indexes as $ index ) {
86
94
$ columnInfo = collect ($ columnsInfo )->where ('COLUMN_NAME ' , $ index ->COLUMN_NAME )->first ();
87
95
$ indx = 'getOneBy ' . ucfirst (Str::camel ($ index ->COLUMN_NAME ));
88
- $ functions [$ indx ] = $ this ->writeGetOneFunction ($ getOneStub , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE ));
96
+ $ functions [$ indx ] = $ this ->writeGetOneFunction (
97
+ $ stubContent ['getOneStub ' ],
98
+ $ index ->COLUMN_NAME ,
99
+ $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE )
100
+ );
89
101
90
102
if ($ index ->Non_unique == 1 ) {
91
103
$ indx = 'getAllBy ' . ucfirst (Str::plural (Str::camel ($ index ->COLUMN_NAME )));
92
- $ functions [$ indx ] = $ this ->writeGetAllFunction ($ getAllStub , $ index ->COLUMN_NAME , $ this ->entityName );
104
+ $ functions [$ indx ] = $ this ->writeGetAllFunction ($ stubContent [ ' getAllStub ' ] , $ index ->COLUMN_NAME , $ this ->entityName );
93
105
}
94
106
}
95
107
96
108
if ($ this ->detectForeignKeys ) {
97
109
$ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
98
110
foreach ($ foreignKeys as $ _foreignKey ) {
99
111
$ indx = 'getOneBy ' . ucfirst (Str::camel ($ _foreignKey ->COLUMN_NAME ));
100
- $ functions [$ indx ] = $ this ->writeGetOneFunction ($ getOneStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
112
+ $ functions [$ indx ] = $ this ->writeGetOneFunction ($ stubContent [ ' getOneStub ' ] , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
101
113
$ indx = 'getAllBy ' . ucfirst (Str::plural (Str::camel ($ _foreignKey ->COLUMN_NAME )));
102
- $ functions [$ indx ] = $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
114
+ $ functions [$ indx ] = $ this ->writeGetAllFunction ($ stubContent [ ' getAllStub ' ] , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
103
115
}
104
116
}
105
117
106
118
$ getterFunctions = '' ;
107
119
$ setterFunctions = '' ;
108
- // Create "create" function
109
- foreach ($ this ->columns as $ _column ) {
110
- if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'deleted_at ' ])) {
111
- $ getterFunctions .= trim ($ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
112
- }
113
- if (in_array ($ _column ->COLUMN_NAME , ['created_at ' , 'updated_at ' ], true )) {
114
- $ setterFunctions .= trim ($ this ->writeSetterFunction ($ setterStub , $ _column ->COLUMN_NAME )) . "\n\t\t" ;
115
- }
116
- }
117
- $ createFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ SetterFunctions }} " ],
118
- [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
119
- $ createFunctionStub
120
- );
121
-
122
- $ functions ['create ' ] = $ createFunctionStub ;
120
+ $ functions = $ this ->makeCreateFunction ($ stubContent , $ getterFunctions , $ setterFunctions , $ functions );
123
121
124
122
$ getterFunctions = '' ;
125
123
$ setterFunctions = '' ;
126
- // Create "update" function
127
- foreach ($ this ->columns as $ _column ) {
128
- if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'deleted_at ' ])) {
129
- $ getterFunctions .= trim ($ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
130
- }
131
- if ($ _column ->COLUMN_NAME === 'updated_at ' ) {
132
- $ setterFunctions .= trim ($ this ->writeSetterFunction ($ setterStub , $ _column ->COLUMN_NAME )) . "\n\t\t" ;
133
- }
134
- }
135
- $ updateFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ UpdateFieldSetter }} " ],
136
- [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
137
- $ updateFunctionStub
138
- );
139
-
140
- $ functions ['update ' ] = $ updateFunctionStub ;
124
+ $ functions = $ this ->makeUpdateFunction ($ stubContent , $ getterFunctions , $ setterFunctions , $ functions );
141
125
142
126
// Create "delete" and "undelete" functions if necessary
143
127
if ($ hasSoftDelete ) {
144
- $ functions ['remove ' ] = $ deleteStub ;
145
- $ functions ['restore ' ] = $ undeleteStub ;
128
+ $ functions ['remove ' ] = $ stubContent [ ' deleteStub ' ] ;
129
+ $ functions ['restore ' ] = $ stubContent [ ' undeleteStub ' ] ;
146
130
}
147
131
148
132
foreach ($ functions as &$ func ) {
149
- $ func = str_replace ([" {{ EntityName }} " , " {{ EntityVariableName }} " ],
133
+ $ func = str_replace ([' {{ EntityName }} ' , ' {{ EntityVariableName }} ' ],
150
134
[$ this ->entityName , $ this ->entityVariableName ],
151
135
$ func
152
136
);
153
137
}
138
+
154
139
return $ functions ;
155
140
}
156
141
@@ -189,4 +174,63 @@ private function getConstruct(string $tableName, string $factoryName, bool $hasS
189
174
[$ tableName , $ factoryName , $ hasSoftDelete ? 'true ' : 'false ' ],
190
175
$ constructContent );
191
176
}
177
+
178
+ /**
179
+ * @param array $stubContent
180
+ * @param string $getterFunctions
181
+ * @param string $setterFunctions
182
+ * @param array $functions
183
+ * @return array
184
+ */
185
+ public function makeCreateFunction (array &$ stubContent , string &$ getterFunctions , string &$ setterFunctions , array &$ functions ): array
186
+ {
187
+ foreach ($ this ->columns as $ _column ) {
188
+ if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'deleted_at ' ])) {
189
+ $ getterFunctions .= trim ($ this ->writeGetterFunction ($ stubContent ['getterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
190
+ }
191
+
192
+ if (in_array ($ _column ->COLUMN_NAME , ['created_at ' , 'updated_at ' ], true )) {
193
+ $ setterFunctions .= trim ($ this ->writeSetterFunction ($ stubContent ['setterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t" ;
194
+ }
195
+ }
196
+
197
+ $ createFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ SetterFunctions }} " ],
198
+ [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
199
+ $ stubContent ['createFunctionStub ' ]
200
+ );
201
+
202
+ $ functions ['create ' ] = $ createFunctionStub ;
203
+
204
+ return $ functions ;
205
+ }
206
+
207
+ /**
208
+ * @param array $stubContent
209
+ * @param string $getterFunctions
210
+ * @param string $setterFunctions
211
+ * @param array $functions
212
+ * @return array
213
+ */
214
+ public function makeUpdateFunction (array &$ stubContent , string &$ getterFunctions , string &$ setterFunctions , array &$ functions ): array
215
+ {
216
+ foreach ($ this ->columns as $ _column ) {
217
+
218
+ if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'deleted_at ' ])) {
219
+ $ getterFunctions .= trim ($ this ->writeGetterFunction ($ stubContent ['getterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
220
+ }
221
+
222
+ if ($ _column ->COLUMN_NAME === 'updated_at ' ) {
223
+ $ setterFunctions .= trim ($ this ->writeSetterFunction ($ stubContent ['setterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t" ;
224
+ }
225
+ }
226
+
227
+ $ updateFunctionStub = str_replace (['{{ GetterFunctions }} ' , '{{ UpdateFieldSetter }} ' ],
228
+ [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
229
+ $ stubContent ['updateFunctionStub ' ]
230
+ );
231
+
232
+ $ functions ['update ' ] = $ updateFunctionStub ;
233
+
234
+ return $ functions ;
235
+ }
192
236
}
0 commit comments