Skip to content

Commit 5697386

Browse files
author
Ashutosh Srivastva
authored
Merge pull request #1 from maheshWebkul721/mahesh
Create new module type added.
2 parents 6f1e7fd + fdbaa63 commit 5697386

File tree

7 files changed

+270
-0
lines changed

7 files changed

+270
-0
lines changed

Model/Generate/NewModule.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Webkul Software.
4+
*
5+
* @package Webkul_CodeGenerator
6+
* @author Mahesh Singh
7+
*/
8+
9+
namespace Webkul\CodeGenerator\Model\Generate\NewModule;
10+
11+
use Magento\Framework\Exception\LocalizedException;
12+
13+
class Validator implements \Webkul\CodeGenerator\Api\ValidatorInterface
14+
{
15+
private $validationRule = '/^[a-zA-Z]+[a-zA-Z0-9._]+$/';
16+
17+
public function validate($data)
18+
{
19+
$type = $data['type'];
20+
$module = $data['module'];
21+
$response = [];
22+
if (!$type) {
23+
throw new \InvalidArgumentException(__('define type of code to be generated "new-module"'));
24+
}
25+
if ($module) {
26+
$moduleManager = \Magento\Framework\App\ObjectManager::getInstance()
27+
->get(\Magento\Framework\Module\ModuleListInterface::class);
28+
$moduleData = $moduleManager->getOne($module);
29+
if ($moduleData) {
30+
throw new LocalizedException(
31+
__(
32+
'%1 Module already exists.',
33+
$module
34+
)
35+
);
36+
}
37+
$response["module"] = $module;
38+
$response["type"] = $type;
39+
40+
} else {
41+
throw new \InvalidArgumentException(__("module name not provided"));
42+
}
43+
$moduleNameSplit = explode('_', $module);
44+
if (!isset($moduleNameSplit[1])) {
45+
throw new \RuntimeException(
46+
__('Incorrect module name "%1", correct name ex: Webkul_Test', $module)
47+
);
48+
}
49+
50+
foreach ($moduleNameSplit as $part) {
51+
if (!preg_match($this->validationRule, $part)) {
52+
throw new \RuntimeException(
53+
__('Module vendor or name must be alphanumeric.')
54+
);
55+
}
56+
}
57+
58+
return $response;
59+
}
60+
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
``` php -f bin/magento setup:update ```
88

99
# Usage
10+
- To Create new Module
11+
12+
``` php bin/magento generate:code Module_Name –type=new-module ```
13+
14+
- To Generate code types
1015

1116
``` php bin/magento generate:code Module_Name --table="table_name" --type=code-type --name=ModelName ```
1217

etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<arguments>
2222
<argument name="validators" xsi:type="array">
2323
<item name="model" xsi:type="object">Webkul\CodeGenerator\Model\Generate\Model\Validator</item>
24+
<item name="new-module" xsi:type="object">Webkul\CodeGenerator\Model\Generate\NewModule\Validator</item>
2425
</argument>
2526
</arguments>
2627
</type>
@@ -29,6 +30,7 @@
2930
<arguments>
3031
<argument name="generators" xsi:type="array">
3132
<item name="model" xsi:type="object">Webkul\CodeGenerator\Model\Generate\Model</item>
33+
<item name="new-module" xsi:type="object">Webkul\CodeGenerator\Model\Generate\NewModule</item>
3234
</argument>
3335
</arguments>
3436
</type>

templates/composer.json.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "%vendor%/module-%composerName%",
3+
"description": "",
4+
"type": "magento2-module",
5+
"license": "proprietary",
6+
"authors": [
7+
{
8+
"email": "[email protected]",
9+
"name": "Mahesh Singh"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"%moduleName%": ""
19+
}
20+
}
21+
}

templates/module.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" ?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="%moduleName%" setup_version="1.0.0"/>
4+
</config>

templates/registration.php.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
\Magento\Framework\Component\ComponentRegistrar::register(
3+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
4+
'%moduleName%',
5+
__DIR__
6+
);

0 commit comments

Comments
 (0)