1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\console;
use Yii;
use yii\base\InlineAction;
use yii\console\Controller;
/**
* This is the command line version of Gii - a code generator.
*
* You can use this command to generate models, controllers, etc. For example,
* to generate an ActiveRecord model based on a DB table, you can run:
*
* ```
* $ ./yii gii/model --tableName=city --modelClass=City
* ```
*
* @author Tobias Munk <schmunk@usrbin.de>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class GenerateController extends Controller
{
/**
* @var \yii\gii\Module
*/
public $module;
/**
* @var boolean whether to overwrite all existing code files when in non-interactive mode.
* Defaults to false, meaning none of the existing code files will be overwritten.
* This option is used only when `--interactive=0`.
*/
public $overwrite = false;
/**
* @var array a list of the available code generators
*/
public $generators = [];
/**
* @var array generator option values
*/
private $_options = [];
/**
* @inheritdoc
*/
public function __get($name)
{
return isset($this->_options[$name]) ? $this->_options[$name] : null;
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
$this->_options[$name] = $value;
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
foreach ($this->generators as $id => $config) {
$this->generators[$id] = Yii::createObject($config);
}
}
/**
* @inheritdoc
*/
public function createAction($id)
{
/** @var $action GenerateAction */
$action = parent::createAction($id);
foreach ($this->_options as $name => $value) {
$action->generator->$name = $value;
}
return $action;
}
/**
* @inheritdoc
*/
public function actions()
{
$actions = [];
foreach ($this->generators as $name => $generator) {
$actions[$name] = [
'class' => 'yii\gii\console\GenerateAction',
'generator' => $generator,
];
}
return $actions;
}
public function actionIndex()
{
$this->run('/help', ['gii']);
}
/**
* @inheritdoc
*/
public function getUniqueID()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function options($id)
{
$options = parent::options($id);
$options[] = 'overwrite';
if (!isset($this->generators[$id])) {
return $options;
}
$attributes = $this->generators[$id]->attributes;
unset($attributes['templates']);
return array_merge(
$options,
array_keys($attributes)
);
}
/**
* @inheritdoc
*/
public function getActionHelpSummary($action)
{
if ($action instanceof InlineAction) {
return parent::getActionHelpSummary($action);
} else {
/** @var $action GenerateAction */
return $action->generator->getName();
}
}
/**
* @inheritdoc
*/
public function getActionHelp($action)
{
if ($action instanceof InlineAction) {
return parent::getActionHelp($action);
} else {
/** @var $action GenerateAction */
$description = $action->generator->getDescription();
return wordwrap(preg_replace('/\s+/', ' ', $description));
}
}
/**
* @inheritdoc
*/
public function getActionArgsHelp($action)
{
return [];
}
/**
* @inheritdoc
*/
public function getActionOptionsHelp($action)
{
if ($action instanceof InlineAction) {
return parent::getActionOptionsHelp($action);
}
/** @var $action GenerateAction */
$attributes = $action->generator->attributes;
unset($attributes['templates']);
$hints = $action->generator->hints();
$options = parent::getActionOptionsHelp($action);
foreach ($attributes as $name => $value) {
$type = gettype($value);
$options[$name] = [
'type' => $type === 'NULL' ? 'string' : $type,
'required' => $value === null && $action->generator->isAttributeRequired($name),
'default' => $value,
'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '',
];
}
return $options;
}
protected function formatHint($hint)
{
$hint = preg_replace('%<code>(.*?)</code>%', '\1', $hint);
$hint = preg_replace('/\s+/', ' ', $hint);
return wordwrap($hint);
}
}