commandData = $commandData; $this->path = database_path('migrations/'); } public function generate() { $templateData = $this->generatorHelpers->get_template('Migration.migration'); $templateData = $this->generatorHelpers->fill_template($this->commandData->dynamicVars, $templateData); $templateData = str_replace('$FIELDS$', $this->generateFields(), $templateData); $tableName = $this->commandData->dynamicVars['$TABLE_NAME$']; $fileName = date('Y_m_d_His').'_'.'create_'.Str::snake(Str::plural($tableName)).'_table.php'; FileUtil::createFile($this->path, $fileName, $templateData); $this->commandData->commandComment("\nMigration created: "); $this->commandData->commandInfo($fileName); } private function generateFields() { $fields = []; $foreignKeys = []; $createdAtField = null; $updatedAtField = null; $fields[] = '$table->id();'; foreach ($this->commandData->fields as $field) { $fields[] = $field->migrationText; if (!empty($field->foreignKeyText)) { $foreignKeys[] = $field->foreignKeyText; } } $fields[] = '$table->timestamps();'; if ($this->commandData->getOption('softDelete')) { $fields[] = '$table->softDeletes();'; } return implode($this->generatorHelpers->generator_nl_tab(1, 3), array_merge($fields, $foreignKeys)); } public function rollback() { $fileName = 'create_'.$this->commandData->config->tableName.'_table.php'; $allFiles = File::allFiles($this->path); $files = []; foreach ($allFiles as $file) { $files[] = $file->getFilename(); } $files = array_reverse($files); foreach ($files as $file) { if (Str::contains($file, $fileName)) { if ($this->rollbackFile($this->path, $file)) { $this->commandData->commandComment('Migration file deleted: '.$file); } break; } } } }