@@ -18,14 +18,17 @@ class Data extends AbstractHelper
18
18
{
19
19
private $ directoryList ;
20
20
private $ driverFile ;
21
+ private $ model ;
21
22
22
23
public function __construct (
23
24
\Magento \Framework \App \Helper \Context $ context ,
24
25
\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
26
28
){
27
29
$ this ->directoryList = $ directoryList ; // ?? ObjectManager::getInstance->get(DirectoryList::class);
28
30
$ this ->driverFile = $ driverFile ;
31
+ $ this ->model = $ model ;
29
32
30
33
parent ::__construct ($ context );
31
34
}
@@ -67,29 +70,56 @@ public function getAllFilesPath($path = '/var') {
67
70
}
68
71
69
72
/*
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/
71
74
*/
72
- public function newModuleFiles ($ data , $ filePathsArray ){
75
+ public function newModuleFiles ($ parentModule , $ data , $ filePathsArray ){
73
76
$ newFilesPathArray = [];
74
77
$ namespace = $ data ['namespace ' ];
75
78
$ module = $ data ['module ' ];
76
79
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}}
84
84
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 ):
86
94
$ filePath =str_replace ("Namespace " ,$ capNamespace ,$ filePath );
87
95
$ filePath =str_replace ("Module " ,$ capModule ,$ filePath );
88
96
89
97
$ newFilesPathArray []=$ filePath ;
90
98
continue ;
91
99
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
+
93
123
$ filePath =str_replace (".a2bizz " ,"" ,$ filePath );
94
124
$ filePath =str_replace ("Namespace " ,$ capNamespace ,$ filePath );
95
125
$ filePath =str_replace ("namespace " ,$ lowNamespace ,$ filePath );
@@ -101,7 +131,7 @@ public function newModuleFiles($data, $filePathsArray){
101
131
endforeach ;
102
132
103
133
//echo "<pre>";
104
- //print_r($newFilesPathArray);exit;
134
+ //print_r($newFilesPathArray);
105
135
//exit;
106
136
return $ newFilesPathArray ;
107
137
}
@@ -126,27 +156,34 @@ public function createFoldersAndCopyBlankoFiles($fromFilesPathArray, $toFilesPat
126
156
return true ;
127
157
}
128
158
129
- public function replaceXml ($ data ,$ content ){
159
+ public function replaceXml ($ parentModule , $ data ,$ content ){
130
160
$ namespace = $ data ['namespace ' ];
131
161
$ module = $ data ['module ' ];
132
162
133
163
$ capNamespace = ucfirst ($ namespace );
134
164
$ lowNamespace = strtolower ($ namespace );
135
165
$ capModule = ucfirst ($ module );
136
166
$ lowModule = strtolower ($ module );
167
+ if ($ parentModule !=NULL ):
168
+ $ parentCapModule = ucfirst ($ parentModule );
169
+ else :
170
+ $ parentCapModule ="" ;
171
+ endif ;
137
172
138
173
$ search = array (
139
174
'/\[Namespace\]/ ' ,
140
175
'/\[namespace\]/ ' ,
141
176
'/\[Module\]/ ' ,
142
177
'/\[module\]/ ' ,
178
+ '/\[PModule\]/ ' ,
143
179
);
144
180
145
181
$ replace = array (
146
182
$ capNamespace ,
147
183
$ lowNamespace ,
148
184
$ capModule ,
149
185
$ lowModule ,
186
+ $ parentCapModule ,
150
187
);
151
188
152
189
return preg_replace ($ search , $ replace , $ content );
@@ -178,7 +215,7 @@ public function replacePhp($data,$content){
178
215
return preg_replace ($ search , $ replace , $ content );
179
216
}
180
217
181
- public function insertVarsInNewCustomModule ($ data ,$ toFilesPathArray ){
218
+ public function insertVarsInNewCustomModule ($ parentModule , $ data ,$ toFilesPathArray ){
182
219
try {
183
220
foreach ($ toFilesPathArray as $ toFilePath ):
184
221
$ handle = fopen ($ toFilePath , 'r+ ' );
@@ -191,7 +228,9 @@ public function insertVarsInNewCustomModule($data,$toFilesPathArray){
191
228
$ type = strrchr ($ toFilePath , '. ' );
192
229
switch ($ type ) {
193
230
case '.xml ' :
194
- $ content = $ this ->replaceXml ($ data ,$ content );
231
+ case '.a2bizz ' :
232
+ case '.parent ' :
233
+ $ content = $ this ->replaceXml ($ parentModule ,$ data ,$ content );
195
234
break ;
196
235
case '.php ' :
197
236
case '.phtml ' :
@@ -214,19 +253,44 @@ public function insertVarsInNewCustomModule($data,$toFilesPathArray){
214
253
return true ;
215
254
}
216
255
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
+
217
275
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
+
218
282
// 1- get File paths from where we are copying files for creating custom Module
219
283
$ fromFiles = $ this ->getAllFilesPath ();
220
284
221
285
// 2- convert path - Where you want to put new custom module
222
286
// 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 );
224
288
225
289
// 4- this function will create folders, on given path for new custom module
226
290
// 5- also it will paste blanko files - files which we will use to creat custom module
227
291
$ this ->createFoldersAndCopyBlankoFiles ($ fromFiles ,$ toFiles );
228
292
229
293
// 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 );
231
295
}
232
296
}
0 commit comments