-
Notifications
You must be signed in to change notification settings - Fork 97
/
BaseGenerator.php
539 lines (489 loc) · 19.7 KB
/
BaseGenerator.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/**
* Created by PhpStorm.
* User: Yohanes
* Date: 14-Jun-16
* Time: 3:56 PM
*/
namespace mootensai\enhancedgii;
use Yii;
use yii\base\NotSupportedException;
use yii\db\Connection;
use yii\db\TableSchema;
use yii\helpers\Inflector;
abstract class BaseGenerator extends \yii\gii\Generator
{
const RELATIONS_NONE = 'none';
const RELATIONS_ALL = 'all';
const RELATIONS_ALL_INVERSE = 'all-inverse';
// thanks to github.com/iurijacob for simplify the relation array
const REL_TYPE = 0;
const REL_CLASS = 1;
const REL_IS_MULTIPLE = 2;
const REL_TABLE = 3;
const REL_PRIMARY_KEY = 4;
const REL_FOREIGN_KEY = 5;
const REL_IS_MASTER = 6;
const FK_TABLE_NAME = 0;
const FK_FIELD_NAME = 1;
public $db = 'db';
/* @var $tableSchema TableSchema */
public $tableSchema;
public $tableName;
public $modelClass;
public $baseModelClass = 'yii\db\ActiveRecord';
public $nsModel = 'app\models';
public $nsSearchModel = 'app\models';
public $skippedRelations;
public $useSchemaName = true;
static public function getTreeColumns(){
return ['id', 'root', 'lft', 'rgt', 'lvl', 'name', 'icon', 'icon_type', 'active', 'selected', 'disabled', 'readonly',
'visible', 'collapsed', 'movable_u', 'movable_d', 'movable_l', 'movable_r', 'removable', 'removable_all'];
}
/**
* @inheritdoc
*/
public function autoCompleteData()
{
$db = $this->getDbConnection();
if ($db !== null) {
return [
'tableName' => function () use ($db) {
return $db->getSchema()->getTableNames();
},
];
} else {
return [];
}
}
/**
* Validates the [[db]] attribute.
*/
public function validateDb()
{
if (!Yii::$app->has($this->db)) {
$this->addError('db', 'There is no application component named "db".');
} elseif (!Yii::$app->get($this->db) instanceof Connection) {
$this->addError('db', 'The "db" application component must be a DB connection instance.');
}
}
/**
* Validates the [[tableName]] attribute.
*/
public function validateTableName()
{
if (strpos($this->tableName, '*') !== false && substr_compare($this->tableName, '*', -1, 1)) {
$this->addError('tableName', 'Asterisk is only allowed as the last character.');
return;
}
$tables = $this->getTableNames();
if (empty($tables)) {
$this->addError('tableName', "Table '{$this->tableName}' does not exist.");
} else {
foreach ($tables as $table) {
$class = $this->generateClassName($table);
if ($this->isReservedKeyword($class)) {
$this->addError('tableName', "Table '$table' will generate a class which is a reserved PHP keyword.");
break;
}
}
}
}
/**
* Checks if model class is valid
*/
public function validateModelClass()
{
$pk = $this->tableSchema->primaryKey;
if (empty($pk)) {
$this->addError('modelClass', "The table associated with $this->modelClass must have primary key(s).");
}
}
// public function isModelExist()
// {
// $class = $this->modelClass;
// try {
// if (!class_exists($class)) {
// return false;
// }
// } catch (\Exception $e) {
// return false;
// }
// }
/**
* Checks if the given table is a junction table.
* For simplicity, this method only deals with the case where the pivot contains two PK columns,
* each referencing a column in a different table.
* @param TableSchema the table being checked
* @param TableSchema $table
* @return array|boolean the relevant foreign key constraint information if the table is a junction table,
* or false if the table is not a junction table.
*/
protected function checkPivotTable($table)
{
$pk = $table->primaryKey;
if (count($pk) !== 2) {
return false;
}
$fks = [];
foreach ($table->foreignKeys as $refs) {
if (count($refs) === 2) {
if (isset($refs[$pk[0]])) {
$fks[$pk[0]] = [$refs[0], $refs[$pk[0]]];
} elseif (isset($refs[$pk[1]])) {
$fks[$pk[1]] = [$refs[0], $refs[$pk[1]]];
}
}
}
if (count($fks) === 2 && $fks[$pk[0]][0] !== $fks[$pk[1]][0]) {
return $fks;
} else {
return false;
}
}
/**
* @return string[] all db schema names or an array with a single empty string
* @throws NotSupportedException
* @since 2.0.5
*/
protected function getSchemaNames()
{
$db = $this->getDbConnection();
$schema = $db->getSchema();
if ($schema->hasMethod('getSchemaNames')) { // keep BC to Yii versions < 2.0.4
try {
$schemaNames = $schema->getSchemaNames();
} catch (NotSupportedException $e) {
// schema names are not supported by schema
}
}
if (!isset($schemaNames)) {
if (($pos = strpos($this->tableName, '.')) !== false) {
$schemaNames = [substr($this->tableName, 0, $pos)];
} else {
$schemaNames = [''];
}
}
return $schemaNames;
}
/**
* @return array the generated relation declarations
*/
protected function generateRelations()
{
if (!$this->generateRelations === self::RELATIONS_NONE) {
return [];
}
$db = $this->getDbConnection();
$relations = [];
foreach ($this->getSchemaNames() as $schemaName) {
foreach ($db->getSchema()->getTableSchemas($schemaName) as $table) {
$className = $this->generateClassName($table->fullName);
foreach ($table->foreignKeys as $refs) {
$refTable = $refs[0];
$refTableSchema = $db->getTableSchema($refTable);
if ($refTableSchema === null) {
// Foreign key could point to non-existing table: https://github.com/yiisoft/yii2-gii/issues/34
continue;
}
unset($refs[0]);
$fks = array_keys($refs);
$refClassName = $this->generateClassName($refTable);
// Add relation for this table
$link = $this->generateRelationLink(array_flip($refs));
$relationName = $this->generateRelationName($relations, $table, $fks[0], false);
$relFK = key($refs);
$relations[$table->fullName][lcfirst($relationName)] = [
self::REL_TYPE => "return \$this->hasOne(\\{$this->nsModel}\\$refClassName::className(), $link);", // relation type
self::REL_CLASS => $refClassName, //relclass
self::REL_IS_MULTIPLE => 0, //is multiple
self::REL_TABLE => $refTable, //related table
self::REL_PRIMARY_KEY => $refs[$relFK], // related primary key
self::REL_FOREIGN_KEY => $relFK, // this foreign key
self::REL_IS_MASTER => in_array($relFK, $table->getColumnNames()) ? 1 : 0
];
// Add relation for the referenced table
$hasMany = $this->isHasManyRelation($table, $fks);
$link = $this->generateRelationLink($refs);
$relationName = $this->generateRelationName($relations, $refTableSchema, $className, $hasMany);
$relations[$refTableSchema->fullName][lcfirst($relationName)] = [
self::REL_TYPE => "return \$this->" . ($hasMany ? 'hasMany' : 'hasOne') . "(\\{$this->nsModel}\\$className::className(), $link);", // rel type
self::REL_CLASS => $className, //rel class
self::REL_IS_MULTIPLE => $hasMany, //is multiple
self::REL_TABLE => $table->fullName, // rel table
self::REL_PRIMARY_KEY => $refs[key($refs)], // rel primary key
self::REL_FOREIGN_KEY => key($refs), // this foreign key
self::REL_IS_MASTER => in_array($relFK, $refTableSchema->getColumnNames()) ? 1 : 0
];
}
if (($junctionFks = $this->checkPivotTable($table)) === false) {
continue;
}
$relations = $this->generateManyManyRelations($table, $junctionFks, $relations);
}
}
if ($this->generateRelations === self::RELATIONS_ALL_INVERSE) {
return $this->addInverseRelations($relations);
}
return $relations;
}
/**
* Determines if relation is of has many type
*
* @param TableSchema $table
* @param array $fks
* @return boolean
* @since 2.0.5
*/
protected function isHasManyRelation($table, $fks)
{
$uniqueKeys = [$table->primaryKey];
try {
$uniqueKeys = array_merge($uniqueKeys, $this->getDbConnection()->getSchema()->findUniqueIndexes($table));
} catch (NotSupportedException $e) {
// ignore
}
foreach ($uniqueKeys as $uniqueKey) {
if (count(array_diff(array_merge($uniqueKey, $fks), array_intersect($uniqueKey, $fks))) === 0) {
return false;
}
}
return true;
}
/**
* Adds inverse relations
*
* @param array $relations relation declarations
* @return array relation declarations extended with inverse relation names
* @since 2.0.5
*/
protected function addInverseRelations($relations)
{
$relationNames = [];
foreach ($this->getSchemaNames() as $schemaName) {
foreach ($this->getDbConnection()->getSchema()->getTableSchemas($schemaName) as $table) {
$className = $this->generateClassName($table->fullName);
foreach ($table->foreignKeys as $refs) {
$refTable = $refs[0];
$refTableSchema = $this->getDbConnection()->getTableSchema($refTable);
unset($refs[0]);
$fks = array_keys($refs);
$leftRelationName = $this->generateRelationName($relationNames, $table, $fks[0], false);
$relationNames[$table->fullName][$leftRelationName] = true;
$hasMany = $this->isHasManyRelation($table, $fks);
$rightRelationName = $this->generateRelationName(
$relationNames,
$refTableSchema,
$className,
$hasMany
);
$relationNames[$refTableSchema->fullName][$rightRelationName] = true;
$relations[$table->fullName][$leftRelationName][0] =
rtrim($relations[$table->fullName][$leftRelationName][0], ';')
. "->inverseOf('".lcfirst($rightRelationName)."');";
$relations[$refTableSchema->fullName][$rightRelationName][0] =
rtrim($relations[$refTableSchema->fullName][$rightRelationName][0], ';')
. "->inverseOf('".lcfirst($leftRelationName)."');";
}
}
}
return $relations;
}
/**
* Generates relations using a junction table by adding an extra viaTable().
* @param TableSchema the table being checked
* @param array $fks obtained from the checkPivotTable() method
* @param array $relations
* @param TableSchema $table
* @return array modified $relations
*/
private function generateManyManyRelations($table, $fks, $relations)
{
$db = $this->getDbConnection();
$table0 = $fks[$table->primaryKey[0]][0];
$table1 = $fks[$table->primaryKey[1]][0];
$className0 = $this->generateClassName($table0);
$className1 = $this->generateClassName($table1);
$table0Schema = $db->getTableSchema($table0);
$table1Schema = $db->getTableSchema($table1);
$link = $this->generateRelationLink([$fks[$table->primaryKey[1]][1] => $table->primaryKey[1]]);
$viaLink = $this->generateRelationLink([$table->primaryKey[0] => $fks[$table->primaryKey[0]][1]]);
$relationName = $this->generateRelationName($relations, $table0Schema, $table->primaryKey[1], true);
$relations[$table0Schema->fullName][$relationName] = [
"return \$this->hasMany(\\{$this->nsModel}\\$className1::className(), $link)->viaTable('"
. $this->generateTableName($table->name) . "', $viaLink);",
$className1,
true,
];
$link = $this->generateRelationLink([$fks[$table->primaryKey[0]][1] => $table->primaryKey[0]]);
$viaLink = $this->generateRelationLink([$table->primaryKey[1] => $fks[$table->primaryKey[1]][1]]);
$relationName = $this->generateRelationName($relations, $table1Schema, $table->primaryKey[0], true);
$relations[$table1Schema->fullName][$relationName] = [
"return \$this->hasMany(\\{$this->nsModel}\\$className0::className(), $link)->viaTable('"
. $this->generateTableName($table->name) . "', $viaLink);",
$className0,
true,
];
return $relations;
}
/**
* Generates the link parameter to be used in generating the relation declaration.
* @param array $refs reference constraint
* @return string the generated link parameter.
*/
protected function generateRelationLink($refs)
{
$pairs = [];
foreach ($refs as $a => $b) {
$pairs[] = "'$a' => '$b'";
}
return '[' . implode(', ', $pairs) . ']';
}
/**
* Generates a class name from the specified table name.
* @param string $tableName the table name (which may contain schema prefix)
* @param boolean $useSchemaName should schema name be included in the class name, if present
* @return string the generated class name
*/
protected function generateClassName($tableName, $useSchemaName = null)
{
if (isset($this->classNames[$tableName])) {
return $this->classNames[$tableName];
}
$schemaName = '';
$fullTableName = $tableName;
if (($pos = strrpos($tableName, '.')) !== false) {
if (($useSchemaName === null && $this->useSchemaName) || $useSchemaName) {
$schemaName = substr($tableName, 0, $pos) . '_';
}
$tableName = substr($tableName, $pos + 1);
}
$db = $this->getDbConnection();
$patterns = [];
$patterns[] = "/^{$db->tablePrefix}(.*?)$/";
$patterns[] = "/^(.*?){$db->tablePrefix}$/";
if (strpos($this->tableName, '*') !== false) {
$pattern = $this->tableName;
if (($pos = strrpos($pattern, '.')) !== false) {
$pattern = substr($pattern, $pos + 1);
}
$patterns[] = '/^' . str_replace('*', '(\w+)', $pattern) . '$/';
}
$className = $tableName;
foreach ($patterns as $pattern) {
if (preg_match($pattern, $tableName, $matches)) {
$className = $matches[1];
break;
}
}
return $this->classNames[$fullTableName] = Inflector::id2camel($schemaName . $className, '_');
}
/**
* Generate a relation name for the specified table and a base name.
* @param array $relations the relations being generated currently.
* @param TableSchema $table the table schema
* @param string $key a base name that the relation name may be generated from
* @param boolean $multiple whether this is a has-many relation
* @return string the relation name
*/
protected function generateRelationName($relations, $table, $key, $multiple)
{
// print_r($key);
static $baseModel;
if ($baseModel === null && isset($this->baseClass)) {
$baseClass = $this->baseClass;
$baseModel = new $baseClass();
}
if (!empty($key) && substr_compare($key, 'id', -2, 2, true) === 0 && strcasecmp($key, 'id')) {
$key = rtrim(substr($key, 0, -2), '_');
} else if (!empty($key) && substr_compare($key, 'id', 0, 2, true) === 0 && strcasecmp($key, 'id')) {
$key = ltrim(substr($key, 2, strlen($key)), '_');
}
if ($multiple) {
$key = Inflector::pluralize($key);
}
$name = $rawName = Inflector::id2camel($key, '_');
$i = 0;
while (isset($baseModel) && $baseModel->hasProperty(lcfirst($name))) {
$name = $rawName . ($i++);
}
while (isset($table->columns[lcfirst($name)])) {
$name = $rawName . ($i++);
}
while (isset($relations[$table->fullName][$name])) {
$name = $rawName . ($i++);
}
return lcfirst($name);
}
/**
* @return Connection the DB connection as specified by [[db]].
*/
public function getDbConnection()
{
return Yii::$app->get($this->db, false);
}
public function getTableSchema()
{
return $this->tableSchema;
}
protected $tableNames;
protected $classNames;
/**
* @return array the table names that match the pattern specified by [[tableName]].
*/
protected function getTableNames()
{
if ($this->tableNames !== null) {
return $this->tableNames;
}
$db = $this->getDbConnection();
if ($db === null) {
return [];
}
$tableNames = [];
if (strpos($this->tableName, '*') !== false) {
if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos);
$pattern = '/^' . str_replace('*', '\w+', substr($this->tableName, $pos + 1)) . '$/';
} else {
$schema = '';
$pattern = '/^' . str_replace('*', '\w+', $this->tableName) . '$/';
}
foreach ($db->schema->getTableNames($schema) as $table) {
if (preg_match($pattern, $table)) {
$tableNames[] = $schema === '' ? $table : ($schema . '.' . $table);
}
}
} elseif (($table = $db->getTableSchema($this->tableName, true)) !== null) {
$tableNames[] = $this->tableName;
$this->classNames[$this->tableName] = $this->modelClass;
}
return $this->tableNames = $tableNames;
}
/**
* @return integer[] model column names
*/
public function getColumnNames()
{
return $this->getTableSchema()->getColumnNames();
}
/**
* Generates the table name by considering table prefix.
* If [[useTablePrefix]] is false, the table name will be returned without change.
* @param string $tableName the table name (which may contain schema prefix)
* @return string the generated table name
*/
public function generateTableName($tableName)
{
if (!$this->useTablePrefix) {
return $tableName;
}
$db = $this->getDbConnection();
if (preg_match("/^{$db->tablePrefix}(.*?)$/", $tableName, $matches)) {
$tableName = '{{%' . $matches[1] . '}}';
} elseif (preg_match("/^(.*?){$db->tablePrefix}$/", $tableName, $matches)) {
$tableName = '{{' . $matches[1] . '%}}';
}
return $tableName;
}
}