1515
1616class DevtoolsModules extends BasePackage
1717{
18- public function updateModules ( array $ data )
18+ public function addModule ( $ data )
1919 {
2020 if ($ data ['type ' ] === 'core ' ) {
2121 $ this ->updateCore ($ data );
@@ -30,6 +30,32 @@ public function updateModules(array $data)
3030 }
3131 }
3232
33+ public function updateModule ($ data )
34+ {
35+ if ($ data ['type ' ] === 'core ' ) {
36+ $ this ->updateCore ($ data );
37+ } else if ($ data ['type ' ] === 'components ' ) {
38+ $ this ->updateComponent ($ data );
39+ } else if ($ data ['type ' ] === 'packages ' ) {
40+ $ this ->updatePackage ($ data );
41+ } else if ($ data ['type ' ] === 'middlewares ' ) {
42+ $ this ->updateMiddleware ($ data );
43+ } else if ($ data ['type ' ] === 'views ' ) {
44+ $ this ->updateView ($ data );
45+ }
46+ }
47+
48+ // public function removeModule($data)
49+ // {
50+ // $module = $this->getById($data['id']);
51+
52+ // if ($this->remove($module['id'])) {
53+ // $this->addResponse('Removed module ' . $module['name']);
54+ // } else {
55+ // $this->addResponse('Error removing module.', 1);
56+ // }
57+ // }
58+
3359 protected function updateCore (array $ data )
3460 {
3561 if ($ this ->core ->update ($ data )) {
@@ -188,43 +214,15 @@ protected function updateView(array $data)
188214
189215 $ view = array_merge ($ view , $ data );
190216
191- $ this ->modules ->views ->update ($ view );
192-
193- $ pathArr [0 ] = 'apps ' ;
194- $ pathArr [1 ] = ucfirst ($ view ['app_type ' ]);
195- $ pathArr [2 ] = ucfirst ('Views ' );
196- $ pathArr [3 ] = ucfirst ($ view ['name ' ]);
197-
198- $ path = implode ('/ ' , $ pathArr ) . '/view.json ' ;
217+ if ($ this ->modules ->views ->update ($ view )) {
218+ if ($ this ->updateModuleJson ($ data )) {
219+ $ this ->addResponse ('Module updated ' );
199220
200- try {
201- $ jsonFile = $ this ->localContent ->fileExists ($ path );
202- } catch (FilesystemException | UnableToRetrieveMetadata $ exception ) {
203- throw $ exception ;
221+ return ;
222+ }
204223 }
205224
206- $ jsonContent = [];
207- $ jsonContent ['name ' ] = $ view ['name ' ];
208- $ jsonContent ['display_name ' ] = $ view ['display_name ' ];
209- $ jsonContent ['description ' ] = $ view ['description ' ];
210- $ jsonContent ['app_type ' ] = $ view ['app_type ' ];
211- $ jsonContent ['category ' ] = $ view ['category ' ];
212- $ jsonContent ['version ' ] = $ view ['version ' ];
213- $ jsonContent ['repo ' ] = $ view ['repo ' ];
214- $ jsonContent ['dependencies ' ] = $ view ['dependencies ' ];
215- $ jsonContent ['settings ' ] = $ view ['settings ' ];
216-
217- $ jsonContent = Json::encode ($ jsonContent , JSON_UNESCAPED_SLASHES |JSON_PRETTY_PRINT );
218-
219- $ jsonContent = str_replace ('\\" ' , '" ' , $ jsonContent );
220- $ jsonContent = str_replace ('"{ ' , '{ ' , $ jsonContent );
221- $ jsonContent = str_replace ('}" ' , '} ' , $ jsonContent );
222-
223- try {
224- $ this ->localContent ->write ($ path , $ jsonContent );
225- } catch (FilesystemException | UnableToWriteFile $ exception ) {
226- throw $ exception ;
227- }
225+ $ this ->addResponse ('Error updating Module ' , 1 );
228226 }
229227
230228 public function validateJson ($ data )
@@ -314,4 +312,105 @@ public function getDefaultDependencies()
314312
315313 return Json::encode ($ defaultDependencies );
316314 }
315+
316+ protected function updateModuleJson ($ data )
317+ {
318+ $ jsonFile = $ this ->getModuleJsonFileLocation ($ data );
319+
320+ if ($ data ['module_type ' ] === 'components ' ) {
321+ } else if ($ data ['module_type ' ] === 'packages ' ) {
322+ } else if ($ data ['module_type ' ] === 'middlewares ' ) {
323+ } else if ($ data ['module_type ' ] === 'views ' ) {
324+
325+ $ data = $ this ->jsonDecodeData ($ data );
326+
327+ $ jsonContent = [];
328+ $ jsonContent ["name " ] = $ data ["name " ];
329+ $ jsonContent ["display_name " ] = $ data ["display_name " ];
330+ $ jsonContent ["description " ] = $ data ["description " ];
331+ $ jsonContent ["module_type " ] = $ data ["module_type " ];
332+ $ jsonContent ["app_type " ] = $ data ["app_type " ];
333+ $ jsonContent ["category " ] = $ data ["category " ];
334+ $ jsonContent ["version " ] = $ data ["version " ];
335+ $ jsonContent ["repo " ] = $ data ["repo " ];
336+ $ jsonContent ["dependencies " ] = $ data ["dependencies " ];
337+ $ jsonContent ["settings " ] = $ data ["settings " ];
338+
339+ }
340+
341+ $ jsonContent = Json::encode ($ jsonContent , JSON_UNESCAPED_SLASHES );
342+
343+ $ jsonContent = str_replace ('\\" ' , '" ' , $ jsonContent );
344+ $ jsonContent = str_replace ('"{ ' , '{ ' , $ jsonContent );
345+ $ jsonContent = str_replace ('}" ' , '} ' , $ jsonContent );
346+ $ jsonContent = $ this ->basepackages ->utils ->formatJson (['json ' => $ jsonContent ]);
347+
348+ try {
349+ $ this ->localContent ->write ($ jsonFile , $ jsonContent );
350+ } catch (FilesystemException | UnableToWriteFile $ exception ) {
351+ $ this ->addResponse ('Unable to write json content to file: ' . $ jsonFile );
352+
353+ return false ;
354+ }
355+
356+ return true ;
357+ }
358+
359+ protected function getModuleJsonFileLocation ($ data )
360+ {
361+ if ($ data ['module_type ' ] === 'components ' ) {
362+ $ moduleLocation = 'apps/ ' . ucfirst ($ data ['app_type ' ]) . '/Components/ ' ;
363+ $ this ->view ->moduleMenu = $ data ['menu ' ];
364+ } else if ($ data ['module_type ' ] === 'packages ' ) {
365+ if ($ data ['app_type ' ] === 'core ' &&
366+ ($ data ['category ' ] === 'basepackages ' ||
367+ $ data ['category ' ] === 'providers ' )
368+ ) {
369+ if ($ data ['category ' ] === 'basepackages ' ) {
370+ $ moduleLocation = 'system/Base/Installer/Packages/Setup/Register/Modules/Packages/Basepackages/ ' ;
371+ } else if ($ data ['category ' ] === 'providers ' ) {
372+ $ moduleLocation = 'system/Base/Installer/Packages/Setup/Register/Modules/Packages/Providers/ ' ;
373+ }
374+ } else {
375+ $ moduleLocation = 'apps/ ' . ucfirst ($ data ['app_type ' ]) . '/Packages/ ' ;
376+ }
377+ } else if ($ data ['module_type ' ] === 'middlewares ' ) {
378+ $ moduleLocation = 'apps/ ' . ucfirst ($ data ['app_type ' ]) . '/Middlewares/ ' ;
379+ } else if ($ data ['module_type ' ] === 'views ' ) {
380+ $ moduleLocation = 'apps/ ' . ucfirst ($ data ['app_type ' ]) . '/Views/ ' ;
381+ }
382+
383+ if ($ data ['module_type ' ] === 'packages ' &&
384+ ($ data ['category ' ] === 'basepackages ' ||
385+ $ data ['category ' ] === 'providers ' )
386+ ) {
387+ return
388+ $ moduleLocation .
389+ ucfirst ($ data ['name ' ]) . '/ ' .
390+ substr ($ data ['module_type ' ], 0 , -1 ) . '.json ' ;
391+ } else {
392+ if ($ data ['module_type ' ] === 'components ' ) {
393+ $ routeArr = explode ('/ ' , $ data ['route ' ]);
394+
395+ foreach ($ routeArr as &$ path ) {
396+ $ path = ucfirst ($ path );
397+ }
398+
399+ $ routePath = implode ('/ ' , $ routeArr ) . '/Install/ ' ;
400+ } else if ($ data ['module_type ' ] === 'middlewares ' ) {
401+ $ routePath = $ data ['name ' ] . '/Install/ ' ;
402+ } else if ($ data ['module_type ' ] === 'packages ' ) {
403+ $ pathArr = preg_split ('/(?=[A-Z])/ ' , $ data ['name ' ], -1 , PREG_SPLIT_NO_EMPTY );
404+
405+ $ routePath = implode ('/ ' , $ pathArr ) . '/Install/ ' ;
406+ } else if ($ data ['module_type ' ] === 'views ' ) {
407+ $ routePath = $ data ['name ' ] . '/ ' ;
408+ }
409+
410+ return
411+ $ moduleLocation .
412+ $ routePath .
413+ substr ($ data ['module_type ' ], 0 , -1 ) . '.json ' ;
414+ }
415+ }
317416}
0 commit comments