|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PrismX\Generators\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use PrismX\Generators\Support\Model; |
|
8
|
|
|
use PrismX\Generators\Support\AbstractGenerator; |
|
9
|
|
|
|
|
10
|
|
|
class MigrationGenerator extends AbstractGenerator |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct(Model $model) |
|
13
|
|
|
{ |
|
14
|
|
|
parent::__construct($model); |
|
15
|
|
|
$this->stub = File::get(STUBS_PATH.'/migration.stub'); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function populateStub(): string |
|
19
|
|
|
{ |
|
20
|
|
|
$stub = $this->stub; |
|
21
|
|
|
$stub = str_replace('{{ClassName}}', $this->getClassName(), $stub); |
|
22
|
|
|
$stub = str_replace('{{TableName}}', $this->model->tableName(), $stub); |
|
23
|
|
|
$stub = str_replace('{{schema}}', $this->buildDefinition(), $stub); |
|
24
|
|
|
|
|
25
|
|
|
return $stub; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function buildDefinition() |
|
29
|
|
|
{ |
|
30
|
|
|
$definition = ''; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($this->model->columns() as $column) { |
|
33
|
|
|
$dataType = $column->dataType(); |
|
34
|
|
|
if ($column->name() === 'id') { |
|
35
|
|
|
$dataType = 'increments'; |
|
36
|
|
|
} elseif ($column->dataType() === 'id') { |
|
37
|
|
|
$dataType = 'unsignedBigInteger'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$definition .= self::INDENT.'$table->'.$dataType."('{$column->name()}'"; |
|
41
|
|
|
|
|
42
|
|
|
if (! empty($column->attributes()) && $column->dataType() !== 'id') { |
|
43
|
|
|
$definition .= ', '; |
|
44
|
|
|
if (in_array($column->dataType(), ['set', 'enum'])) { |
|
45
|
|
|
$definition .= json_encode($column->attributes()); |
|
46
|
|
|
} else { |
|
47
|
|
|
$definition .= implode(', ', $column->attributes()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
$definition .= ')'; |
|
51
|
|
|
|
|
52
|
|
|
foreach ($column->modifiers() as $modifier) { |
|
53
|
|
|
if (is_array($modifier)) { |
|
54
|
|
|
$definition .= '->'.key($modifier).'('.current($modifier).')'; |
|
55
|
|
|
} else { |
|
56
|
|
|
$definition .= '->'.$modifier.'()'; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$definition .= ';'.PHP_EOL; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($this->model->usesSoftDeletes()) { |
|
64
|
|
|
$definition .= self::INDENT.'$table->'.$this->model->softDeletesDataType().'();'.PHP_EOL; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->model->usesTimestamps()) { |
|
68
|
|
|
$definition .= self::INDENT.'$table->'.$this->model->timestampsDataType().'();'.PHP_EOL; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return trim($definition); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function getClassName() |
|
75
|
|
|
{ |
|
76
|
|
|
return 'Create'.Str::studly($this->model->tableName()).'Table'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getPath(): string |
|
80
|
|
|
{ |
|
81
|
|
|
$check = glob('database/migrations/*_create_'.$this->model->tableName().'_table.php'); |
|
82
|
|
|
|
|
83
|
|
|
return $check[0] ?? 'database/migrations/'.\Carbon\Carbon::now()->format('Y_m_d_His').'_create_'.$this->model->tableName().'_table.php'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|