Skip to content

Commit b88be8e

Browse files
authored
Merge pull request #46 from webkul/dev
Dev
2 parents da33720 + 5a87f2a commit b88be8e

14 files changed

+78
-30
lines changed

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-2
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;
@@ -859,7 +859,10 @@ public function addFormField($input, $output, $questionHelper)
859859
}
860860

861861
if (!$input->getOption('field_type')) {
862-
$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+
);
863866
$this->addNotEmptyValidator($question);
864867
$input->setOption(
865868
"field_type",

Model/Generate/Model.php

+39-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;
@@ -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
}
@@ -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' => "",
@@ -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/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/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) {

Model/OptionsPool.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;

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "webkul/code-generator",
33
"description": "module lets you generate models, controller, helpers in seconds so that you can focus more on application logic ",
44
"type": "magento2-module",
5-
"version": "2.0.7",
5+
"version": "2.0.8",
66
"license": [
77
"proprietary"
88
],

etc/di.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Webkul Software.
55
*
66
* @package Webkul_CodeGenerator
7-
* @author Ashutosh Srivastva
7+
* @author Webkul Software Pvt Ltd
88
*/
99
-->
1010
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Webkul Software.
55
*
66
* @package Webkul_CodeGenerator
7-
* @author Ashutosh Srivastva
7+
* @author Webkul Software Pvt Ltd
88
*/ -->
99
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
1010
<module name="Webkul_CodeGenerator">

registration.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
\Magento\Framework\Component\ComponentRegistrar::register(

0 commit comments

Comments
 (0)