From 4f992a9294f0754b228401e5025627bf53b73778 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Sun, 22 Dec 2024 11:02:31 -0300 Subject: [PATCH] Seed Generator --- app/src/Generator/Seed.php | 139 +++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 app/src/Generator/Seed.php diff --git a/app/src/Generator/Seed.php b/app/src/Generator/Seed.php new file mode 100644 index 0000000..4ce6bd5 --- /dev/null +++ b/app/src/Generator/Seed.php @@ -0,0 +1,139 @@ +log('Running generate seeds' . (($dryRun) ? ' [dry-run]' : ''), true, $io); + $tables = $this->tableRepository->getAll(); + foreach ($tables as $table) { + if (in_array($table, $this->skips)) { + continue; + } + $this->log("Table: {$table}"); + $filename = $this->buildFilename($table); + $this->log("Filename: {$filename}", $dryRun, $io); + $content = $this->buildFile($table); + $this->log("Content: {$content}"); + + if ($dryRun) { + $status = file_put_contents($filename, $content); + $this->log("Saved: " . var_export($status, true)); + } + } + $this->log("Total tables seeded: " . count($this->tableRepository->getAll()), true, $io); + } + + protected function buildFilename(string $table): string + { + $i = $this->tableRepository->getIndex($table); + $time = $this->startDate->add(new DateInterval("PT{$i}S")); + return implode(DIRECTORY_SEPARATOR, [ + $this->seedsPath, + "{$time->format('YmdHis')}_{$table}_seeder.php" + ]); + } + protected function buildFile(string $table): string + { + return implode(PHP_EOL, [ + $this->buildHeader(), + $this->buildClass($table), + "{", + $this->buildFunction($table), + '}', + '' + ]); + } + protected function buildHeader(): string + { + return "buildClassName($table)} extends AbstractSeed"; + } + protected function buildClassName(string $table): string + { + return str_replace(' ', '', ucwords(str_replace('_', ' ', $table))) . 'Seeder'; + } + protected function buildFunction(string $table): string + { + return implode(PHP_EOL, [ + "\tpublic function run(): void", + "\t{", + $this->buildData($table), + "", + $this->buildInitialSetup(), + "\t\t\$this->table('{$table}')", + "\t\t\t->insert(\$data)", + "\t\t\t->saveData();", + $this->buildFinalSetup(), + "\t}" + ]); + } + protected function buildData(string $table): string + { + $output = ["\t\t\$data = ["]; + $dataGenerator = $this->dataRepository->getAll($table); + foreach ($dataGenerator as $row) { + $output []= "\t\t\t["; + foreach ($row as $key => $value) { + if (is_bool($value)) { + $value = $value ? 1 : 0; + } + if (!ctype_digit("{$value}") and $value !== null) { + if (str_contains($value, "'")) { + $value = str_replace("'", "\'", $value); + } + $value = "'{$value}'"; + } + if ($value === null) { + $value = 'null'; + } + if (strlen($value) > 2 and str_starts_with($value, '0')) { + $value = "'{$value}'"; + } + $output []= "\t\t\t\t'{$key}' => {$value},"; + } + $output []= "\t\t\t],"; + } + $output []= "\t\t];"; + $this->logger->debug("Total data: {$this->dataRepository->size}"); + return implode(PHP_EOL, $output); + } + protected function buildInitialSetup(): string + { + return "\t\t\$this->execute('SET unique_checks=0; SET foreign_key_checks=0;');"; + } + protected function buildFinalSetup(): string + { + return "\t\t\$this->execute('SET unique_checks=1; SET foreign_key_checks=1;');"; + } +} \ No newline at end of file