3
3
* Webkul Software.
4
4
*
5
5
* @package Webkul_CodeGenerator
6
- * @author Ashutosh Srivastva
6
+ * @author Webkul Software Pvt Ltd
7
7
*/
8
8
9
9
namespace Webkul \CodeGenerator \Model \Generate ;
17
17
use Laminas \Code \Generator \ParameterGenerator ;
18
18
use Magento \Framework \Setup \Declaration \Schema \Declaration \ReaderComposite ;
19
19
use Webkul \CodeGenerator \Model \Helper ;
20
+ use Magento \Framework \Simplexml \Config ;
21
+ use Webkul \CodeGenerator \Model \XmlGeneratorFactory ;
20
22
21
23
/**
22
24
* Generate Model.php
@@ -38,10 +40,12 @@ class Model implements GenerateInterface
38
40
* __construct function
39
41
*
40
42
* @param ReaderComposite $readerComposite
43
+ * @param XmlGeneratorFactory $xmlGenerator
41
44
* @param Helper $helper
42
45
*/
43
46
public function __construct (
44
47
ReaderComposite $ readerComposite ,
48
+ protected XmlGeneratorFactory $ xmlGenerator ,
45
49
Helper $ helper
46
50
) {
47
51
$ this ->readerComposite = $ readerComposite ;
@@ -89,10 +93,15 @@ public function execute($data)
89
93
$ apiDataDirPath = $ apiDataDirPath .DIRECTORY_SEPARATOR .'Data '
90
94
);
91
95
96
+ $ this ->helper ->createDirectory (
97
+ $ etcDirPath = $ path .DIRECTORY_SEPARATOR .'etc '
98
+ );
99
+
92
100
$ this ->createApiClass ($ apiDataDirPath , $ data , $ columns );
93
101
$ this ->createModelClass ($ modelDirPath , $ data , $ columns );
94
102
$ this ->createResourceModelClass ($ rModelDirPath , $ data , $ identityColumn );
95
103
$ this ->createCollectionClass ($ collectionDirPath , $ data , $ identityColumn );
104
+ $ this ->addDiXmlData ($ etcDirPath , $ data );
96
105
97
106
return ['status ' => 'success ' , 'message ' => "model generated successfully " ];
98
107
}
@@ -144,7 +153,7 @@ public function createApiClass($dir, $data, $columns)
144
153
$ column ['type ' ]
145
154
)]),
146
155
new Tag \ReturnTag ([
147
- 'datatype ' => $ nameSpace .'\\' .$ data ['name ' ].'Interface ' ,
156
+ 'datatype ' => '\\' . $ nameSpace .'\\' .$ data ['name ' ].'Interface ' ,
148
157
]),
149
158
],
150
159
]),
@@ -205,8 +214,8 @@ public function createModelClass($dir, $data, $columns)
205
214
{
206
215
$ moduleNamespace = explode ('_ ' , $ data ['module ' ]);
207
216
$ nameSpace = $ moduleNamespace [0 ].'\\' .$ moduleNamespace [1 ].'\\Model ' ;
208
- $ parentClass = "Magento \\ Framework \\Model \\AbstractModel " ;
209
- $ parentInterface = "Magento \\ Framework \\DataObject \\IdentityInterface " ;
217
+ $ parentClass = "Magento " . '\\' . " Framework \\Model \\AbstractModel " ;
218
+ $ parentInterface = "Magento " . '\\' . " Framework \\DataObject \\IdentityInterface " ;
210
219
$ apiInterface = $ moduleNamespace [0 ].'\\' .$ moduleNamespace [1 ].'\\Api \\Data \\' .$ data ['name ' ].'Interface ' ;
211
220
$ resourceClass = '\\' .$ nameSpace .'\\ResourceModel \\' .$ data ['name ' ];
212
221
$ modelClass = new ClassGenerator ();
@@ -229,7 +238,8 @@ public function createModelClass($dir, $data, $columns)
229
238
// MethodGenerator::fromArray([
230
239
// 'name' => 'load',
231
240
// 'parameters' => ['id', 'field'],
232
- // 'body' => 'if ($id === null) {'. "\n". 'return $this->noRouteReasons();'. "\n". '}'. "\n". 'return parent::load($id, $field);',
241
+ // 'body' => 'if ($id === null) {'. "\n". 'return $this->noRouteReasons();'. "\n". '}'.
242
+ // "\n". 'return parent::load($id, $field);',
233
243
// 'docblock' => DocBlockGenerator::fromArray([
234
244
// 'shortDescription' => 'load model',
235
245
// 'longDescription' => "",
@@ -299,7 +309,7 @@ public function createModelClass($dir, $data, $columns)
299
309
'tags ' => [
300
310
new Tag \ParamTag ($ camelCase , [$ this ->helper ->getReturnType ($ column ['type ' ])]),
301
311
new Tag \ReturnTag ([
302
- 'datatype ' => $ nameSpace .'\\' .$ data ['name ' ].'Interface ' ,
312
+ 'datatype ' => '\\' . $ nameSpace .'\\' .$ data ['name ' ].'Interface ' ,
303
313
]),
304
314
],
305
315
]),
@@ -466,4 +476,29 @@ public function createCollectionClass($collectionDirPath, $data, $identityColumn
466
476
$ file ->generate ()
467
477
);
468
478
}
479
+
480
+ /**
481
+ * Add di xml data
482
+ *
483
+ * @param string $etcDirPath
484
+ * @param array $data
485
+ * @return void
486
+ */
487
+ public function addDiXmlData ($ etcDirPath , $ data )
488
+ {
489
+ $ moduleName = $ data ['module ' ];
490
+ $ data ['model-class ' ] = str_replace ('_ ' , '\\' , $ moduleName ).'\\' .'Model ' .'\\' .$ data ['name ' ];
491
+ $ data ['api-class ' ] = str_replace ('_ ' , '\\' , $ moduleName ).'\\' .'Api ' .'\\' .$ data ['name ' ].'Interface ' ;
492
+ $ diXmlFile = $ this ->helper ->getDiXmlFile ($ etcDirPath , $ data );
493
+ $ xmlObj = new Config ($ diXmlFile );
494
+ $ diXml = $ xmlObj ->getNode ();
495
+ $ typeNode = $ this ->xmlGenerator ->create ()->addXmlNode (
496
+ $ diXml ,
497
+ 'preference ' ,
498
+ '' ,
499
+ ['for ' => $ data ['api-class ' ], 'type ' => $ data ['model-class ' ]]
500
+ );
501
+ $ xmlData = $ this ->xmlGenerator ->create ()->formatXml ($ diXml ->asXml ());
502
+ $ this ->helper ->saveFile ($ diXmlFile , $ xmlData );
503
+ }
469
504
}
0 commit comments