1 <?php
2
3 namespace Scaffolder\Compilers\Core;
4
5 use Carbon\Carbon;
6 use Illuminate\Support\Facades\File;
7 use Scaffolder\Compilers\AbstractCoreCompiler;
8 use Scaffolder\Support\FileToCompile;
9 use Scaffolder\Support\PathParser;
10 use stdClass;
11
12 class MigrationCompiler extends AbstractCoreCompiler
13 {
14 15 16 17
18 private $date;
19
20 21 22
23 public function __construct()
24 {
25 $this->date = Carbon::now();
26 }
27
28 29 30 31 32 33 34 35 36 37 38 39 40
41 public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, array $extensions, $extra = null)
42 {
43
44 $this->date->addSeconds(5);
45
46 if (File::exists(base_path('scaffolder-config/cache/migration_' . $hash . self::CACHE_EXT)))
47 {
48 return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
49 }
50 else
51 {
52 $this->stub = $stub;
53
54 $this->replaceClassName($modelName)
55 ->replaceTableName($scaffolderConfig, $modelName)
56 ->addFields($modelData);
57
58 foreach ($extensions as $extension)
59 {
60 $this->stub = $extension->runAfterMigrationIsCompiled($this->stub, $modelData, $scaffolderConfig);
61 }
62
63 return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
64 }
65 }
66
67 68 69 70 71 72 73 74 75 76
77 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
78 {
79 $path = PathParser::parse($scaffolderConfig->paths->migrations) . $this->date->format('Y_m_d_His') . '_create_' . strtolower($modelName) . 's_table.php';
80
81
82 if ($fileToCompile->cached)
83 {
84 File::copy(base_path('scaffolder-config/cache/migration_' . $fileToCompile->hash . self::CACHE_EXT), $path);
85 }
86 else
87 {
88 File::put(base_path('scaffolder-config/cache/migration_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
89 File::copy(base_path('scaffolder-config/cache/migration_' . $fileToCompile->hash . self::CACHE_EXT), $path);
90 }
91
92 return $path;
93 }
94
95 96 97 98 99 100 101 102
103 private function replaceTableName(stdClass $scaffolderConfig, $modelName)
104 {
105 $tableName = isset($scaffolderConfig->tableName) && !empty($scaffolderConfig->tableName) ? $scaffolderConfig->tableName : $modelName . 's';
106
107 $this->stub = str_replace('{{table_name}}', strtolower($tableName), $this->stub);
108
109 return $this;
110 }
111
112 113 114 115 116 117 118
119 private function addFields($modelData)
120 {
121
122 $fields = $this->tab(3) . "\$table->increments('id');" . PHP_EOL . PHP_EOL;
123
124
125 foreach ($modelData->fields as $field)
126 {
127 if ($field->index == 'primary')
128 {
129 $fields = '';
130 break;
131 }
132 }
133
134 foreach ($modelData->fields as $field)
135 {
136 $parsedModifiers = '';
137
138
139 if (!empty($field->modifiers))
140 {
141 $modifiersArray = explode(':', $field->modifiers);
142
143 foreach ($modifiersArray as $modifier)
144 {
145 $modifierAndValue = explode(',', $modifier);
146
147 if (count($modifierAndValue) == 2)
148 {
149 $parsedModifiers .= '->' . $modifierAndValue[0] . '(' . $modifierAndValue[1] . ')';
150 }
151 else
152 {
153 $parsedModifiers .= '->' . $modifierAndValue[0] . '()';
154 }
155 }
156 }
157
158
159 if ($field->index != 'none')
160 {
161 $fields .= sprintf($this->tab(3) . "\$table->%s('%s')%s->%s();" . PHP_EOL, $field->type->db, $field->name, $parsedModifiers, $field->index);
162 }
163 else
164 {
165 $fields .= sprintf($this->tab(3) . "\$table->%s('%s')%s;" . PHP_EOL, $field->type->db, $field->name, $parsedModifiers);
166 }
167
168
169 if (!empty($field->foreignKey))
170 {
171 $foreignKey = explode(':', $field->foreignKey);
172 $fields .= sprintf($this->tab(3) . "\$table->foreign('%s')->references('%s')->on('%s');" . PHP_EOL . PHP_EOL, $field->name, $foreignKey[0], $foreignKey[1]);
173 }
174 }
175
176 $fields .= PHP_EOL . $this->tab(3) . "\$table->timestamps();" . PHP_EOL;
177
178 $this->stub = str_replace('{{fields}}', $fields, $this->stub);
179
180 return $this;
181 }
182 }
183