Implement loading existing migration classes from the filesystem

[MAILPOET-4466]
This commit is contained in:
Jan Jakes
2022-10-27 16:04:16 +02:00
committed by Aschepikov
parent 516c460ace
commit e9970f3cc8
8 changed files with 73 additions and 0 deletions

View File

@ -2,6 +2,10 @@
namespace MailPoet\Migrator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
class Repository {
/** @var string */
private $migrationsDir;
@ -29,6 +33,21 @@ class Repository {
}
}
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() && strtolower($file->getExtension()) === 'php') {
$migrations[] = $file->getBasename('.' . $file->getExtension());
}
}
sort($migrations);
return $migrations;
}
private function generateName(): string {
return 'Migration_' . gmdate('Ymd_his');
}

View File

@ -55,6 +55,18 @@ class RepositoryTest extends MailPoetUnitTest {
$repository->create();
}
public function testItLoadsMigrationFiles(): void {
$repository = $this->make(Repository::class, [
'migrationsDir' => __DIR__ . '/TestMigrations',
]);
$this->assertSame([
'Migration_1',
'Migration_2',
'Migration_3',
'Migration_4',
], $repository->loadAll());
}
public function _after() {
parent::_after();
$this->removeDir(self::MIGRATIONS_OUTPUT_DIR);

View File

@ -0,0 +1 @@
This file should not be loaded by migrations repository.

View File

@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace MailPoet\Migrations;
use MailPoet\Migrator\Migration;
class Migration_1 extends Migration {
public function run(): void {
}
}

View File

@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace MailPoet\Migrations;
use MailPoet\Migrator\Migration;
class Migration_2 extends Migration {
public function run(): void {
}
}

View File

@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace MailPoet\Migrations;
use MailPoet\Migrator\Migration;
class Migration_3 extends Migration {
public function run(): void {
}
}

View File

@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace MailPoet\Migrations;
use MailPoet\Migrator\Migration;
class Migration_4 extends Migration {
public function run(): void {
}
}

View File

@ -0,0 +1 @@
This file should not be loaded by migrations repository.