1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SQL databases adapters implementation. |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\Storage\Sql\Migration; |
14
|
|
|
|
15
|
|
|
use Amp\Promise; |
16
|
|
|
use function Amp\call; |
17
|
|
|
use function Amp\File\scandir; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
final class SqlMigrationLoader |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
private $directory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* SqlMigrationLoader constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $directory |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $directory) |
33
|
|
|
{ |
34
|
|
|
$this->directory = $directory; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creating migration objects and sorting versions |
39
|
|
|
* |
40
|
|
|
* @return Promise<array<string, \ServiceBus\Storage\Sql\Migration\Migration>> |
41
|
|
|
*/ |
42
|
|
|
public function load(): Promise |
43
|
|
|
{ |
44
|
|
|
return call( |
45
|
|
|
function(): \Generator |
46
|
|
|
{ |
47
|
|
|
$migrations = []; |
48
|
|
|
|
49
|
|
|
/** @var \SplFileInfo[] $supportedFiles */ |
50
|
|
|
$supportedFiles = yield from $this->loadFiles(); |
51
|
|
|
|
52
|
|
|
foreach($supportedFiles as $file) |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* @psalm-suppress UnresolvableInclude |
56
|
|
|
* @noinspection PhpIncludeInspection |
57
|
|
|
*/ |
58
|
|
|
include_once (string) $file; |
59
|
|
|
|
60
|
|
|
/** @var string $name */ |
61
|
|
|
$name = $file->getBasename('.' . $file->getExtension()); |
62
|
|
|
$version = (string) \substr($name, 7); |
63
|
|
|
|
64
|
|
|
/** @psalm-var class-string<\ServiceBus\Storage\Sql\Migration\Migration> $class */ |
65
|
|
|
$class = \sprintf('\%s', $name); |
66
|
|
|
|
67
|
|
|
$migration = new $class; |
68
|
|
|
|
69
|
|
|
if(($migration instanceof Migration) === false) |
70
|
|
|
{ |
71
|
|
|
throw new \RuntimeException( |
72
|
|
|
\sprintf('Migration must extend `%s` class', Migration::class) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$migrations[$version] = new $class; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
\ksort($migrations); |
80
|
|
|
|
81
|
|
|
return $migrations; |
82
|
|
|
} |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Getting list of migration files |
88
|
|
|
* |
89
|
|
|
* @return Promise<\SplFileInfo[]> |
90
|
|
|
*/ |
91
|
|
|
private function loadFiles(): Promise |
92
|
|
|
{ |
93
|
|
|
return call( |
94
|
|
|
function(): \Generator |
95
|
|
|
{ |
96
|
|
|
/** @var string[] $files */ |
97
|
|
|
$files = yield scandir($this->directory); |
98
|
|
|
|
99
|
|
|
return \array_filter( |
100
|
|
|
\array_map( |
101
|
|
|
function(string $fileName): ?\SplFileInfo |
102
|
|
|
{ |
103
|
|
|
return \strpos($fileName, 'Version') !== false |
104
|
|
|
? new \SplFileInfo($this->directory . '/' . $fileName) |
105
|
|
|
: null; |
106
|
|
|
}, |
107
|
|
|
$files |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |