Skip to content

Commit 5a87f2a

Browse files
authored
Merge pull request #44 from SauravKumar-Webkul/master
Master Dev Merge
2 parents 566d31e + 3784b19 commit 5a87f2a

24 files changed

+89
-92
lines changed

.vscode/launch.json

-48
This file was deleted.

Api/GenerateInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Api;

Api/ValidatorInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Api;

Console/Command/Generate.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Console\Command;
@@ -52,8 +52,6 @@ public function __construct(
5252
$this->validators = $validators;
5353
$this->optionsPool = \Magento\Framework\App\ObjectManager::getInstance()
5454
->get(\Webkul\CodeGenerator\Model\OptionsPool::class);
55-
$state = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\State::class);
56-
$state->setAreaCode("adminhtml");
5755
parent::__construct();
5856
}
5957

@@ -861,7 +859,10 @@ public function addFormField($input, $output, $questionHelper)
861859
}
862860

863861
if (!$input->getOption('field_type')) {
864-
$question = new Question('<question>Enter Field Type (input/select/multiselect/imageUploader):</question> ', 'input');
862+
$question = new Question(
863+
'<question>Enter Field Type (input/select/multiselect/imageUploader):</question> ',
864+
'input'
865+
);
865866
$this->addNotEmptyValidator($question);
866867
$input->setOption(
867868
"field_type",

Model/Generate/Model.php

+41-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate;
@@ -17,6 +17,8 @@
1717
use Laminas\Code\Generator\ParameterGenerator;
1818
use Magento\Framework\Setup\Declaration\Schema\Declaration\ReaderComposite;
1919
use Webkul\CodeGenerator\Model\Helper;
20+
use Magento\Framework\Simplexml\Config;
21+
use Webkul\CodeGenerator\Model\XmlGeneratorFactory;
2022

2123
/**
2224
* Generate Model.php
@@ -38,10 +40,12 @@ class Model implements GenerateInterface
3840
* __construct function
3941
*
4042
* @param ReaderComposite $readerComposite
43+
* @param XmlGeneratorFactory $xmlGenerator
4144
* @param Helper $helper
4245
*/
4346
public function __construct(
4447
ReaderComposite $readerComposite,
48+
protected XmlGeneratorFactory $xmlGenerator,
4549
Helper $helper
4650
) {
4751
$this->readerComposite = $readerComposite;
@@ -89,10 +93,15 @@ public function execute($data)
8993
$apiDataDirPath = $apiDataDirPath.DIRECTORY_SEPARATOR.'Data'
9094
);
9195

96+
$this->helper->createDirectory(
97+
$etcDirPath = $path.DIRECTORY_SEPARATOR.'etc'
98+
);
99+
92100
$this->createApiClass($apiDataDirPath, $data, $columns);
93101
$this->createModelClass($modelDirPath, $data, $columns);
94102
$this->createResourceModelClass($rModelDirPath, $data, $identityColumn);
95103
$this->createCollectionClass($collectionDirPath, $data, $identityColumn);
104+
$this->addDiXmlData($etcDirPath, $data);
96105

97106
return ['status' => 'success', 'message' => "model generated successfully"];
98107
}
@@ -144,7 +153,7 @@ public function createApiClass($dir, $data, $columns)
144153
$column['type']
145154
)]),
146155
new Tag\ReturnTag([
147-
'datatype' => $nameSpace.'\\'.$data['name'].'Interface',
156+
'datatype' => '\\'.$nameSpace.'\\'.$data['name'].'Interface',
148157
]),
149158
],
150159
]),
@@ -205,8 +214,8 @@ public function createModelClass($dir, $data, $columns)
205214
{
206215
$moduleNamespace = explode('_', $data['module']);
207216
$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";
210219
$apiInterface = $moduleNamespace[0].'\\'.$moduleNamespace[1].'\\Api\\Data\\'.$data['name'].'Interface';
211220
$resourceClass = '\\'.$nameSpace.'\\ResourceModel\\'.$data['name'];
212221
$modelClass = new ClassGenerator();
@@ -229,7 +238,8 @@ public function createModelClass($dir, $data, $columns)
229238
// MethodGenerator::fromArray([
230239
// 'name' => 'load',
231240
// '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);',
233243
// 'docblock' => DocBlockGenerator::fromArray([
234244
// 'shortDescription' => 'load model',
235245
// 'longDescription' => "",
@@ -299,7 +309,7 @@ public function createModelClass($dir, $data, $columns)
299309
'tags' => [
300310
new Tag\ParamTag($camelCase, [$this->helper->getReturnType($column['type'])]),
301311
new Tag\ReturnTag([
302-
'datatype' => $nameSpace.'\\'.$data['name'].'Interface',
312+
'datatype' => '\\'.$nameSpace.'\\'.$data['name'].'Interface',
303313
]),
304314
],
305315
]),
@@ -466,4 +476,29 @@ public function createCollectionClass($collectionDirPath, $data, $identityColumn
466476
$file->generate()
467477
);
468478
}
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+
}
469504
}

