@@ -48,6 +48,13 @@ private function writeGetterFunction(string $getterStub, string $columnName): st
48
48
$ getterStub );
49
49
}
50
50
51
+ private function writeSetterFunction (string $ setterStub , string $ columnName ): string
52
+ {
53
+ return str_replace ('{{ SetterName }} ' ,
54
+ ucfirst (camel_case ($ columnName )),
55
+ $ setterStub );
56
+ }
57
+
51
58
/**
52
59
* Execute the console command.
53
60
*
@@ -65,17 +72,18 @@ public function handle(): int
65
72
$ entityNamespace = config ('repository.path.namespace.entities ' );
66
73
$ factoryNamespace = config ('repository.path.namespace.factories ' );
67
74
$ repositoryNamespace = config ('repository.path.namespace.repositories ' );
68
- $ relativeMysqlRepositoryPath = config ('repository.path.relative.repositories ' )."\\$ entityName " ;
69
- $ mysqlRepositoryStubsPath = config ('repository.path.stub.repositories.mysql ' );
70
- $ filenameWithPath = $ relativeMysqlRepositoryPath .'\\' .$ mysqlRepositoryName .'.php ' ;
75
+ $ relativeMysqlRepositoryPath = config ('repository.path.relative.repositories ' )."$ entityName " ;
76
+ $ mysqlRepositoryStubsPath = __DIR__ . '/../../ ' . config ('repository.path.stub.repositories.mysql ' );
77
+ $ phpVersion = config ('repository.php_version ' );
78
+ $ filenameWithPath = $ relativeMysqlRepositoryPath . '/ ' . $ mysqlRepositoryName .'.php ' ;
71
79
72
80
if ($ this ->option ('delete ' )) {
73
81
unlink ("$ relativeMysqlRepositoryPath/ $ mysqlRepositoryName.php " );
74
82
$ this ->info ("MySql Repository \"$ mysqlRepositoryName \" has been deleted. " );
75
83
return 0 ;
76
84
}
77
85
78
- if ( ! file_exists ($ relativeMysqlRepositoryPath ) && ! mkdir ($ relativeMysqlRepositoryPath ) && ! is_dir ($ relativeMysqlRepositoryPath )) {
86
+ if ( ! file_exists ($ relativeMysqlRepositoryPath ) && ! mkdir ($ relativeMysqlRepositoryPath, 0775 , true ) && ! is_dir ($ relativeMysqlRepositoryPath )) {
79
87
$ this ->alert ("Directory \"$ relativeMysqlRepositoryPath \" was not created " );
80
88
return 0 ;
81
89
}
@@ -103,61 +111,66 @@ public function handle(): int
103
111
$ updateFunctionStub = file_get_contents ($ mysqlRepositoryStubsPath .'update.stub ' );
104
112
$ deleteAndUndeleteStub = file_get_contents ($ mysqlRepositoryStubsPath .'deleteAndUndelete.stub ' );
105
113
$ getterStub = file_get_contents ($ mysqlRepositoryStubsPath .'getter.stub ' );
114
+ $ setterStub = file_get_contents ($ mysqlRepositoryStubsPath .'setter.stub ' );
106
115
$ timeFieldStub = file_get_contents ($ mysqlRepositoryStubsPath .'timeField.stub ' );
116
+ $ functions = '' ;
107
117
108
118
// Initialize MySql Repository
109
- $ baseContent = substr_replace ($ baseContent ,
110
- $ this ->writeGetOneFunction ($ getOneStub , 'id ' , 'int ' ),
111
- -1 , 0 );
112
- $ baseContent = substr_replace ($ baseContent ,
113
- $ this ->writeGetAllFunction ($ getAllStub , 'id ' , 'int ' ),
114
- -1 , 0 );
119
+ $ functions .= $ this ->writeGetOneFunction ($ getOneStub , 'id ' , 'int ' );
120
+ $ functions .= $ this ->writeGetAllFunction ($ getAllStub , 'id ' , 'int ' );
115
121
116
122
if ($ detectForeignKeys ) {
117
123
foreach ($ foreignKeys as $ _foreignKey ) {
118
- $ baseContent = substr_replace ($ baseContent ,
119
- $ this ->writeGetOneFunction ($ getOneStub , $ _foreignKey ->COLUMN_NAME , $ entityName ),
120
- -1 , 0 );
121
- $ baseContent = substr_replace ($ baseContent ,
122
- $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ entityName ),
123
- -1 , 0 );
124
+ $ functions .= $ this ->writeGetOneFunction ($ getOneStub , $ _foreignKey ->COLUMN_NAME , $ entityName );
125
+ $ functions .= $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ entityName );
124
126
}
125
127
}
126
128
129
+ $ getterFunctions = '' ;
130
+ $ setterFunctions = '' ;
127
131
// Create "create" function
128
132
foreach ($ columns as $ _column ) {
129
- if ( ! in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'updated_at ' , 'deleted_at ' ])) {
130
- $ createFunctionStub = substr_replace ($ createFunctionStub ,
131
- $ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME ),
132
- -95 , 0 );
133
- } elseif (in_array ($ _column ->COLUMN_NAME , ['created_at ' , 'updated_at ' ], true )) {
134
- $ createFunctionStub = substr_replace ($ createFunctionStub ,
135
- $ this ->writeGetterFunction ($ timeFieldStub , $ _column ->COLUMN_NAME ),
136
- -95 , 0 );
133
+ if ( ! in_array ($ _column ->COLUMN_NAME , ['id ' , 'deleted_at ' ])) {
134
+ $ getterFunctions .= $ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME );
135
+ }
136
+ if (in_array ($ _column ->COLUMN_NAME , ['created_at ' , 'updated_at ' ], true )) {
137
+ $ setterFunctions .= $ this ->writeSetterFunction ($ setterStub , $ _column ->COLUMN_NAME );
137
138
}
138
139
}
139
- $ baseContent = substr_replace ($ baseContent , $ createFunctionStub , -1 , 0 );
140
+ $ createFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ SetterFunctions }} " ],
141
+ [substr ($ getterFunctions , 0 , -1 ), substr ($ setterFunctions , 0 , -1 )],
142
+ $ createFunctionStub
143
+ );
140
144
145
+ $ functions .= $ createFunctionStub ;
146
+
147
+ $ getterFunctions = '' ;
148
+ $ setterFunctions = '' ;
141
149
// Create "update" function
142
150
foreach ($ columns as $ _column ) {
143
- if ( ! in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'updated_at ' , 'deleted_at ' ])) {
144
- $ updateFunctionStub = substr_replace ($ updateFunctionStub ,
145
- $ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME ),
146
- -12 , 0 );
147
- } elseif ($ _column ->COLUMN_NAME === 'updated_at ' ) {
148
- $ updateFunctionStub = substr_replace ($ updateFunctionStub ,
149
- $ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME ),
150
- -12 , 0 );
151
+ if ( ! in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'deleted_at ' ])) {
152
+ $ getterFunctions .= $ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME );
153
+ }
154
+ if ($ _column ->COLUMN_NAME === 'updated_at ' ) {
155
+ $ setterFunctions .= $ this ->writeSetterFunction ($ setterStub , $ _column ->COLUMN_NAME );
151
156
}
152
157
}
153
- $ baseContent = substr_replace ($ baseContent , $ updateFunctionStub , -1 , 0 );
158
+ $ updateFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ UpdateFieldSetter }} " ],
159
+ [substr ($ getterFunctions , 0 , -1 ), substr ($ setterFunctions , 0 , -1 )],
160
+ $ updateFunctionStub
161
+ );
162
+
163
+ $ functions .= $ updateFunctionStub ;
154
164
155
165
// Create "delete" and "undelete" functions if necessary
156
166
$ hasSoftDelete = in_array ('deleted_at ' , $ columns ->pluck ('COLUMN_NAME ' )->toArray (), true );
157
167
if ($ hasSoftDelete ) {
158
- $ baseContent = substr_replace ( $ baseContent , $ deleteAndUndeleteStub, - 1 , 0 ) ;
168
+ $ functions .= $ deleteAndUndeleteStub ;
159
169
}
160
170
171
+ $ baseContent = str_replace ('{{ Functions }} ' ,
172
+ $ functions , $ baseContent );
173
+
161
174
$ baseContent = str_replace (['{{ EntityName }} ' , '{{ EntityNamespace }} ' , '{{ FactoryName }} ' , '{{ FactoryNamespace }} ' , '{{ EntityVariableName }} ' , '{{ MySqlRepositoryName }} ' , '{{ RepositoryNamespace }} ' , '{{ RepositoryInterfaceName }} ' , '{{ TableName }} ' , '{{ HasSoftDelete }} ' ],
162
175
[$ entityName , $ entityNamespace , $ factoryName , $ factoryNamespace , $ entityVariableName , $ mysqlRepositoryName , $ repositoryNamespace , $ interfaceName , $ tableName , $ hasSoftDelete ? 'true ' : 'false ' ],
163
176
$ baseContent );
@@ -172,4 +185,4 @@ public function handle(): int
172
185
173
186
return 0 ;
174
187
}
175
- }
188
+ }
0 commit comments