-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ashutoshwebkul/dev
Dev
- Loading branch information
Showing
31 changed files
with
1,317 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
/** | ||
* Webkul Software. | ||
* | ||
* @package Webkul_CodeGenerator | ||
* @author Sanjay Chouhan | ||
*/ | ||
|
||
namespace Webkul\CodeGenerator\Model\Generate; | ||
|
||
use Webkul\CodeGenerator\Model\Helper; | ||
use Webkul\CodeGenerator\Api\GenerateInterface; | ||
use Webkul\CodeGenerator\Model\XmlGeneratorFactory; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
use Magento\Framework\Simplexml\Config; | ||
use Magento\Framework\Simplexml\Element; | ||
|
||
/** | ||
* Class Command | ||
*/ | ||
class Command implements GenerateInterface | ||
{ | ||
protected $helper; | ||
|
||
protected $xmlGeneratorFactory; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param XmlGeneratorFactory $xmlGeneratorFactory | ||
* @param Helper $helper | ||
*/ | ||
public function __construct( | ||
XmlGeneratorFactory $xmlGeneratorFactory, | ||
Helper $helper | ||
) { | ||
$this->helper = $helper; | ||
$this->xmlGenerator = $xmlGeneratorFactory->create(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute($data) | ||
{ | ||
$moduleName = $data['module']; | ||
$path = $data['path']; | ||
|
||
Helper::createDirectory( | ||
$commandDirPath = $path.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Command' | ||
); | ||
|
||
Helper::createDirectory( | ||
$etcDirPath = $path.DIRECTORY_SEPARATOR.'etc' | ||
); | ||
|
||
$this->createCommand($commandDirPath, $data); | ||
$data['command-class'] = str_replace('_', '\\', $moduleName).'\\Console\\Command\\'.ucfirst($data['name']); | ||
$this->addDiXmlData($etcDirPath, $data); | ||
|
||
return ['status' => 'success', 'message' => "Command Generated Successfully"]; | ||
} | ||
|
||
/** | ||
* create Command class | ||
* | ||
* @param string $dir | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function createCommand($dir, $data) | ||
{ | ||
$fileName = ucfirst($data['name']); | ||
$nameSpace = $data['module']; | ||
$nameArray = explode("_", $nameSpace); | ||
$commandFile = $this->helper->getTemplatesFiles('templates/command/command.php.dist'); | ||
$commandFile = str_replace('%module_name%', $data['module'], $commandFile); | ||
$commandFile = str_replace('%name%', $fileName, $commandFile); | ||
$commandFile = str_replace('%command%', $data['command'], $commandFile); | ||
$commandFile = str_replace('%namespace%', $nameArray[0].'\\'.$nameArray[1], $commandFile); | ||
|
||
$this->helper->saveFile( | ||
$dir.DIRECTORY_SEPARATOR.$fileName.'.php', | ||
$commandFile | ||
); | ||
} | ||
|
||
/** | ||
* add di xml data | ||
* | ||
* @param string $etcDirPath | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function addDiXmlData($etcDirPath, $data) | ||
{ | ||
$commandName = str_replace(':', '', $data['command']); | ||
$diXmlFile = $this->helper->getDiXmlFile($etcDirPath); | ||
$xmlObj = new Config($diXmlFile); | ||
$diXml = $xmlObj->getNode(); | ||
$typeNode = $this->xmlGenerator->addXmlNode( | ||
$diXml, | ||
'type', | ||
'', | ||
['name'=>'Magento\Framework\Console\CommandList'] | ||
); | ||
$argsNode = $this->xmlGenerator->addXmlNode($typeNode, 'arguments'); | ||
$argNode = $this->xmlGenerator->addXmlNode( | ||
$argsNode, | ||
'argument', | ||
'', | ||
['name'=>'commands', 'xsi:type'=>'array'] | ||
); | ||
$this->xmlGenerator->addXmlNode( | ||
$argNode, | ||
'item', | ||
$data['command-class'], | ||
['name'=>$commandName, 'xsi:type'=>'object'] | ||
); | ||
$xmlData = $this->xmlGenerator->formatXml($diXml->asXml()); | ||
$this->helper->saveFile($diXmlFile, $xmlData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Webkul Software. | ||
* | ||
* @package Webkul_CodeGenerator | ||
* @author Sanjay Chouhan | ||
*/ | ||
|
||
namespace Webkul\CodeGenerator\Model\Generate\Command; | ||
|
||
class Validator implements \Webkul\CodeGenerator\Api\ValidatorInterface | ||
{ | ||
/** | ||
* Validate command params | ||
* | ||
* @param array $data | ||
* @return array | ||
*/ | ||
public function validate($data) | ||
{ | ||
$module = $data['module']; | ||
$type = $data['type']; | ||
$name = $data['name']; | ||
|
||
$response = []; | ||
if ($module) { | ||
$moduleManager = \Magento\Framework\App\ObjectManager::getInstance() | ||
->get(\Magento\Framework\Module\ModuleListInterface::class); | ||
$moduleData = $moduleManager->getOne($module); | ||
if (!$moduleData) { | ||
throw new \InvalidArgumentException(__("Invalid module name")); | ||
} | ||
$response["module"] = $module; | ||
} else { | ||
throw new \InvalidArgumentException(__("Module name not provided")); | ||
} | ||
|
||
if ($name) { | ||
$response["name"] = $name; | ||
} else { | ||
throw new \InvalidArgumentException(__("name is required")); | ||
} | ||
|
||
if (isset($data['command']) && $data['command']) { | ||
$response["command"] = $data['command']; | ||
} else { | ||
throw new \InvalidArgumentException(__("command is required")); | ||
} | ||
|
||
$dir = \Magento\Framework\App\ObjectManager::getInstance() | ||
->get(\Magento\Framework\Module\Dir::class); | ||
|
||
$modulePath = $dir->getDir($module); | ||
$response["path"] = $modulePath; | ||
$response["type"] = $type; | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
/** | ||
* Webkul Software. | ||
* | ||
* @package Webkul_CodeGenerator | ||
* @author Sanjay Chouhan | ||
*/ | ||
|
||
namespace Webkul\CodeGenerator\Model\Generate; | ||
|
||
use Webkul\CodeGenerator\Model\Helper; | ||
use Webkul\CodeGenerator\Api\GenerateInterface; | ||
use Webkul\CodeGenerator\Model\XmlGeneratorFactory; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
use Magento\Framework\Simplexml\Config; | ||
use Magento\Framework\Simplexml\Element; | ||
|
||
/** | ||
* Class Email | ||
*/ | ||
class Email implements GenerateInterface | ||
{ | ||
protected $helper; | ||
|
||
protected $xmlGeneratorFactory; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param XmlGeneratorFactory $xmlGeneratorFactory | ||
* @param Helper $helper | ||
*/ | ||
public function __construct( | ||
XmlGeneratorFactory $xmlGeneratorFactory, | ||
Helper $helper | ||
) { | ||
$this->helper = $helper; | ||
$this->xmlGenerator = $xmlGeneratorFactory->create(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute($data) | ||
{ | ||
$moduleName = $data['module']; | ||
$path = $data['path']; | ||
|
||
Helper::createDirectory( | ||
$emailDirPath = $path.DIRECTORY_SEPARATOR.'view/frontend/email' | ||
); | ||
|
||
Helper::createDirectory( | ||
$etcDirPath = $path.DIRECTORY_SEPARATOR.'etc' | ||
); | ||
|
||
$this->createEmailTemplate($emailDirPath, $data); | ||
$this->addEmailXmlData($etcDirPath, $data); | ||
|
||
return ['status' => 'success', 'message' => "Email Template Generated Successfully"]; | ||
} | ||
|
||
/** | ||
* create email template | ||
* | ||
* @param string $dir | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function createEmailTemplate($dir, $data) | ||
{ | ||
$emailFile = $this->helper->getTemplatesFiles('templates/email/email.html.dist'); | ||
$emailFile = str_replace('%module_name%', $data['module'], $emailFile); | ||
|
||
$this->helper->saveFile( | ||
$dir.DIRECTORY_SEPARATOR.$data['template'].'.html', | ||
$emailFile | ||
); | ||
} | ||
|
||
/** | ||
* add email_templates.xml data | ||
* | ||
* @param string $etcDirPath | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function addEmailXmlData($etcDirPath, $data) | ||
{ | ||
$emailXmlFile = $this->helper->loadTemplateFile($etcDirPath, 'email_templates.xml', 'templates/email/email_templates.xml.dist'); | ||
$xmlObj = new Config($emailXmlFile); | ||
$configXml = $xmlObj->getNode(); | ||
$this->xmlGenerator->addXmlNode( | ||
$configXml, | ||
'template', | ||
'', | ||
[ | ||
'id'=>$data['id'], | ||
'label'=>$data['name'], | ||
'file'=>$data['template'].'.html', | ||
'type'=>'html', | ||
'area'=>'frontend', | ||
'module'=>$data['module'] | ||
] | ||
); | ||
$xmlData = $this->xmlGenerator->formatXml($configXml->asXml()); | ||
$this->helper->saveFile($emailXmlFile, $xmlData); | ||
} | ||
} |
Oops, something went wrong.