|
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
|
3 |
|
public function __construct(string $directory) |
|
33
|
|
|
{ |
|
34
|
3 |
|
$this->directory = $directory; |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creating migration objects and sorting versions |
|
39
|
|
|
* |
|
40
|
|
|
* @return Promise<array<string, \ServiceBus\Storage\Sql\Migration\Migration>> |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function load(): Promise |
|
43
|
|
|
{ |
|
44
|
3 |
|
return call( |
|
45
|
|
|
function (): \Generator |
|
46
|
|
|
{ |
|
47
|
3 |
|
$migrations = []; |
|
48
|
|
|
|
|
49
|
|
|
/** @var \SplFileInfo[] $files */ |
|
50
|
3 |
|
$files = yield $this->loadFiles(); |
|
51
|
|
|
|
|
52
|
3 |
|
foreach ($files as $file) |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* @psalm-suppress UnresolvableInclude |
|
56
|
|
|
* @noinspection PhpIncludeInspection |
|
57
|
|
|
*/ |
|
58
|
3 |
|
include_once (string) $file; |
|
59
|
|
|
|
|
60
|
|
|
/** @var string $name */ |
|
61
|
3 |
|
$name = $file->getBasename('.' . $file->getExtension()); |
|
62
|
3 |
|
$version = (string) \substr($name, 7); |
|
63
|
|
|
|
|
64
|
|
|
/** @var class-string<Migration> $class */ |
|
65
|
3 |
|
$class = \sprintf('\%s', $name); |
|
66
|
|
|
|
|
67
|
3 |
|
$migration = new $class; |
|
68
|
|
|
|
|
69
|
3 |
|
if ($migration instanceof Migration) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$migrations[$version] = $migration; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
\ksort($migrations); |
|
76
|
|
|
|
|
77
|
3 |
|
return $migrations; |
|
78
|
3 |
|
} |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Getting list of migration files |
|
84
|
|
|
* |
|
85
|
|
|
* @return Promise<\SplFileInfo[]> |
|
86
|
|
|
*/ |
|
87
|
3 |
|
private function loadFiles(): Promise |
|
88
|
|
|
{ |
|
89
|
3 |
|
return call( |
|
90
|
|
|
function (): \Generator |
|
91
|
|
|
{ |
|
92
|
|
|
/** @var string[] $files */ |
|
93
|
3 |
|
$files = yield scandir($this->directory); |
|
94
|
|
|
|
|
95
|
3 |
|
return \array_filter( |
|
96
|
3 |
|
\array_map( |
|
97
|
|
|
function (string $fileName): ?\SplFileInfo |
|
98
|
|
|
{ |
|
99
|
3 |
|
return \strpos($fileName, 'Version') !== false |
|
100
|
3 |
|
? new \SplFileInfo($this->directory . '/' . $fileName) |
|
101
|
3 |
|
: null; |
|
102
|
3 |
|
}, |
|
103
|
3 |
|
$files |
|
104
|
|
|
) |
|
105
|
|
|
); |
|
106
|
3 |
|
} |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|