1
|
|
|
<?php |
2
|
|
|
namespace App\Gii\Generator\Module; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\gii\generators\module\Generator as BaseGenerator; |
6
|
|
|
use yii\gii\CodeFile; |
7
|
|
|
use yii\helpers\Html; |
8
|
|
|
use yii\helpers\StringHelper; |
9
|
|
|
|
10
|
|
|
class Generator extends BaseGenerator |
11
|
|
|
{ |
12
|
|
|
public $templates = [ |
13
|
|
|
'default' => '@resources/gii/generators/module/views' |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
public function getControllerNamespace() |
17
|
|
|
{ |
18
|
|
|
return substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\')) . '\Controller'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public $formView = '@resources/gii/generators/module/views/form.php'; |
22
|
|
|
|
23
|
|
|
public function formView() |
24
|
|
|
{ |
25
|
|
|
return $this->formView; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public $viewPath; |
29
|
|
|
|
30
|
|
|
public function generate() |
31
|
|
|
{ |
32
|
|
|
$files = []; |
33
|
|
|
$modulePath = $this->getModulePath(); |
34
|
|
|
$files[] = new CodeFile( |
35
|
|
|
$modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php', |
36
|
|
|
$this->render("module.php") |
37
|
|
|
); |
38
|
|
|
$files[] = new CodeFile( |
39
|
|
|
$modulePath . '/Controller/DefaultController.php', |
40
|
|
|
$this->render("controller.php") |
41
|
|
|
); |
42
|
|
|
$files[] = new CodeFile( |
43
|
|
|
Yii::getAlias($this->viewPath) . '/default/index.php', |
44
|
|
|
$this->render("view.php") |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
return $files; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function rules() |
51
|
|
|
{ |
52
|
|
|
return array_merge(parent::rules(), [ |
53
|
|
|
[['viewPath'], 'required'], |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function hints() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
'moduleClass' => 'The module class, e.g., <code>App\Http\Module\Admin\Module</code>', |
61
|
|
|
'moduleID' => 'The module ID, e.g., <code>admin</code>', |
62
|
|
|
'viewPath' => 'The views path, e.g., <code>@resources/modules/admin/views</code>', |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function successMessage() |
70
|
|
|
{ |
71
|
|
|
if (Yii::$app->hasModule($this->moduleID)) { |
72
|
|
|
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']); |
73
|
|
|
|
74
|
|
|
return "The module has been generated successfully. You may $link."; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$output = <<<EOD |
78
|
|
|
<p>The module has been generated successfully.</p> |
79
|
|
|
<p>To access the module, you need to add this to your application configuration:</p> |
80
|
|
|
EOD; |
81
|
|
|
$code = <<<EOD |
82
|
|
|
<?php |
83
|
|
|
...... |
84
|
|
|
'modules' => [ |
85
|
|
|
'{$this->moduleID}' => [ |
86
|
|
|
'class' => '{$this->moduleClass}', |
87
|
|
|
'viewPath' => '{$this->viewPath}', |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
...... |
91
|
|
|
EOD; |
92
|
|
|
|
93
|
|
|
return $output . '<pre>' . highlight_string($code, true) . '</pre>'; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|