Passed
Push — master ( 472045...d88b3b )
by Prateek
10:54 queued 06:51
created

Migration::getGallerySchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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