Implement loading existing migration classes from the filesystem
[MAILPOET-4466]
This commit is contained in:
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
namespace MailPoet\Migrator;
|
namespace MailPoet\Migrator;
|
||||||
|
|
||||||
|
use RecursiveDirectoryIterator;
|
||||||
|
use RecursiveIteratorIterator;
|
||||||
|
use SplFileInfo;
|
||||||
|
|
||||||
class Repository {
|
class Repository {
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $migrationsDir;
|
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 {
|
private function generateName(): string {
|
||||||
return 'Migration_' . gmdate('Ymd_his');
|
return 'Migration_' . gmdate('Ymd_his');
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,18 @@ class RepositoryTest extends MailPoetUnitTest {
|
|||||||
$repository->create();
|
$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() {
|
public function _after() {
|
||||||
parent::_after();
|
parent::_after();
|
||||||
$this->removeDir(self::MIGRATIONS_OUTPUT_DIR);
|
$this->removeDir(self::MIGRATIONS_OUTPUT_DIR);
|
||||||
|
1
mailpoet/tests/unit/Migrator/TestMigrations/.dotfile
Normal file
1
mailpoet/tests/unit/Migrator/TestMigrations/.dotfile
Normal file
@ -0,0 +1 @@
|
|||||||
|
This file should not be loaded by migrations repository.
|
10
mailpoet/tests/unit/Migrator/TestMigrations/Migration_1.php
Normal file
10
mailpoet/tests/unit/Migrator/TestMigrations/Migration_1.php
Normal 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 {
|
||||||
|
}
|
||||||
|
}
|
10
mailpoet/tests/unit/Migrator/TestMigrations/Migration_2.php
Normal file
10
mailpoet/tests/unit/Migrator/TestMigrations/Migration_2.php
Normal 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 {
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
This file should not be loaded by migrations repository.
|
Reference in New Issue
Block a user