1
|
|
|
<?php |
2
|
|
|
namespace Prateekkarki\Laragen\Generators\Common; |
3
|
|
|
|
4
|
|
|
use Prateekkarki\Laragen\Generators\BaseGenerator; |
5
|
|
|
use Prateekkarki\Laragen\Generators\GeneratorInterface; |
6
|
|
|
use Prateekkarki\Laragen\Models\DataOption; |
7
|
|
|
|
8
|
|
|
class Migration extends BaseGenerator implements GeneratorInterface |
9
|
|
|
{ |
10
|
|
|
protected static $counter = 0; |
11
|
|
|
|
12
|
|
|
public function generate() |
13
|
|
|
{ |
14
|
|
|
$generatedFiles = []; |
15
|
|
|
$migrationTemplate = $this->buildTemplate('common/migrations/table', [ |
16
|
|
|
'{{modelName}}' => $this->module->getModelName(), |
17
|
|
|
'{{modelNamePlural}}' => $this->module->getModelNamePlural(), |
18
|
|
|
'{{moduleName}}' => $this->module->getModuleName(), |
19
|
|
|
'{{modelTableSchema}}' => $this->getSchema() |
20
|
|
|
]); |
21
|
|
|
|
22
|
|
|
$fullFilePath = $this->getMigrationFile(); |
23
|
|
|
file_put_contents($fullFilePath, $migrationTemplate); |
24
|
|
|
$generatedFiles[] = $fullFilePath; |
25
|
|
|
|
26
|
|
|
foreach ($this->module->getGalleries() as $gallery) { |
27
|
|
|
$pivotTemplate = $this->buildTemplate('common/migrations/pivot', [ |
28
|
|
|
'{{pivotName}}' => $this->module->getPivotName($gallery), |
29
|
|
|
'{{pivotTableName}}' => $this->module->getPivotTableName($gallery), |
30
|
|
|
'{{pivotTableSchema}}' => $this->getGallerySchema($gallery) |
31
|
|
|
]); |
32
|
|
|
$pivotFilePath = $this->getPivotFile($gallery); |
33
|
|
|
file_put_contents($pivotFilePath, $pivotTemplate); |
34
|
|
|
$generatedFiles[] = $pivotFilePath; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
foreach ($this->module->getForeignColumns('related') as $relatedModules) { |
38
|
|
|
foreach ($relatedModules as $related) { |
39
|
|
|
$pivotTemplate = $this->buildTemplate('common/migrations/pivot', [ |
40
|
|
|
'{{pivotName}}' => $this->module->getPivotName($related), |
41
|
|
|
'{{pivotTableName}}' => $this->module->getPivotTableName($related), |
42
|
|
|
'{{pivotTableSchema}}' => $this->getPivotSchema($related) |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
$pivotFilePath = $this->getPivotFile($related); |
|
|
|
|
46
|
|
|
file_put_contents($pivotFilePath, $pivotTemplate); |
|
|
|
|
47
|
|
|
$generatedFiles[] = $pivotFilePath; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
foreach ($this->module->getMultipleColumns() as $multipleModules) { |
51
|
|
|
foreach ($multipleModules as $multiple => $multipleData) { |
52
|
|
|
$pivotTemplate = $this->buildTemplate('common/migrations/pivot', [ |
53
|
|
|
'{{pivotName}}' => str_singular($this->module->getPivotName($multiple)), |
|
|
|
|
54
|
|
|
'{{pivotTableName}}' => str_plural($this->module->getPivotTableName($multiple)), |
|
|
|
|
55
|
|
|
'{{pivotTableSchema}}' => $this->getMultipleSchema($multipleData) |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$pivotFilePath = $this->getPivotFile($multiple); |
59
|
|
|
file_put_contents($pivotFilePath, $pivotTemplate); |
60
|
|
|
$generatedFiles[] = $pivotFilePath; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $generatedFiles; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getMigrationFile() |
68
|
|
|
{ |
69
|
|
|
$fileCounter = sprintf('%06d', (int) date('His') + ++self::$counter); |
70
|
|
|
$filenamePrefix = date('Y_m_d_').$fileCounter."_"; |
71
|
|
|
$fileName = "create_".$this->module->getModuleName()."_table.php"; |
72
|
|
|
|
73
|
|
|
$existingFiles = scandir($this->getPath("database/migrations/")); |
74
|
|
|
|
75
|
|
|
foreach ($existingFiles as $file) { |
76
|
|
|
if (strpos($file, $fileName) !== false) { |
77
|
|
|
$filenamePrefix = str_replace($fileName, "", $file); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return $this->getPath("database/migrations/").$filenamePrefix.$fileName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function getPivotFile($related) |
84
|
|
|
{ |
85
|
|
|
$fileCounter = sprintf('%06d', (int) date('His') + ++self::$counter); |
86
|
|
|
$filenamePrefix = date('Y_m_d_').$fileCounter."_"; |
87
|
|
|
$fileName = "create_".str_singular($this->module->getPivotTableName($related))."_table.php"; |
|
|
|
|
88
|
|
|
|
89
|
|
|
$existingFiles = scandir($this->getPath("database/migrations/")); |
90
|
|
|
|
91
|
|
|
foreach ($existingFiles as $file) { |
92
|
|
|
if (strpos($file, $fileName) !== false) { |
93
|
|
|
$filenamePrefix = str_replace($fileName, "", $file); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->getPath("database/migrations/").$filenamePrefix.$fileName; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function getMultipleSchema($multipleData) |
101
|
|
|
{ |
102
|
|
|
$schema = '$table->bigInteger("'.$this->module->getModelNameLowercase().'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3); |
103
|
|
|
$schema .= "\$table->foreign('".$this->module->getModelNameLowercase()."_id')->references('id')->on('".$this->module->getModulename()."')->onDelete('set null');"; |
104
|
|
|
|
105
|
|
|
foreach($multipleData as $column => $optionString){ |
106
|
|
|
$option = new DataOption($column, $optionString); |
107
|
|
|
$schema .= $option->getSchema(); |
108
|
|
|
$schema .= ''.PHP_EOL.$this->getTabs(3); |
109
|
|
|
} |
110
|
|
|
return $schema; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function getPivotSchema($related) |
114
|
|
|
{ |
115
|
|
|
$schema = '$table->bigInteger("'.$this->module->getModelNameLowercase().'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3); |
116
|
|
|
$schema .= "\$table->foreign('".$this->module->getModelNameLowercase()."_id')->references('id')->on('".$this->module->getModulename()."')->onDelete('set null');"; |
117
|
|
|
|
118
|
|
|
$schema .= '$table->bigInteger("'.str_singular($related).'_id")->unsigned()->nullable();'; |
|
|
|
|
119
|
|
|
$schema .= "\$table->foreign('".str_singular($related)."_id')->references('id')->on('".$related."')->onDelete('set null');"; |
|
|
|
|
120
|
|
|
|
121
|
|
|
return $schema; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getGallerySchema($gallery) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$schema = '$table->bigInteger("'.$this->module->getModelNameLowercase().'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3); |
127
|
|
|
$schema .= "\$table->foreign('".$this->module->getModelNameLowercase()."_id')->references('id')->on('".$this->module->getModulename()."')->onDelete('set null');"; |
128
|
|
|
$schema .= '$table->string("filename", 128);'; |
129
|
|
|
$schema .= '$table->timestamps();'; |
130
|
|
|
return $schema; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function getSchema() |
134
|
|
|
{ |
135
|
|
|
$schema = ""; |
136
|
|
|
$keyArray = array_keys($this->module->getData()); |
137
|
|
|
$lastColumn = array_pop($keyArray); |
138
|
|
|
|
139
|
|
|
foreach ($this->module->getData() as $column => $optionString) { |
140
|
|
|
$option = new DataOption($column, $optionString); |
141
|
|
|
|
142
|
|
|
$schema .= $option->getSchema(); |
143
|
|
|
|
144
|
|
|
if ($column != $lastColumn) { |
145
|
|
|
$schema .= PHP_EOL.$this->getTabs(3); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $schema; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|