|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\console\commands; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\helpers\Console; |
|
7
|
|
|
use luya\helpers\FileHelper; |
|
8
|
|
|
use yii\helpers\Inflector; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Command to create a new LUYA Module. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Basil Suter <[email protected]> |
|
14
|
|
|
* @since 1.0.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class ModuleController extends \luya\console\Command |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritdoc |
|
20
|
|
|
*/ |
|
21
|
|
|
public $defaultAction = 'create'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Humanize the class name |
|
25
|
|
|
* |
|
26
|
|
|
* @return string The humanized name. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function humanizeName($name) |
|
29
|
|
|
{ |
|
30
|
|
|
return $name = Inflector::humanize(Inflector::camel2words($name)); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Render the readme template. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $folders |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @param string $ns |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function renderReadme($folders, $name, $ns) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
return $this->view->render('@luya/console/commands/views/module/readme.php', [ |
|
44
|
|
|
'folders' => $folders, |
|
45
|
|
|
'name' => $name, |
|
46
|
|
|
'humanName' => $this->humanizeName($name), |
|
47
|
|
|
'ns' => $ns, |
|
48
|
|
|
'luyaText' => $this->getGeneratorText('module/create'), |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Render the admin template. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $folders |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @param string $ns |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function renderAdmin($folders, $name, $ns) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return $this->view->render('@luya/console/commands/views/module/adminmodule.php', [ |
|
63
|
|
|
'folders' => $folders, |
|
64
|
|
|
'name' => $this->humanizeName($name), |
|
65
|
|
|
'ns' => $ns, |
|
66
|
|
|
'luyaText' => $this->getGeneratorText('module/create'), |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Render the frontend template. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $folders |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @param string $ns |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
View Code Duplication |
public function renderFrontend($folders, $name, $ns) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
return $this->view->render('@luya/console/commands/views/module/frontendmodule.php', [ |
|
81
|
|
|
'folders' => $folders, |
|
82
|
|
|
'name' => $this->humanizeName($name), |
|
83
|
|
|
'ns' => $ns, |
|
84
|
|
|
'luyaText' => $this->getGeneratorText('module/create'), |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create a new frontend/admin module. |
|
90
|
|
|
* |
|
91
|
|
|
* @return number |
|
92
|
|
|
*/ |
|
93
|
|
|
public function actionCreate() |
|
94
|
|
|
{ |
|
95
|
|
|
Console::clearScreenBeforeCursor(); |
|
96
|
|
|
|
|
97
|
|
|
$moduleName = $this->prompt("Enter the name of the module you like to generate:"); |
|
98
|
|
|
|
|
99
|
|
|
$newName = preg_replace("/[^a-z]/", "", strtolower($moduleName)); |
|
100
|
|
|
|
|
101
|
|
|
if ($newName !== $moduleName) { |
|
102
|
|
|
if (!$this->confirm("We have changed the name to '{$newName}'. Do you want to proceed with this name?")) { |
|
|
|
|
|
|
103
|
|
|
return $this->outputError('Abort by user.'); |
|
104
|
|
|
} else { |
|
105
|
|
|
$moduleName = $newName; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$appModulesFolder = Yii::$app->basePath . DIRECTORY_SEPARATOR . 'modules'; |
|
110
|
|
|
$moduleFolder = $appModulesFolder . DIRECTORY_SEPARATOR . $moduleName; |
|
111
|
|
|
|
|
112
|
|
|
if (file_exists($moduleFolder)) { |
|
113
|
|
|
return $this->outputError("The folder " . $moduleFolder . " exists already."); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$folders = [ |
|
117
|
|
|
'basePath' => $moduleFolder, |
|
118
|
|
|
'adminPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'admin', |
|
119
|
|
|
'adminAwsPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'aws', |
|
120
|
|
|
'adminMigrationPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'migrations', |
|
121
|
|
|
'frontendPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend', |
|
122
|
|
|
'frontendBlocksPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'blocks', |
|
123
|
|
|
'frontendControllersPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'controllers', |
|
124
|
|
|
'frontendViewsPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'views', |
|
125
|
|
|
'modelsPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'models', |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
$ns = 'app\\modules\\'.$moduleName; |
|
129
|
|
|
|
|
130
|
|
|
foreach ($folders as $folder) { |
|
131
|
|
|
FileHelper::createDirectory($folder); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$contents = [ |
|
135
|
|
|
$moduleFolder. DIRECTORY_SEPARATOR . 'README.md' => $this->renderReadme($folders, $moduleName, $ns), |
|
136
|
|
|
$moduleFolder. DIRECTORY_SEPARATOR . 'admin/Module.php' => $this->renderAdmin($folders, $moduleName, $ns), |
|
137
|
|
|
$moduleFolder. DIRECTORY_SEPARATOR . 'frontend/Module.php' => $this->renderFrontend($folders, $moduleName, $ns), |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
foreach ($contents as $fileName => $content) { |
|
141
|
|
|
FileHelper::writeFile($fileName, $content); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->outputSuccess("Module files has been created successfully. Check the README file to understand how to add the module to your config."); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.