|
| 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 | +} |
0 commit comments