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
<?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\console\Controller;
/**
* Allows you to run Gii from the command line.
* Example command:
*
* ```
* $ ./yii gii/<generator> --property1=foo --property2=bar --generate=true
* ```
*
* @author Tobias Munk <schmunk@usrbin.de>
* @since 2.0
*/
class GenerateController extends Controller
{
/**
* @var \yii\gii\Module
*/
public $module;
/**
* @var boolean whether to generate all files and overwrite existing files
*/
public $generate = false;
public $generators = [];
/**
* @var array generator option values
*/
private $_options = [];
/**
* @inheritdoc
*/
public function __get($name)
{
return isset($this->_options[$name]) ? $this->_options[$name] : null;
if ($this->action) {
$options = $this->options($this->action->id);
if (in_array($name, $options)) {
return isset($this->_options[$name]) ? $this->_options[$name] : null;
} else {
return parent::__get($name);
}
} elseif (array_key_exists($name, $this->_options)) {
return $this->_options[$name];
} else {
return parent::__get($name);
}
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
$this->_options[$name] = $value;
return;
if ($this->action) {
$options = $this->options($this->action->id);
if (in_array($name, $options)) {
$this->_options[$name] = $value;
} else {
parent::__set($name, $value);
}
} else {
$this->_options[$name] = $value;
}
}
public function init()
{
parent::init();
foreach ($this->generators as $id => $config) {
$this->generators[$id] = Yii::createObject($config);
}
}
public function createAction($id)
{
$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\Action',
'generator' => $generator,
];
}
return $actions;
}
public function getUniqueID()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function options($id)
{
if (isset($this->generators[$id])) {
return array_merge(
parent::options($id),
array_keys($this->generators[$id]->attributes)
);
} else {
return parent::options($id);
}
}
/**
* @inheritdoc
*/
public function getActionHelpSummary($action)
{
/** @var $action Action */
return $action->generator->getName();
}
/**
* @inheritdoc
*/
public function getActionHelp($action)
{
/** @var $action Action */
return $action->generator->getDescription();
}
/**
* @inheritdoc
*/
public function getActionArgsHelp($action)
{
return [];
}
/**
* @inheritdoc
*/
public function getActionOptionsHelp($action)
{
/** @var $action Action */
$attributes = $action->generator->attributes;
$hints = $action->generator->hints();
$options = [];
foreach ($attributes as $name => $value) {
$options[$name] = [
'type' => 'string',
'default' => $value,
'comment' => isset($hints[$name]) ? $hints[$name] : '',
];
}
return $options;
}
}