1
|
|
|
<?php |
2
|
|
|
namespace Prateekkarki\Laragen\Generators\Common; |
3
|
|
|
|
4
|
|
|
use Prateekkarki\Laragen\Generators\BaseGenerator; |
5
|
|
|
use Prateekkarki\Laragen\Generators\GeneratorInterface; |
6
|
|
|
|
7
|
|
|
class Migration extends BaseGenerator implements GeneratorInterface |
8
|
|
|
{ |
9
|
|
|
protected static $counter = 0; |
10
|
|
|
|
11
|
|
|
private static $destination = "laragen/database/migrations"; |
12
|
|
|
private static $tableTemplate = "common/migrations/table"; |
13
|
|
|
private static $pivotTemplate = "common/migrations/pivot"; |
14
|
|
|
|
15
|
|
|
public function generate() |
16
|
|
|
{ |
17
|
|
|
if (self::$counter == 0 && is_dir(base_path(self::$destination."/"))) { |
18
|
|
|
$this->deleteFiles(base_path(self::$destination."/")); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$generatedFiles = []; |
22
|
|
|
foreach ($this->module->getFilteredColumns('needsTableInit') as $type) { |
23
|
|
|
$migrationTemplate = $this->buildTemplate(self::$pivotTemplate, [ |
24
|
|
|
'{{pivotName}}' => $type->getMigrationPivot(), |
25
|
|
|
'{{pivotTableName}}' => $type->getPivotTable(), |
26
|
|
|
'{{pivotTableSchema}}' => $type->getPivotSchema() |
27
|
|
|
]); |
28
|
|
|
|
29
|
|
|
$fullFilePath = $this->getPivotFile($type); |
30
|
|
|
file_put_contents($fullFilePath, $migrationTemplate); |
31
|
|
|
$generatedFiles[] = $fullFilePath; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$migrationTemplate = $this->buildTemplate(self::$tableTemplate, [ |
35
|
|
|
'{{modelName}}' => $this->module->getModelName(), |
36
|
|
|
'{{modelNamePlural}}' => $this->module->getModelNamePlural(), |
37
|
|
|
'{{moduleName}}' => $this->module->getModuleName(), |
38
|
|
|
'{{modelTableSchema}}' => $this->getSchema() |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$fullFilePath = $this->getMigrationFile(); |
42
|
|
|
file_put_contents($fullFilePath, $migrationTemplate); |
43
|
|
|
$generatedFiles[] = $fullFilePath; |
44
|
|
|
|
45
|
|
|
foreach ($this->module->getFilteredColumns(['hasPivot']) as $type) { |
46
|
|
|
$migrationTemplate = $this->buildTemplate(self::$pivotTemplate, [ |
47
|
|
|
'{{pivotName}}' => $type->getMigrationPivot(), |
48
|
|
|
'{{pivotTableName}}' => $type->getPivotTable(), |
49
|
|
|
'{{pivotTableSchema}}' => $type->getPivotSchema() |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$fullFilePath = $this->getPivotFile($type); |
53
|
|
|
file_put_contents($fullFilePath, $migrationTemplate); |
54
|
|
|
$generatedFiles[] = $fullFilePath; |
55
|
|
|
} |
56
|
|
|
return $generatedFiles; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function getMigrationFile() |
60
|
|
|
{ |
61
|
|
|
$fileCounter = sprintf('%06d', (int) date('His') + ++self::$counter); |
62
|
|
|
$filenamePrefix = date('Y_m_d_').$fileCounter."_"; |
63
|
|
|
$fileName = "create_".$this->module->getModuleName()."_table.php"; |
64
|
|
|
|
65
|
|
|
return $this->getPath(self::$destination."/").$filenamePrefix.$fileName; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getPivotFile($related) |
69
|
|
|
{ |
70
|
|
|
$fileCounter = sprintf('%06d', (int) date('His') + ++self::$counter); |
71
|
|
|
$filenamePrefix = date('Y_m_d_').$fileCounter."_"; |
72
|
|
|
$fileName = "create_".$related->getPivotTable()."_table.php"; |
73
|
|
|
|
74
|
|
|
return $this->getPath(self::$destination."/").$filenamePrefix.$fileName; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getSchema() |
78
|
|
|
{ |
79
|
|
|
$schema = ""; |
80
|
|
|
foreach ($this->module->getColumns(true) as $type) { |
81
|
|
|
|
82
|
|
|
$schema .= $type->getSchema(); |
83
|
|
|
|
84
|
|
|
if ($type->getColumn() != $this->module->getLastColumn()) { |
85
|
|
|
$schema .= PHP_EOL.$this->getTabs(3); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $schema; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|