Skip to content

Commit 233eb02

Browse files
author
Ashutosh Srivastva
authored
Merge pull request #26 from ashutoshwebkul/dev
Dev
2 parents 1b895bf + 8b1fef8 commit 233eb02

31 files changed

+1317
-11
lines changed

Model/Generate/Command.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Webkul Software.
4+
*
5+
* @package Webkul_CodeGenerator
6+
* @author Sanjay Chouhan
7+
*/
8+
9+
namespace Webkul\CodeGenerator\Model\Generate;
10+
11+
use Webkul\CodeGenerator\Model\Helper;
12+
use Webkul\CodeGenerator\Api\GenerateInterface;
13+
use Webkul\CodeGenerator\Model\XmlGeneratorFactory;
14+
use Magento\Framework\Serialize\Serializer\Json;
15+
use Magento\Framework\Simplexml\Config;
16+
use Magento\Framework\Simplexml\Element;
17+
18+
/**
19+
* Class Command
20+
*/
21+
class Command implements GenerateInterface
22+
{
23+
protected $helper;
24+
25+
protected $xmlGeneratorFactory;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param XmlGeneratorFactory $xmlGeneratorFactory
31+
* @param Helper $helper
32+
*/
33+
public function __construct(
34+
XmlGeneratorFactory $xmlGeneratorFactory,
35+
Helper $helper
36+
) {
37+
$this->helper = $helper;
38+
$this->xmlGenerator = $xmlGeneratorFactory->create();
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function execute($data)
45+
{
46+
$moduleName = $data['module'];
47+
$path = $data['path'];
48+
49+
Helper::createDirectory(
50+
$commandDirPath = $path.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Command'
51+
);
52+
53+
Helper::createDirectory(
54+
$etcDirPath = $path.DIRECTORY_SEPARATOR.'etc'
55+
);
56+
57+
$this->createCommand($commandDirPath, $data);
58+
$data['command-class'] = str_replace('_', '\\', $moduleName).'\\Console\\Command\\'.ucfirst($data['name']);
59+
$this->addDiXmlData($etcDirPath, $data);
60+
61+
return ['status' => 'success', 'message' => "Command Generated Successfully"];
62+
}
63+
64+
/**
65+
* create Command class
66+
*
67+
* @param string $dir
68+
* @param array $data
69+
* @return void
70+
*/
71+
public function createCommand($dir, $data)
72+
{
73+
$fileName = ucfirst($data['name']);
74+
$nameSpace = $data['module'];
75+
$nameArray = explode("_", $nameSpace);
76+
$commandFile = $this->helper->getTemplatesFiles('templates/command/command.php.dist');
77+
$commandFile = str_replace('%module_name%', $data['module'], $commandFile);
78+
$commandFile = str_replace('%name%', $fileName, $commandFile);
79+
$commandFile = str_replace('%command%', $data['command'], $commandFile);
80+
$commandFile = str_replace('%namespace%', $nameArray[0].'\\'.$nameArray[1], $commandFile);
81+
82+
$this->helper->saveFile(
83+
$dir.DIRECTORY_SEPARATOR.$fileName.'.php',
84+
$commandFile
85+
);
86+
}
87+
88+
/**
89+
* add di xml data
90+
*
91+
* @param string $etcDirPath
92+
* @param array $data
93+
* @return void
94+
*/
95+
public function addDiXmlData($etcDirPath, $data)
96+
{
97+
$commandName = str_replace(':', '', $data['command']);
98+
$diXmlFile = $this->helper->getDiXmlFile($etcDirPath);
99+
$xmlObj = new Config($diXmlFile);
100+
$diXml = $xmlObj->getNode();
101+
$typeNode = $this->xmlGenerator->addXmlNode(
102+
$diXml,
103+
'type',
104+
'',
105+
['name'=>'Magento\Framework\Console\CommandList']
106+
);
107+
$argsNode = $this->xmlGenerator->addXmlNode($typeNode, 'arguments');
108+
$argNode = $this->xmlGenerator->addXmlNode(
109+
$argsNode,
110+
'argument',
111+
'',
112+
['name'=>'commands', 'xsi:type'=>'array']
113+
);
114+
$this->xmlGenerator->addXmlNode(
115+
$argNode,
116+
'item',
117+
$data['command-class'],
118+
['name'=>$commandName, 'xsi:type'=>'object']
119+
);
120+
$xmlData = $this->xmlGenerator->formatXml($diXml->asXml());
121+
$this->helper->saveFile($diXmlFile, $xmlData);
122+
}
123+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Webkul Software.
4+
*
5+
* @package Webkul_CodeGenerator
6+
* @author Sanjay Chouhan
7+
*/
8+
9+
namespace Webkul\CodeGenerator\Model\Generate\Command;
10+
11+
class Validator implements \Webkul\CodeGenerator\Api\ValidatorInterface
12+
{
13+
/**
14+
* Validate command params
15+
*
16+
* @param array $data
17+
* @return array
18+
*/
19+
public function validate($data)
20+
{
21+
$module = $data['module'];
22+
$type = $data['type'];
23+
$name = $data['name'];
24+
25+
$response = [];
26+
if ($module) {
27+
$moduleManager = \Magento\Framework\App\ObjectManager::getInstance()
28+
->get(\Magento\Framework\Module\ModuleListInterface::class);
29+
$moduleData = $moduleManager->getOne($module);
30+
if (!$moduleData) {
31+
throw new \InvalidArgumentException(__("Invalid module name"));
32+
}
33+
$response["module"] = $module;
34+
} else {
35+
throw new \InvalidArgumentException(__("Module name not provided"));
36+
}
37+
38+
if ($name) {
39+
$response["name"] = $name;
40+
} else {
41+
throw new \InvalidArgumentException(__("name is required"));
42+
}
43+
44+
if (isset($data['command']) && $data['command']) {
45+
$response["command"] = $data['command'];
46+
} else {
47+
throw new \InvalidArgumentException(__("command is required"));
48+
}
49+
50+
$dir = \Magento\Framework\App\ObjectManager::getInstance()
51+
->get(\Magento\Framework\Module\Dir::class);
52+
53+
$modulePath = $dir->getDir($module);
54+
$response["path"] = $modulePath;
55+
$response["type"] = $type;
56+
57+
return $response;
58+
}
59+
}

Model/Generate/Controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function createAdminController($dir, $data)
9999
{
100100
$fileName = ucfirst($data['name']);
101101
$nameSpace = $data['module'];
102+
$resource = $data['resource'];
102103
$pathParts = explode("Controller/", $data['path']);
103104

104105
$nameArray = explode("_", $nameSpace);
@@ -111,6 +112,7 @@ public function createAdminController($dir, $data)
111112
$controllerFile = str_replace('%module_name%', $data['module'], $controllerFile);
112113
$controllerFile = str_replace('%class_name%', $fileName, $controllerFile);
113114
$controllerFile = str_replace('%namespace%', $nameSpace, $controllerFile);
115+
$controllerFile = str_replace('%resource_name%', $resource, $controllerFile);
114116
$this->helper->saveFile(
115117
$dir.DIRECTORY_SEPARATOR.$fileName.'.php',
116118
$controllerFile

Model/Generate/Controller/Validator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function validate($data)
1717
$name = $data['name'];
1818
$area = $data['area'] ?? null;
1919
$path = $data['path'] ?? null;
20+
$resource = $data['resource'] ?? null;
2021
$response = [];
2122
if ($module) {
2223
$moduleManager = \Magento\Framework\App\ObjectManager::getInstance()
@@ -42,6 +43,8 @@ public function validate($data)
4243
throw new \InvalidArgumentException(__("name is required"));
4344
}
4445

46+
$response['resource'] = $resource;
47+
4548
$dir = \Magento\Framework\App\ObjectManager::getInstance()
4649
->get(\Magento\Framework\Module\Dir::class);
4750

Model/Generate/Cron.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
*/
2121
class Cron implements GenerateInterface
2222
{
23-
const CONFIGXML_NODE = '//group';
24-
2523
protected $helper;
2624

2725
protected $xmlGeneratorFactory;

Model/Generate/Cron/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function validate($data)
3838
if ($name) {
3939
$response["name"] = $name;
4040
} else {
41-
throw new \InvalidArgumentException(__("Name is required"));
41+
throw new \InvalidArgumentException(__("name is required"));
4242
}
4343

4444
if (isset($data['schedule']) && $data['schedule']) {

Model/Generate/Email.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Webkul Software.
4+
*
5+
* @package Webkul_CodeGenerator
6+
* @author Sanjay Chouhan
7+
*/
8+
9+
namespace Webkul\CodeGenerator\Model\Generate;
10+
11+
use Webkul\CodeGenerator\Model\Helper;
12+
use Webkul\CodeGenerator\Api\GenerateInterface;
13+
use Webkul\CodeGenerator\Model\XmlGeneratorFactory;
14+
use Magento\Framework\Serialize\Serializer\Json;
15+
use Magento\Framework\Simplexml\Config;
16+
use Magento\Framework\Simplexml\Element;
17+
18+
/**
19+
* Class Email
20+
*/
21+
class Email implements GenerateInterface
22+
{
23+
protected $helper;
24+
25+
protected $xmlGeneratorFactory;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param XmlGeneratorFactory $xmlGeneratorFactory
31+
* @param Helper $helper
32+
*/
33+
public function __construct(
34+
XmlGeneratorFactory $xmlGeneratorFactory,
35+
Helper $helper
36+
) {
37+
$this->helper = $helper;
38+
$this->xmlGenerator = $xmlGeneratorFactory->create();
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function execute($data)
45+
{
46+
$moduleName = $data['module'];
47+
$path = $data['path'];
48+
49+
Helper::createDirectory(
50+
$emailDirPath = $path.DIRECTORY_SEPARATOR.'view/frontend/email'
51+
);
52+
53+
Helper::createDirectory(
54+
$etcDirPath = $path.DIRECTORY_SEPARATOR.'etc'
55+
);
56+
57+
$this->createEmailTemplate($emailDirPath, $data);
58+
$this->addEmailXmlData($etcDirPath, $data);
59+
60+
return ['status' => 'success', 'message' => "Email Template Generated Successfully"];
61+
}
62+
63+
/**
64+
* create email template
65+
*
66+
* @param string $dir
67+
* @param array $data
68+
* @return void
69+
*/
70+
public function createEmailTemplate($dir, $data)
71+
{
72+
$emailFile = $this->helper->getTemplatesFiles('templates/email/email.html.dist');
73+
$emailFile = str_replace('%module_name%', $data['module'], $emailFile);
74+
75+
$this->helper->saveFile(
76+
$dir.DIRECTORY_SEPARATOR.$data['template'].'.html',
77+
$emailFile
78+
);
79+
}
80+
81+
/**
82+
* add email_templates.xml data
83+
*
84+
* @param string $etcDirPath
85+
* @param array $data
86+
* @return void
87+
*/
88+
public function addEmailXmlData($etcDirPath, $data)
89+
{
90+
$emailXmlFile = $this->helper->loadTemplateFile($etcDirPath, 'email_templates.xml', 'templates/email/email_templates.xml.dist');
91+
$xmlObj = new Config($emailXmlFile);
92+
$configXml = $xmlObj->getNode();
93+
$this->xmlGenerator->addXmlNode(
94+
$configXml,
95+
'template',
96+
'',
97+
[
98+
'id'=>$data['id'],
99+
'label'=>$data['name'],
100+
'file'=>$data['template'].'.html',
101+
'type'=>'html',
102+
'area'=>'frontend',
103+
'module'=>$data['module']
104+
]
105+
);
106+
$xmlData = $this->xmlGenerator->formatXml($configXml->asXml());
107+
$this->helper->saveFile($emailXmlFile, $xmlData);
108+
}
109+
}

0 commit comments

Comments
 (0)