Model/Generate/Model/Validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate\Model;

Model/Generate/NewModule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Mahesh Singh
6+
* @author Webkul
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate;

Model/Generate/NewModule/Validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Mahesh Singh
6+
* @author Webkul
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate\NewModule;

Model/Generate/Repository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate;

Model/Generate/Repository/Validator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate\Repository;
@@ -21,9 +21,9 @@ public function validate($data)
2121
$module = $data['module'];
2222
$type = $data['type'];
2323
$name = $data['name'];
24-
$path = $data['path']??null;
25-
$modelClass = $data['model-class']??null;
26-
$collectionClass = $data['collection-class']??null;
24+
$path = $data['path'] ?? null;
25+
$modelClass = $data['model-class'] ?? null;
26+
$collectionClass = $data['collection-class'] ?? null;
2727
$response = [];
2828
$response['type'] = $type;
2929
if ($module) {

Model/Generate/Shipping.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Mahesh Singh
6+
* @author Webkul
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate;

Model/Generate/Shipping/Validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Mahesh Singh
6+
* @author Webkul
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate\Shipping;

Model/Generate/View.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Mahesh Singh
6+
* @author Webkul
77
*/
88

99
namespace Webkul\CodeGenerator\Model\Generate;

Model/Generate/View/Validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Mahesh Singh
6+
* @author Webkul
77
*/
88
namespace Webkul\CodeGenerator\Model\Generate\View;
99

Model/GeneratorPool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Model;

Model/Helper.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Webkul Software.
44
*
55
* @package Webkul_CodeGenerator
6-
* @author Ashutosh Srivastva
6+
* @author Webkul Software Pvt Ltd
77
*/
88

99
namespace Webkul\CodeGenerator\Model;
@@ -15,6 +15,16 @@
1515

1616
class Helper
1717
{
18+
/**
19+
* __construct function
20+
*
21+
* @param \Magento\Framework\Filesystem\Driver\File $driverFile
22+
*/
23+
public function __construct(
24+
protected \Magento\Framework\Filesystem\Driver\File $driverFile
25+
) {
26+
}
27+
1828
/**
1929
* Save File
2030
*
@@ -24,10 +34,7 @@ class Helper
2434
*/
2535
public function saveFile($path, $content)
2636
{
27-
file_put_contents(
28-
$path,
29-
$content
30-
);
37+
$this->driverFile->filePutContents($path, $content);
3138
}
3239

3340
/**
@@ -46,7 +53,6 @@ public function getHeadDocBlock($moduleName)
4653
new Tag\GenericTag('author', 'Webkul'),
4754
new Tag\GenericTag('copyright', 'Copyright (c) Webkul Software Private Limited (https://webkul.com)'),
4855
new Tag\LicenseTag('https://store.webkul.com/license.html', '')
49-
5056
],
5157
]);
5258
}
@@ -80,8 +86,8 @@ public function getReturnType($type = 'default')
8086
*/
8187
public function createDirectory($dirPath, $permission = 0777)
8288
{
83-
if (!is_dir($dirPath)) {
84-
mkdir($dirPath, $permission, true);
89+
if (!$this->driverFile->isDirectory($dirPath)) {
90+
$this->driverFile->createDirectory($dirPath, $permission);
8591
}
8692
}
8793

@@ -93,7 +99,11 @@ public function createDirectory($dirPath, $permission = 0777)
9399
*/
94100
public function getTemplatesFiles($template)
95101
{
96-
return file_get_contents(dirname(dirname(__FILE__)). DIRECTORY_SEPARATOR. $template);
102+
return $this->driverFile->fileGetContents(
103+
$this->driverFile->getParentDirectory(
104+
$this->driverFile->getParentDirectory(__FILE__)
105+
). DIRECTORY_SEPARATOR. $template
106+
);
97107
}
98108

99109
/**
@@ -108,7 +118,7 @@ public function getTemplatesFiles($template)
108118
public function loadTemplateFile($path, $fileName, $templatePath, $replace = [])
109119
{
110120
$filePath = $path.DIRECTORY_SEPARATOR.$fileName;
111-
if (!file_exists($filePath)) {
121+
if (!$this->driverFile->isExists($filePath)) {
112122
$data = $this->getTemplatesFiles($templatePath);
113123
if (!empty($replace) && is_array($replace)) {
114124
foreach ($replace as $find => $value) {

0 commit comments

Comments
 (0)