@@ -18,14 +18,17 @@ class Data extends AbstractHelper
1818{
1919 private $ directoryList ;
2020 private $ driverFile ;
21+ private $ model ;
2122
2223 public function __construct (
2324 \Magento \Framework \App \Helper \Context $ context ,
2425 \Magento \Framework \Filesystem \DirectoryList $ directoryList ,
25- \Magento \Framework \Filesystem \Driver \File $ driverFile
26+ \Magento \Framework \Filesystem \Driver \File $ driverFile ,
27+ \A2bizz \ModuleCreator \Model \ModuleCreator $ model
2628 ){
2729 $ this ->directoryList = $ directoryList ; // ?? ObjectManager::getInstance->get(DirectoryList::class);
2830 $ this ->driverFile = $ driverFile ;
31+ $ this ->model = $ model ;
2932
3033 parent ::__construct ($ context );
3134 }
@@ -67,29 +70,56 @@ public function getAllFilesPath($path = '/var') {
6770 }
6871
6972 /*
70- * return replaced path for new module to generate inside app/code/
73+ * return replaced path for new module to generate inside Root- app/code/
7174 */
72- public function newModuleFiles ($ data , $ filePathsArray ){
75+ public function newModuleFiles ($ parentModule , $ data , $ filePathsArray ){
7376 $ newFilesPathArray = [];
7477 $ namespace = $ data ['namespace ' ];
7578 $ module = $ data ['module ' ];
7679
77- $ capNamespace = ucfirst ($ namespace );
78- $ lowNamespace = strtolower ($ namespace );
79- $ capModule = ucfirst ($ module );
80- $ lowModule = strtolower ($ module );
81-
82- foreach ($ filePathsArray as $ filePath ):
83- $ filePath =str_replace ("/app/code/A2bizz/ModuleCreator/MTemplate " ,"" ,$ filePath );
80+ $ capNamespace = ucfirst ($ namespace ); //return {{Namespace}}
81+ $ lowNamespace = strtolower ($ namespace ); //return {{namespace}}
82+ $ capModule = ucfirst ($ module ); //return {{Module}}
83+ $ lowModule = strtolower ($ module ); //return {{module}}
8484
85- if ($ filePath =="/var/www/html/app/code/Namespace/Module/etc/module.xml " ):
85+ //echo "<pre>";
86+ //print_r($filePathsArray);
87+ //exit;
88+
89+ foreach ($ filePathsArray as $ key =>$ filePath ):
90+ $ filePath =str_replace ("/app/code/A2bizz/ModuleCreator/MTemplate " ,"" ,$ filePath );
91+ $ pathRoot = $ this ->directoryList ->getRoot ();
92+ $ moduleXMLPath = $ pathRoot .'/app/code/Namespace/Module/etc/module.xml ' ;
93+ if ($ filePath ==$ moduleXMLPath ):
8694 $ filePath =str_replace ("Namespace " ,$ capNamespace ,$ filePath );
8795 $ filePath =str_replace ("Module " ,$ capModule ,$ filePath );
8896
8997 $ newFilesPathArray []=$ filePath ;
9098 continue ;
9199 endif ;
92-
100+
101+ //Code for taking parent menu.xml and child menu.xml as per
102+ $ menuXMLPath = $ pathRoot .'/app/code/Namespace/Module/etc/adminhtml/menu.xml ' ;
103+ $ menuA2bizzXMLPath = $ pathRoot .'/app/code/Namespace/Module/etc/adminhtml/menu.xml.a2bizz ' ;
104+
105+ //Here we testing if Namespace already created, if not, then alternate menu.xml.a2bizz do not need modification
106+ //and will remain same except the path for new module
107+ if ($ parentModule ==NULL && $ filePath ==$ menuA2bizzXMLPath ):
108+ //var_dump($parentModule);
109+ $ filePath =str_replace ("Namespace " ,$ capNamespace ,$ filePath );
110+ $ filePath =str_replace ("Module " ,$ capModule ,$ filePath );
111+
112+ $ newFilesPathArray []=$ filePath ;
113+ continue ;
114+ endif ;
115+ //Here we testing if Namespace already created, if yes, then alternate menu.xml.a2bizz is needed modification
116+ //so here we are changing name of menu.xml to menu.xml.parent and menu.xaml.a2bizz will be menu.xml for new module created
117+ if ($ parentModule !=NULL && $ filePath ==$ menuXMLPath ):
118+ //echo $parentModule;
119+ $ filePath =str_replace ("menu.xml " ,"menu.xml.parent " ,$ filePath );
120+ endif ;
121+
122+
93123 $ filePath =str_replace (".a2bizz " ,"" ,$ filePath );
94124 $ filePath =str_replace ("Namespace " ,$ capNamespace ,$ filePath );
95125 $ filePath =str_replace ("namespace " ,$ lowNamespace ,$ filePath );
@@ -101,7 +131,7 @@ public function newModuleFiles($data, $filePathsArray){
101131 endforeach ;
102132
103133 //echo "<pre>";
104- //print_r($newFilesPathArray);exit;
134+ //print_r($newFilesPathArray);
105135 //exit;
106136 return $ newFilesPathArray ;
107137 }
@@ -126,27 +156,34 @@ public function createFoldersAndCopyBlankoFiles($fromFilesPathArray, $toFilesPat
126156 return true ;
127157 }
128158
129- public function replaceXml ($ data ,$ content ){
159+ public function replaceXml ($ parentModule , $ data ,$ content ){
130160 $ namespace = $ data ['namespace ' ];
131161 $ module = $ data ['module ' ];
132162
133163 $ capNamespace = ucfirst ($ namespace );
134164 $ lowNamespace = strtolower ($ namespace );
135165 $ capModule = ucfirst ($ module );
136166 $ lowModule = strtolower ($ module );
167+ if ($ parentModule !=NULL ):
168+ $ parentCapModule = ucfirst ($ parentModule );
169+ else :
170+ $ parentCapModule ="" ;
171+ endif ;
137172
138173 $ search = array (
139174 '/\[Namespace\]/ ' ,
140175 '/\[namespace\]/ ' ,
141176 '/\[Module\]/ ' ,
142177 '/\[module\]/ ' ,
178+ '/\[PModule\]/ ' ,
143179 );
144180
145181 $ replace = array (
146182 $ capNamespace ,
147183 $ lowNamespace ,
148184 $ capModule ,
149185 $ lowModule ,
186+ $ parentCapModule ,
150187 );
151188
152189 return preg_replace ($ search , $ replace , $ content );
@@ -178,7 +215,7 @@ public function replacePhp($data,$content){
178215 return preg_replace ($ search , $ replace , $ content );
179216 }
180217
181- public function insertVarsInNewCustomModule ($ data ,$ toFilesPathArray ){
218+ public function insertVarsInNewCustomModule ($ parentModule , $ data ,$ toFilesPathArray ){
182219 try {
183220 foreach ($ toFilesPathArray as $ toFilePath ):
184221 $ handle = fopen ($ toFilePath , 'r+ ' );
@@ -191,7 +228,9 @@ public function insertVarsInNewCustomModule($data,$toFilesPathArray){
191228 $ type = strrchr ($ toFilePath , '. ' );
192229 switch ($ type ) {
193230 case '.xml ' :
194- $ content = $ this ->replaceXml ($ data ,$ content );
231+ case '.a2bizz ' :
232+ case '.parent ' :
233+ $ content = $ this ->replaceXml ($ parentModule ,$ data ,$ content );
195234 break ;
196235 case '.php ' :
197236 case '.phtml ' :
@@ -214,19 +253,44 @@ public function insertVarsInNewCustomModule($data,$toFilesPathArray){
214253 return true ;
215254 }
216255
256+ /*
257+ * return true if Parent Available, False if not
258+ */
259+ public function checkParentStatus ($ data ){
260+ //gets count of a collection of namespace if already has module created
261+ $ parentModule =NULL ;
262+ $ collection =$ this ->model ->getCollection ()
263+ ->addFieldToFilter ('namespace ' ,$ data ['namespace ' ])
264+ ->addFieldToFilter ('is_parent ' ,1 );
265+
266+ if ($ collection ->count () > 0 ):
267+ foreach ($ collection as $ row ):
268+ $ parentModule =$ row ->getModule ();
269+ break ;
270+ endforeach ;
271+ endif ;
272+ return $ parentModule ;
273+ }
274+
217275 public function createNewModule ($ data ){
276+ //Check if the Namespace for new custom module already preset
277+ //We can say that we are looking if the parent Namespace already present for new creating module
278+ //if the Namespace with a module already present then it will be considered as Parent Module for Other cusotom Modules
279+ $ parentModule =$ this ->checkParentStatus ($ data );
280+ //echo $parentModule;exit;
281+
218282 // 1- get File paths from where we are copying files for creating custom Module
219283 $ fromFiles = $ this ->getAllFilesPath ();
220284
221285 // 2- convert path - Where you want to put new custom module
222286 // 3- add namespace and module - by which name, new custom module will be added
223- $ toFiles = $ this ->newModuleFiles ($ data ,$ fromFiles );
287+ $ toFiles = $ this ->newModuleFiles ($ parentModule , $ data ,$ fromFiles );
224288
225289 // 4- this function will create folders, on given path for new custom module
226290 // 5- also it will paste blanko files - files which we will use to creat custom module
227291 $ this ->createFoldersAndCopyBlankoFiles ($ fromFiles ,$ toFiles );
228292
229293 // 6- now this function will read each files and update namespace and modules in the given content
230- $ this ->insertVarsInNewCustomModule ($ data ,$ toFiles );
294+ $ this ->insertVarsInNewCustomModule ($ parentModule , $ data ,$ toFiles );
231295 }
232296}
0 commit comments