-
Notifications
You must be signed in to change notification settings - Fork 17
/
GenerateModelsCommand.php
604 lines (501 loc) · 18 KB
/
GenerateModelsCommand.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<?php
namespace User11001\EloquentModelGenerator\Console;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Way\Generators\Commands\GeneratorCommand;
use Way\Generators\Generator;
use Way\Generators\Filesystem\Filesystem;
use Way\Generators\Compilers\TemplateCompiler;
use Illuminate\Contracts\Config\Repository as Config;
class GenerateModelsCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'models:generate';
private static $namespace;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate Eloquent models from an existing table structure.';
private $schemaGenerator;
/**
* @param Generator $generator
* @param Filesystem $file
* @param TemplateCompiler $compiler
* @param Config $config
*/
public function __construct(
Generator $generator,
Filesystem $file,
TemplateCompiler $compiler,
Config $config
) {
$this->file = $file;
$this->compiler = $compiler;
$this->config = $config;
parent::__construct($generator);
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['tables', InputArgument::OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'],
];
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
//shameless copy
return [
['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get('database.default')],
['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'],
['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'],
['namespace', 'ns', InputOption::VALUE_OPTIONAL, 'Explicitly set the namespace'],
['overwrite', 'o', InputOption::VALUE_NONE, 'Overwrite existing models ?'],
];
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
//0. determine destination folder
$destinationFolder = $this->getFileGenerationPath();
//1. fetch all tables
$this->info("\nFetching tables...");
$this->initializeSchemaGenerator();
$tables = $this->getTables();
//2. for each table, fetch primary and foreign keys
$this->info('Fetching table columns, primary keys, foreign keys');
$prep = $this->getColumnsPrimaryAndForeignKeysPerTable($tables);
//3. create an array of rules, holding the info for our Eloquent models to be
$this->info('Generating Eloquent rules');
$eloquentRules = $this->getEloquentRules($tables, $prep);
//4. Generate our Eloquent Models
$this->info("Generating Eloquent models\n");
$this->generateEloquentModels($destinationFolder, $eloquentRules);
$this->info("\nAll done!");
}
public function getTables() {
$schemaTables = $this->schemaGenerator->getTables();
$specifiedTables = $this->option('tables');
//when no tables specified, generate all tables
if(empty($specifiedTables)) {
return $schemaTables;
}
$specifiedTables = explode(',', $specifiedTables);
$tablesToGenerate = [];
foreach($specifiedTables as $specifiedTable) {
if(!in_array($specifiedTable, $schemaTables)) {
$this->error("specified table not found: $specifiedTable");
} else {
$tablesToGenerate[$specifiedTable] = $specifiedTable;
}
}
if(empty($tablesToGenerate)) {
$this->error('No tables to generate');
die;
}
return array_values($tablesToGenerate);
}
private function generateEloquentModels($destinationFolder, $eloquentRules)
{
//0. set namespace
self::$namespace = $this->getNamespace();
foreach ($eloquentRules as $table => $rules) {
try {
$this->generateEloquentModel($destinationFolder, $table, $rules);
} catch(Exception $e) {
$this->error("\nFailed to generate model for table $table");
return;
}
}
}
private function generateEloquentModel($destinationFolder, $table, $rules) {
//1. Determine path where the file should be generated
$modelName = $this->generateModelNameFromTableName($table);
$filePathToGenerate = $destinationFolder . '/'.$modelName.'.php';
$canContinue = $this->canGenerateEloquentModel($filePathToGenerate, $table);
if(!$canContinue) {
return;
}
//2. generate relationship functions and fillable array
$hasMany = $rules['hasMany'];
$hasOne = $rules['hasOne'];
$belongsTo = $rules['belongsTo'];
$belongsToMany = $rules['belongsToMany'];
$fillable = implode(', ', $rules['fillable']);
$belongsToFunctions = $this->generateBelongsToFunctions($belongsTo);
$belongsToManyFunctions = $this->generateBelongsToManyFunctions($belongsToMany);
$hasManyFunctions = $this->generateHasManyFunctions($hasMany);
$hasOneFunctions = $this->generateHasOneFunctions($hasOne);
$functions = $this->generateFunctions([
$belongsToFunctions,
$belongsToManyFunctions,
$hasManyFunctions,
$hasOneFunctions,
]);
//3. prepare template data
$templateData = array(
'NAMESPACE' => self::$namespace,
'NAME' => $modelName,
'TABLENAME' => $table,
'FILLABLE' => $fillable,
'FUNCTIONS' => $functions
);
$templatePath = $this->getTemplatePath();
//run Jeffrey's generator
$this->generator->make(
$templatePath,
$templateData,
$filePathToGenerate
);
$this->info("Generated model for table $table");
}
private function canGenerateEloquentModel($filePathToGenerate, $table) {
$canOverWrite = $this->option('overwrite');
if(file_exists($filePathToGenerate)) {
if($canOverWrite) {
$deleted = unlink($filePathToGenerate);
if(!$deleted) {
$this->warn("Failed to delete existing model $filePathToGenerate");
return false;
}
} else {
$this->warn("Skipped model generation, file already exists. (force using --overwrite) $table -> $filePathToGenerate");
return false;
}
}
return true;
}
private function getNamespace() {
$ns = $this->option('namespace');
if(empty($ns)) {
$ns = env('APP_NAME','App\Models');
}
//convert forward slashes in the namespace to backslashes
$ns = str_replace('/', '\\', $ns);
return $ns;
}
private function generateFunctions($functionsContainer)
{
$f = '';
foreach ($functionsContainer as $functions) {
$f .= $functions;
}
return $f;
}
private function generateHasManyFunctions($rulesContainer)
{
$functions = '';
foreach ($rulesContainer as $rules) {
$hasManyModel = $this->generateModelNameFromTableName($rules[0]);
$key1 = $rules[1];
$key2 = $rules[2];
$hasManyFunctionName = $this->getPluralFunctionName($hasManyModel);
$function = "
public function $hasManyFunctionName() {".'
return $this->hasMany'."(\\".self::$namespace."\\$hasManyModel::class, '$key1', '$key2');
}
";
$functions .= $function;
}
return $functions;
}
private function generateHasOneFunctions($rulesContainer)
{
$functions = '';
foreach ($rulesContainer as $rules) {
$hasOneModel = $this->generateModelNameFromTableName($rules[0]);
$key1 = $rules[1];
$key2 = $rules[2];
$hasOneFunctionName = $this->getSingularFunctionName($hasOneModel);
$function = "
public function $hasOneFunctionName() {".'
return $this->hasOne'."(\\".self::$namespace."\\$hasOneModel::class, '$key1', '$key2');
}
";
$functions .= $function;
}
return $functions;
}
private function generateBelongsToFunctions($rulesContainer)
{
$functions = '';
foreach ($rulesContainer as $rules) {
$belongsToModel = $this->generateModelNameFromTableName($rules[0]);
$key1 = $rules[1];
$key2 = $rules[2];
$belongsToFunctionName = $this->getSingularFunctionName($belongsToModel);
$function = "
public function $belongsToFunctionName() {".'
return $this->belongsTo'."(\\".self::$namespace."\\$belongsToModel::class, '$key1', '$key2');
}
";
$functions .= $function;
}
return $functions;
}
private function generateBelongsToManyFunctions($rulesContainer)
{
$functions = '';
foreach ($rulesContainer as $rules) {
$belongsToManyModel = $this->generateModelNameFromTableName($rules[0]);
$through = $rules[1];
$key1 = $rules[2];
$key2 = $rules[3];
$belongsToManyFunctionName = $this->getPluralFunctionName($belongsToManyModel);
$function = "
public function $belongsToManyFunctionName() {".'
return $this->belongsToMany'."(\\".self::$namespace."\\$belongsToManyModel::class, '$through', '$key1', '$key2');
}
";
$functions .= $function;
}
return $functions;
}
private function getPluralFunctionName($modelName)
{
$modelName = lcfirst($modelName);
return str_plural($modelName);
}
private function getSingularFunctionName($modelName)
{
$modelName = lcfirst($modelName);
return str_singular($modelName);
}
private function generateModelNameFromTableName($table)
{
return ucfirst(camel_case(str_singular($table)));
}
private function getColumnsPrimaryAndForeignKeysPerTable($tables)
{
$prep = [];
foreach ($tables as $table) {
//get foreign keys
$foreignKeys = $this->schemaGenerator->getForeignKeyConstraints($table);
//get primary keys
$primaryKeys = $this->schemaGenerator->getPrimaryKeys($table);
// get columns lists
$__columns = $this->schemaGenerator->getSchema()->listTableColumns($table);
$columns = [];
foreach($__columns as $col) {
$columns[] = $col->toArray()['name'];
}
$prep[$table] = [
'foreign' => $foreignKeys,
'primary' => $primaryKeys,
'columns' => $columns,
];
}
return $prep;
}
private function getEloquentRules($tables, $prep)
{
$rules = [];
//first create empty ruleset for each table
foreach ($prep as $table => $properties) {
$rules[$table] = [
'hasMany' => [],
'hasOne' => [],
'belongsTo' => [],
'belongsToMany' => [],
'fillable' => [],
];
}
foreach ($prep as $table => $properties) {
$foreign = $properties['foreign'];
$primary = $properties['primary'];
$columns = $properties['columns'];
$this->setFillableProperties($table, $rules, $columns);
$isManyToMany = $this->detectManyToMany($prep, $table);
if ($isManyToMany === true) {
$this->addManyToManyRules($tables, $table, $prep, $rules);
}
//the below used to be in an ELSE clause but we should be as verbose as possible
//when we detect a many-to-many table, we still want to set relations on it
//else
{
foreach ($foreign as $fk) {
$isOneToOne = $this->detectOneToOne($fk, $primary);
if ($isOneToOne) {
$this->addOneToOneRules($tables, $table, $rules, $fk);
} else {
$this->addOneToManyRules($tables, $table, $rules, $fk);
}
}
}
}
return $rules;
}
private function setFillableProperties($table, &$rules, $columns)
{
$fillable = [];
foreach ($columns as $column_name) {
if ($column_name !== 'created_at' && $column_name !== 'updated_at') {
$fillable[] = "'$column_name'";
}
}
$rules[$table]['fillable'] = $fillable;
}
private function addOneToManyRules($tables, $table, &$rules, $fk)
{
//$table belongs to $FK
//FK hasMany $table
$fkTable = $fk['on'];
$field = $fk['field'];
$references = $fk['references'];
if(in_array($fkTable, $tables)) {
$rules[$fkTable]['hasMany'][] = [$table, $field, $references];
}
if(in_array($table, $tables)) {
$rules[$table]['belongsTo'][] = [$fkTable, $field, $references];
}
}
private function addOneToOneRules($tables, $table, &$rules, $fk)
{
//$table belongsTo $FK
//$FK hasOne $table
$fkTable = $fk['on'];
$field = $fk['field'];
$references = $fk['references'];
if(in_array($fkTable, $tables)) {
$rules[$fkTable]['hasOne'][] = [$table, $field, $references];
}
if(in_array($table, $tables)) {
$rules[$table]['belongsTo'][] = [$fkTable, $field, $references];
}
}
private function addManyToManyRules($tables, $table, $prep, &$rules)
{
//$FK1 belongsToMany $FK2
//$FK2 belongsToMany $FK1
$foreign = $prep[$table]['foreign'];
$fk1 = $foreign[0];
$fk1Table = $fk1['on'];
$fk1Field = $fk1['field'];
//$fk1References = $fk1['references'];
$fk2 = $foreign[1];
$fk2Table = $fk2['on'];
$fk2Field = $fk2['field'];
//$fk2References = $fk2['references'];
//User belongstomany groups user_group, user_id, group_id
if(in_array($fk1Table, $tables)) {
$rules[$fk1Table]['belongsToMany'][] = [$fk2Table, $table, $fk1Field, $fk2Field];
}
if(in_array($fk2Table, $tables)) {
$rules[$fk2Table]['belongsToMany'][] = [$fk1Table, $table, $fk2Field, $fk1Field];
}
}
//if FK is also a primary key, and there is only one primary key, we know this will be a one to one relationship
private function detectOneToOne($fk, $primary)
{
if (count($primary) === 1) {
foreach ($primary as $prim) {
if ($prim === $fk['field']) {
return true;
}
}
}
return false;
}
//does this table have exactly two foreign keys that are also NOT primary,
//and no tables in the database refer to this table?
private function detectManyToMany($prep, $table)
{
$properties = $prep[$table];
$foreignKeys = $properties['foreign'];
$primaryKeys = $properties['primary'];
//ensure we only have two foreign keys
if (count($foreignKeys) === 2) {
//ensure our foreign keys are not also defined as primary keys
$primaryKeyCountThatAreAlsoForeignKeys = 0;
foreach ($foreignKeys as $foreign) {
foreach ($primaryKeys as $primary) {
if ($primary === $foreign['name']) {
++$primaryKeyCountThatAreAlsoForeignKeys;
}
}
}
if ($primaryKeyCountThatAreAlsoForeignKeys === 1) {
//one of the keys foreign keys was also a primary key
//this is not a many to many. (many to many is only possible when both or none of the foreign keys are also primary)
return false;
}
//ensure no other tables refer to this one
foreach ($prep as $compareTable => $properties) {
if ($table !== $compareTable) {
foreach ($properties['foreign'] as $prop) {
if ($prop['on'] === $table) {
return false;
}
}
}
}
//this is a many to many table!
return true;
}
return false;
}
private function initializeSchemaGenerator()
{
$this->schemaGenerator = new SchemaGenerator(
$this->option('connection'),
null,
null
);
return $this->schemaGenerator;
}
/**
* Fetch the template data.
*
* @return array
*/
protected function getTemplateData()
{
return [
'NAME' => ucwords($this->argument('modelName')),
'NAMESPACE' => env('APP_NAME','App\Models'),
];
}
/**
* The path to where the file will be created.
*
* @return mixed
*/
protected function getFileGenerationPath()
{
$path = $this->getPathByOptionOrConfig('path', 'model_target_path');
if(!is_dir($path)) {
$this->warn('Path is not a directory, creating ' . $path);
mkdir($path);
}
return $path;
}
/**
* Get the path to the generator template.
*
* @return mixed
*/
protected function getTemplatePath()
{
$tp = __DIR__.'/templates/model.txt';
return $tp;
}
}