Skip to content

Commit

Permalink
Merge pull request #26 from ashutoshwebkul/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Ashutosh Srivastva authored Jan 3, 2020
2 parents 1b895bf + 8b1fef8 commit 233eb02
Show file tree
Hide file tree
Showing 31 changed files with 1,317 additions and 11 deletions.
123 changes: 123 additions & 0 deletions Model/Generate/Command.php
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);
}
}
59 changes: 59 additions & 0 deletions Model/Generate/Command/Validator.php
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;
}
}
2 changes: 2 additions & 0 deletions Model/Generate/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function createAdminController($dir, $data)
{
$fileName = ucfirst($data['name']);
$nameSpace = $data['module'];
$resource = $data['resource'];
$pathParts = explode("Controller/", $data['path']);

$nameArray = explode("_", $nameSpace);
Expand All @@ -111,6 +112,7 @@ public function createAdminController($dir, $data)
$controllerFile = str_replace('%module_name%', $data['module'], $controllerFile);
$controllerFile = str_replace('%class_name%', $fileName, $controllerFile);
$controllerFile = str_replace('%namespace%', $nameSpace, $controllerFile);
$controllerFile = str_replace('%resource_name%', $resource, $controllerFile);
$this->helper->saveFile(
$dir.DIRECTORY_SEPARATOR.$fileName.'.php',
$controllerFile
Expand Down
3 changes: 3 additions & 0 deletions Model/Generate/Controller/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function validate($data)
$name = $data['name'];
$area = $data['area'] ?? null;
$path = $data['path'] ?? null;
$resource = $data['resource'] ?? null;
$response = [];
if ($module) {
$moduleManager = \Magento\Framework\App\ObjectManager::getInstance()
Expand All @@ -42,6 +43,8 @@ public function validate($data)
throw new \InvalidArgumentException(__("name is required"));
}

$response['resource'] = $resource;

$dir = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Module\Dir::class);

Expand Down
2 changes: 0 additions & 2 deletions Model/Generate/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
class Cron implements GenerateInterface
{
const CONFIGXML_NODE = '//group';

protected $helper;

protected $xmlGeneratorFactory;
Expand Down
2 changes: 1 addition & 1 deletion Model/Generate/Cron/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function validate($data)
if ($name) {
$response["name"] = $name;
} else {
throw new \InvalidArgumentException(__("Name is required"));
throw new \InvalidArgumentException(__("name is required"));
}

if (isset($data['schedule']) && $data['schedule']) {
Expand Down
109 changes: 109 additions & 0 deletions Model/Generate/Email.php
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);
}
}
Loading

0 comments on commit 233eb02

Please sign in to comment.