|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Webkul Software. |
| 4 | + * |
| 5 | + * @package Webkul_CodeGenerator |
| 6 | + * @author Mahesh Singh |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Webkul\CodeGenerator\Model\Generate; |
| 10 | + |
| 11 | +use Webkul\CodeGenerator\Api\GenerateInterface; |
| 12 | +use Zend\Code\Generator\ClassGenerator; |
| 13 | +use Zend\Code\Generator\DocBlockGenerator; |
| 14 | +use Zend\Code\Generator\DocBlock\Tag; |
| 15 | +use Zend\Code\Generator\MethodGenerator; |
| 16 | +use Zend\Code\Generator\PropertyGenerator; |
| 17 | +use Zend\Code\Generator\ParameterGenerator; |
| 18 | +use Webkul\CodeGenerator\Model\Helper; |
| 19 | +use Magento\Framework\Module\StatusFactory; |
| 20 | + |
| 21 | +/** |
| 22 | + * Class NewModule.php |
| 23 | + */ |
| 24 | +class NewModule implements GenerateInterface |
| 25 | +{ |
| 26 | + const MODULE_PATH = 'app/code/'; |
| 27 | + |
| 28 | + protected $readerComposite; |
| 29 | + |
| 30 | + protected $helper; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + Helper $helper, |
| 34 | + StatusFactory $moduleStatusFactory |
| 35 | + ) { |
| 36 | + $this->helper = $helper; |
| 37 | + $this->moduleStatus = $moduleStatusFactory->create(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public function execute($data) |
| 44 | + { |
| 45 | + $moduleName = $data['module']; |
| 46 | + $preparedModuleName = str_replace('_', '/', $moduleName); |
| 47 | + $moduleDir = $this->getModuleBasePath().'/'.$preparedModuleName; |
| 48 | + // @codingStandardsIgnoreStart |
| 49 | + if (!is_dir($moduleDir)) { |
| 50 | + mkdir($moduleDir, 0777, true); |
| 51 | + } |
| 52 | + // @codingStandardsIgnoreEnd |
| 53 | + $this->createModuleXmlFile($moduleDir, $moduleName); |
| 54 | + $this->createRegistrationFile($moduleDir, $moduleName); |
| 55 | + $this->createComposerFile($moduleDir, $moduleName); |
| 56 | + $this->moduleStatus->setIsEnabled(true, [$moduleName]); |
| 57 | + return ['status' => 'success', 'message' => "new module generated successfully"]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create module.xml |
| 62 | + * |
| 63 | + * @param string $moduleDir |
| 64 | + * @param string $moduleName |
| 65 | + * @return void |
| 66 | + * |
| 67 | + */ |
| 68 | + private function createModuleXmlFile($moduleDir, $moduleName) |
| 69 | + { |
| 70 | + $moduleXmlTemplate = $this->getModuleXmlTemplate(); |
| 71 | + $moduleXmlTemplate = str_replace('%moduleName%', $moduleName, $moduleXmlTemplate); |
| 72 | + $moduleEtcDir = $moduleDir.'/etc'; |
| 73 | + // @codingStandardsIgnoreStart |
| 74 | + if (!is_dir($moduleEtcDir)) { |
| 75 | + mkdir($moduleEtcDir, 0777, true); |
| 76 | + } |
| 77 | + $moduleXmlFile = $moduleEtcDir . '/module.xml'; |
| 78 | + file_put_contents($moduleXmlFile, $moduleXmlTemplate); |
| 79 | + // @codingStandardsIgnoreEnd |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create registration.php |
| 84 | + * |
| 85 | + * @param string $moduleDir |
| 86 | + * @param string $moduleName |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + private function createRegistrationFile($moduleDir, $moduleName) |
| 90 | + { |
| 91 | + $registrationTemplate = $this->getRegistrationTemplate(); |
| 92 | + $registrationTemplate = str_replace('%moduleName%', $moduleName, $registrationTemplate); |
| 93 | + $registrationFile = $moduleDir . '/registration.php'; |
| 94 | + // @codingStandardsIgnoreStart |
| 95 | + file_put_contents($registrationFile, $registrationTemplate); |
| 96 | + // @codingStandardsIgnoreEnd |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Create composer.json |
| 101 | + * |
| 102 | + * @param string $moduleDir |
| 103 | + * @param string $moduleName |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + private function createComposerFile($moduleDir, $moduleName) |
| 107 | + { |
| 108 | + $composerModuleName = explode('_', $moduleName); |
| 109 | + $moduleComposerTemplate = $this->getModuleComposerTemplate(); |
| 110 | + $moduleComposerTemplate = str_replace('%moduleName%', $moduleName, $moduleComposerTemplate); |
| 111 | + $moduleComposerTemplate = str_replace( |
| 112 | + '%vendor%', |
| 113 | + strtolower($composerModuleName[0]), |
| 114 | + $moduleComposerTemplate |
| 115 | + ); |
| 116 | + $moduleComposerTemplate = str_replace( |
| 117 | + '%composerName%', |
| 118 | + strtolower($composerModuleName[1]), |
| 119 | + $moduleComposerTemplate |
| 120 | + ); |
| 121 | + $composerFile = $moduleDir . '/composer.json'; |
| 122 | + // @codingStandardsIgnoreStart |
| 123 | + file_put_contents($composerFile, $moduleComposerTemplate); |
| 124 | + // @codingStandardsIgnoreEnd |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Return base path |
| 129 | + * |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + public function getModuleBasePath() : string |
| 133 | + { |
| 134 | + return BP.'/'.self::MODULE_PATH; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * get module.xml template |
| 139 | + * |
| 140 | + * @return string |
| 141 | + */ |
| 142 | + protected function getModuleXmlTemplate() : string |
| 143 | + { |
| 144 | + // @codingStandardsIgnoreStart |
| 145 | + return file_get_contents(dirname(dirname( dirname(__FILE__) )) . '/templates/module.xml.dist'); |
| 146 | + // @codingStandardsIgnoreEnd |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * get registration.php template |
| 151 | + * |
| 152 | + * @return string |
| 153 | + */ |
| 154 | + protected function getRegistrationTemplate() : string |
| 155 | + { |
| 156 | + // @codingStandardsIgnoreStart |
| 157 | + return file_get_contents(dirname(dirname( dirname(__FILE__) )) . '/templates/registration.php.dist'); |
| 158 | + // @codingStandardsIgnoreEnd |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * get registration.php template |
| 163 | + * |
| 164 | + * @return string |
| 165 | + */ |
| 166 | + protected function getModuleComposerTemplate() : string |
| 167 | + { |
| 168 | + // @codingStandardsIgnoreStart |
| 169 | + return file_get_contents(dirname(dirname( dirname(__FILE__) )) . '/templates/composer.json.dist'); |
| 170 | + // @codingStandardsIgnoreEnd |
| 171 | + } |
| 172 | +} |
0 commit comments