Seed Generator

This commit is contained in:
Juan Pablo Vial
2024-12-22 11:02:31 -03:00
parent 21c593c93b
commit 4f992a9294

139
app/src/Generator/Seed.php Normal file
View File

@ -0,0 +1,139 @@
<?php
namespace ProVM\Generator;
use DateTimeInterface;
use DateInterval;
use Psr\Log\LoggerInterface;
use ProVM\Concept;
use ProVM\Enforce;
use ProVM\Repository;
use Symfony\Component\Console\Style\StyleInterface;
class Seed extends Enforce\Generator
{
public function __construct(
protected Concept\Database $database,
protected Concept\Database\Connection $connection,
protected Concept\Database\Query\Builder $queryBuilder,
protected Repository\Table $tableRepository,
protected Repository\Data $dataRepository,
LoggerInterface $logger,
protected string $databaseName,
protected string $seedsPath,
protected DateTimeInterface $startDate,
protected array $skips)
{
parent::__construct($logger);
}
public function generate(StyleInterface $io, bool $dryRun = false): void
{
$this->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 "<?php
use Phinx\Seed\AbstractSeed;
";
}
protected function buildClass(string $table): string
{
return "class {$this->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;');";
}
}