@@ -48,6 +48,13 @@ private function writeGetterFunction(string $getterStub, string $columnName): st
4848 $ getterStub );
4949 }
5050
51+ private function writeSetterFunction (string $ setterStub , string $ columnName ): string
52+ {
53+ return str_replace ('{{ SetterName }} ' ,
54+ ucfirst (camel_case ($ columnName )),
55+ $ setterStub );
56+ }
57+
5158 /**
5259 * Execute the console command.
5360 *
@@ -65,17 +72,18 @@ public function handle(): int
6572 $ entityNamespace = config ('repository.path.namespace.entities ' );
6673 $ factoryNamespace = config ('repository.path.namespace.factories ' );
6774 $ 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 ' ;
7179
7280 if ($ this ->option ('delete ' )) {
7381 unlink ("$ relativeMysqlRepositoryPath/ $ mysqlRepositoryName.php " );
7482 $ this ->info ("MySql Repository \"$ mysqlRepositoryName \" has been deleted. " );
7583 return 0 ;
7684 }
7785
78- if ( ! file_exists ($ relativeMysqlRepositoryPath ) && ! mkdir ($ relativeMysqlRepositoryPath ) && ! is_dir ($ relativeMysqlRepositoryPath )) {
86+ if ( ! file_exists ($ relativeMysqlRepositoryPath ) && ! mkdir ($ relativeMysqlRepositoryPath, 0775 , true ) && ! is_dir ($ relativeMysqlRepositoryPath )) {
7987 $ this ->alert ("Directory \"$ relativeMysqlRepositoryPath \" was not created " );
8088 return 0 ;
8189 }
@@ -103,61 +111,66 @@ public function handle(): int
103111 $ updateFunctionStub = file_get_contents ($ mysqlRepositoryStubsPath .'update.stub ' );
104112 $ deleteAndUndeleteStub = file_get_contents ($ mysqlRepositoryStubsPath .'deleteAndUndelete.stub ' );
105113 $ getterStub = file_get_contents ($ mysqlRepositoryStubsPath .'getter.stub ' );
114+ $ setterStub = file_get_contents ($ mysqlRepositoryStubsPath .'setter.stub ' );
106115 $ timeFieldStub = file_get_contents ($ mysqlRepositoryStubsPath .'timeField.stub ' );
116+ $ functions = '' ;
107117
108118 // 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 ' );
115121
116122 if ($ detectForeignKeys ) {
117123 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 );
124126 }
125127 }
126128
129+ $ getterFunctions = '' ;
130+ $ setterFunctions = '' ;
127131 // Create "create" function
128132 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 );
137138 }
138139 }
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+ );
140144
145+ $ functions .= $ createFunctionStub ;
146+
147+ $ getterFunctions = '' ;
148+ $ setterFunctions = '' ;
141149 // Create "update" function
142150 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 );
151156 }
152157 }
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 ;
154164
155165 // Create "delete" and "undelete" functions if necessary
156166 $ hasSoftDelete = in_array ('deleted_at ' , $ columns ->pluck ('COLUMN_NAME ' )->toArray (), true );
157167 if ($ hasSoftDelete ) {
158- $ baseContent = substr_replace ( $ baseContent , $ deleteAndUndeleteStub, - 1 , 0 ) ;
168+ $ functions .= $ deleteAndUndeleteStub ;
159169 }
160170
171+ $ baseContent = str_replace ('{{ Functions }} ' ,
172+ $ functions , $ baseContent );
173+
161174 $ baseContent = str_replace (['{{ EntityName }} ' , '{{ EntityNamespace }} ' , '{{ FactoryName }} ' , '{{ FactoryNamespace }} ' , '{{ EntityVariableName }} ' , '{{ MySqlRepositoryName }} ' , '{{ RepositoryNamespace }} ' , '{{ RepositoryInterfaceName }} ' , '{{ TableName }} ' , '{{ HasSoftDelete }} ' ],
162175 [$ entityName , $ entityNamespace , $ factoryName , $ factoryNamespace , $ entityVariableName , $ mysqlRepositoryName , $ repositoryNamespace , $ interfaceName , $ tableName , $ hasSoftDelete ? 'true ' : 'false ' ],
163176 $ baseContent );
@@ -172,4 +185,4 @@ public function handle(): int
172185
173186 return 0 ;
174187 }
175- }
188+ }
0 commit comments