migrationsDir = __DIR__ . '/../Migrations';
$this->templateFile = __DIR__ . '/{level}MigrationTemplate.php';
}
public function getMigrationsDir(): string {
return $this->migrationsDir;
}
/** @return array{name: string, path: string} */
public function create(string $level): array {
if (!in_array($level, [self::MIGRATIONS_LEVEL_APP, self::MIGRATIONS_LEVEL_DB], true)) {
throw MigratorException::invalidMigrationLevel($level);
}
$ucFirstLevel = ucfirst($level);
$templateFile = str_replace('{level}', $ucFirstLevel, $this->templateFile);
$template = @file_get_contents($templateFile);
if (!$template) {
throw MigratorException::templateFileReadFailed($templateFile);
}
$name = $this->generateName($level);
$migration = str_replace('{level}', $ucFirstLevel, 'class {level}MigrationTemplate ');
$migration = str_replace($migration, "class $name ", $template);
$path = "$this->migrationsDir/$ucFirstLevel/$name.php";
$result = @file_put_contents($path, $migration);
if (!$result) {
throw MigratorException::migrationFileWriteFailed($path);
}
return [
'name' => $name,
'path' => $path,
];
}
/**
* Array of migration filenames and types.
* Db migrations are loaded first, then app migrations. This ensures that Db migrator is run before app migrations
* @return array