Files
piratepoet/mailpoet/lib/Migrator/Repository.php
Rostislav Wolny 092fcb78d0 Rename Migration to DbMigration
We want to distinguish Db and App level migrations. This is the first step.
[MAILPOET-5416]
2023-07-26 14:01:39 +02:00

70 lines
1.8 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\Migrator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
class Repository {
/** @var string */
private $migrationsDir;
/** @var string */
private $templateFile;
public function __construct() {
$this->migrationsDir = __DIR__ . '/../Migrations';
$this->templateFile = __DIR__ . '/DbMigrationTemplate.php';
}
public function getMigrationsDir(): string {
return $this->migrationsDir;
}
/** @return array{name: string, path: string} */
public function create(): array {
$template = @file_get_contents($this->templateFile);
if (!$template) {
throw MigratorException::templateFileReadFailed($this->templateFile);
}
$name = $this->generateName();
$migration = str_replace('class DbMigrationTemplate ', "class $name ", $template);
$path = $this->migrationsDir . "/$name.php";
$result = @file_put_contents($path, $migration);
if (!$result) {
throw MigratorException::migrationFileWriteFailed($path);
}
return [
'name' => $name,
'path' => $path,
];
}
public function loadAll(): array {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->migrationsDir, RecursiveDirectoryIterator::SKIP_DOTS)
);
$migrations = [];
foreach ($files as $file) {
if (!$file instanceof SplFileInfo || !$file->isFile()) {
continue;
}
if (strtolower($file->getFilename()) === 'index.php') {
continue;
}
if (strtolower($file->getExtension()) === 'php') {
$migrations[] = $file->getBasename('.' . $file->getExtension());
}
}
sort($migrations);
return $migrations;
}
private function generateName(): string {
return 'Migration_' . gmdate('Ymd_His');
}
}