-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchController.php
251 lines (220 loc) · 7.62 KB
/
BatchController.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace carono\giix;
use carono\giix\generators\model\Generator as ModelGenerator;
use yii\console\Controller;
use yii\helpers\Inflector;
/**
* @author Tobias Munk <[email protected]>
*
* @property array $yiiConfiguration
*/
class BatchController extends Controller
{
public $exceptTables = ['{{%migration}}'];
/**
* @var string the generator template name
*/
public $template = 'default';
/**
* @var bool whether to generate and overwrite all files
*/
public $overwrite = false;
/**
* @var array table names for generating models and CRUDs
*/
public $tables = [];
/**
* @var string eg. `app_`
*/
public $tablePrefix = '';
/**
* @var array mapping for table name to model class names
*/
public $tableNameMap = [];
/**
* @var string namespace path for model classes
*/
public $modelNamespace = 'common\\models';
/**
* @var string suffix to append to the base model, setting "Base" will result in a model named "PostBase"
*/
public $modelBaseClassSuffix = '';
/**
* @var string database application component
*/
public $modelDb = 'db';
/**
* @var string base class for the generated models
*/
public $modelBaseClass = 'yii\db\ActiveRecord';
/**
* @var bool whether the strings will be generated using `Yii::t()` or normal strings
*/
public $enableI18N = true;
/**
* @var bool whether the entity names will be singular or the same as the table name
*/
public $singularEntities = true;
/**
* @var string the message category for models used by `Yii::t()` when `$enableI18N` is `true`.
* Defaults to `app`
*/
public $modelMessageCategory = 'models';
/**
* @var string the message category for CRUDs used by `Yii::t()` when `$enableI18N` is `true`.
* Defaults to `app`
*/
/**
* @var bool indicates whether to generate ActiveQuery for the ActiveRecord class
*/
public $modelGenerateQuery = true;
/**
* @var string the namespace of the ActiveQuery class to be generated
*/
public $modelQueryNamespace = 'app\models\query';
/**
* @var string the base class of the new ActiveQuery class
*/
public $modelQueryBaseClass = 'yii\db\ActiveQuery';
/**
* @var bool This indicates whether the generator should generate attribute labels by using the comments of the corresponding DB columns
*/
public $modelGenerateLabelsFromComments = false;
/**
* @var bool This indicates whether the generator should generate attribute hints by using the comments of the corresponding DB columns
*/
public $modelGenerateHintsFromComments = true;
/**
* @var array application configuration for creating temporary applications
*/
protected $appConfig;
/**
* @var \carono\giix\generators\model\Generator
*/
protected $modelGenerator;
/**
* {@inheritdoc}
*/
public function options($id)
{
return array_merge(
parent::options($id),
[
'template',
'overwrite',
'extendedModels',
'enableI18N',
'messageCategory',
'singularEntities',
'tables',
'tablePrefix',
'modelDb',
'modelNamespace',
'modelBaseClass',
'modelBaseClassSuffix',
'modelGenerateQuery',
'modelQueryNamespace',
'modelQueryBaseClass',
'modelGenerateLabelsFromComments',
'modelGenerateHintsFromComments'
]
);
}
/**
* Loads application configuration and checks tables parameter.
*
* @param \yii\base\Action $action
*
* @return bool
*/
public function beforeAction($action)
{
$this->appConfig = $this->getYiiConfiguration();
$this->appConfig['id'] = 'temp';
$this->modelGenerator = new ModelGenerator(['db' => $this->modelDb]);
if (!$this->tables) {
$this->modelGenerator->tableName = '*';
$this->tables = $this->modelGenerator->getTableNames();
$tableList = implode("\n\t- ", $this->tables);
$msg = "Are you sure that you want to run action \"{$action->id}\" for the following tables?\n\t- {$tableList}\n\n";
if (!$this->confirm($msg)) {
return false;
}
}
$this->tables = array_diff($this->tables, $this->exceptTables);
return parent::beforeAction($action);
}
/**
* Run batch process to generate models and CRUDs for all given tables.
*
* @param string $message the message to be echoed
*/
public function actionIndex()
{
echo "Running model batch...\n";
$this->actionModels();
}
/**
* Run batch process to generate models all given tables.
*
* @throws \yii\console\Exception
*/
public function actionModels()
{
foreach ($this->tables as $table) {
$params = [
'interactive' => $this->interactive,
'overwrite' => $this->overwrite,
'template' => $this->template,
'ns' => $this->modelNamespace,
'db' => $this->modelDb,
'tableName' => $table,
'tablePrefix' => $this->tablePrefix,
'enableI18N' => $this->enableI18N,
'singularEntities' => $this->singularEntities,
'messageCategory' => $this->modelMessageCategory,
'baseClassSuffix' => $this->modelBaseClassSuffix,
'modelClass' => isset($this->tableNameMap[$table]) ? $this->tableNameMap[$table] : Inflector::camelize($table),
'baseClass' => $this->modelBaseClass,
'tableNameMap' => $this->tableNameMap,
'generateQuery' => $this->modelGenerateQuery,
'queryNs' => $this->modelQueryNamespace,
'queryBaseClass' => $this->modelQueryBaseClass,
'generateLabelsFromComments' => $this->modelGenerateLabelsFromComments,
'generateHintsFromComments' => $this->modelGenerateHintsFromComments,
];
$route = 'gii/carono-model';
$app = \Yii::$app;
$temp = new \yii\console\Application($this->appConfig);
$temp->runAction(ltrim($route, '/'), $params);
unset($temp);
\Yii::$app = $app;
\Yii::$app->log->logger->flush(true);
}
}
/**
* Returns Yii's initial configuration array.
*
* @todo should be removed, if this issue is closed -> https://github.com/yiisoft/yii2/pull/5687
*
* @return array
*/
protected function getYiiConfiguration()
{
if (isset($GLOBALS['config'])) {
$config = $GLOBALS['config'];
} else {
$config = \yii\helpers\ArrayHelper::merge(
require(\Yii::getAlias('@app') . '/../common/config/main.php'),
(is_file(\Yii::getAlias('@app') . '/../common/config/main-local.php')) ?
require(\Yii::getAlias('@app') . '/../common/config/main-local.php')
: [],
require(\Yii::getAlias('@app') . '/../console/config/main.php'),
(is_file(\Yii::getAlias('@app') . '/../console/config/main-local.php')) ?
require(\Yii::getAlias('@app') . '/../console/config/main-local.php')
: []
);
}
return $config;
}
}