44
55use Illuminate \Console \Command ;
66use Illuminate \Support \Arr ;
7+ use Illuminate \Support \Facades \Config ;
78use RonasIT \Support \Events \SuccessCreateMessage ;
89use RonasIT \Support \Exceptions \ClassNotExistsException ;
910use RonasIT \Support \Exceptions \EntityCreateException ;
@@ -163,6 +164,7 @@ public function __construct()
163164 public function handle ()
164165 {
165166 $ this ->validateInput ();
167+ $ this ->checkConfigs ();
166168 $ this ->eventDispatcher ->listen (SuccessCreateMessage::class, $ this ->getSuccessMessageCallback ());
167169
168170 try {
@@ -172,6 +174,57 @@ public function handle()
172174 }
173175 }
174176
177+ protected function checkConfigs ()
178+ {
179+ $ packageConfigPath = __DIR__ . '/../../config/entity-generator.php ' ;
180+ $ packageConfigs = require $ packageConfigPath ;
181+
182+ $ projectConfigs = config ('entity-generator ' );
183+
184+ $ newConfig = $ this ->outputNewConfig ($ packageConfigs , $ projectConfigs );
185+
186+ if ($ newConfig !== $ projectConfigs ) {
187+ $ this ->info ('Config has been updated ' );
188+ Config::set ('entity-generator ' , $ newConfig );
189+ file_put_contents (config_path ('entity-generator.php ' ), "<?php \n\nreturn " . $ this ->customVarExport ($ newConfig ) . '; ' );
190+ }
191+ }
192+
193+ protected function outputNewConfig ($ packageConfigs , $ projectConfigs )
194+ {
195+ $ flattenedPackageConfigs = Arr::dot ($ packageConfigs );
196+ $ flattenedProjectConfigs = Arr::dot ($ projectConfigs );
197+
198+ $ newConfig = array_merge ($ flattenedPackageConfigs , $ flattenedProjectConfigs );
199+
200+ $ differences = array_diff_key ($ newConfig , $ flattenedProjectConfigs );
201+
202+ foreach ($ differences as $ differenceKey => $ differenceValue ) {
203+ $ this ->info ("Key ' {$ differenceKey }' was missing in your config, we added it with the value ' {$ differenceValue }' " );
204+ }
205+
206+ return array_undot ($ newConfig );
207+ }
208+
209+ protected function customVarExport ($ expression )
210+ {
211+ $ defaultExpression = var_export ($ expression , true );
212+
213+ $ patterns = [
214+ '/array/ ' => '' ,
215+ '/\(/ ' => '[ ' ,
216+ '/\)/ ' => '] ' ,
217+ '/=> \\n/ ' => '=> ' ,
218+ '/=>.+\[/ ' => '=> [ ' ,
219+ '/^ {8}/m ' => "\t\t\t\t" ,
220+ '/^ {6}/m ' => "\t\t\t" ,
221+ '/^ {4}/m ' => "\t\t" ,
222+ '/^ {2}/m ' => "\t" ,
223+ ];
224+
225+ return preg_replace (array_keys ($ patterns ), array_values ($ patterns ), $ defaultExpression );
226+ }
227+
175228 protected function classExists ($ path , $ name )
176229 {
177230 $ paths = config ('entity-generator.paths ' );
0 commit comments