/
Wrapper.php
167 lines (156 loc) · 5.06 KB
/
Wrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Lucinda\Migration;
/**
* Performs migration process based on XML received
*/
class Wrapper
{
private string $folder;
/**
* @var array<string,Script>
*/
private array $instances = [];
private Cache $cache;
/**
* Sets up Cache and Script instances based on migration path received
*
* @param string $folder
* @param Cache $cache
* @throws Exception
*/
public function __construct(string $folder, Cache $cache)
{
// sets folder that stores migrations
if (!file_exists($folder) || !is_dir($folder)) {
throw new Exception("Migration folder not found: ".$folder);
}
$this->folder = $folder;
// sets cache
$this->cache = $cache;
// sets migration classes instances
$files = scandir($folder);
foreach ($files as $classPath) {
if (in_array($classPath, [".", ".."])) {
continue;
}
include_once $folder."/".$classPath;
$className = str_replace(".php", "", $classPath);
$object = new $className();
if (!($object instanceof Script)) {
throw new Exception($className." must be instanceof \Lucinda\Migration\Script");
}
$this->instances[$className] = $object;
}
}
/**
* Generates a migration class
*
* @return string
*/
public function generate(): string
{
$className = "Version".date("YmdHis");
$contents = str_replace(
"TemplateScript",
$className,
file_get_contents(dirname(__DIR__)."/templates/TemplateScript.php")
);
file_put_contents($this->folder."/".$className.".php", $contents);
return $className;
}
/**
* Runs all remaining migration Script-s (those whose Status is PENDING/FAILED) and returns results
*
* @return Result[]
*/
public function migrate(): array
{
$output = [];
$cacheEntries = $this->cache->read();
foreach ($this->instances as $className=>$instance) {
if (!isset($cacheEntries[$className]) || $cacheEntries[$className]==Status::FAILED) {
$result = $this->goUp($className, $instance);
$output[] = $result;
if ($result->getThrowable()) {
break;
}
}
}
return $output;
}
/**
* Runs DOWN (roll back) command migration Script (whose Status is PASSED) and returns result
*
* @param string $className
* @return ?Result
* @throws Exception
*/
public function down(string $className): ?Result
{
if (!isset($this->instances[$className])) {
throw new Exception($className." not known!");
}
$cacheEntries = $this->cache->read();
if (isset($cacheEntries[$className]) && $cacheEntries[$className]==Status::PASSED) {
return $this->goDown($className, $this->instances[$className]);
}
throw new Exception("Migration class not found or not in PASSED state");
}
/**
* Runs UP (commit) command migration Script (whose Status is PENDING/FAILED) and returns result
*
* @param string $className
* @return ?Result
* @throws Exception
*/
public function up(string $className): ?Result
{
if (!isset($this->instances[$className])) {
throw new Exception($className." not known!");
}
$cacheEntries = $this->cache->read();
if (!isset($cacheEntries[$className]) || $cacheEntries[$className]==Status::FAILED) {
return $this->goUp($className, $this->instances[$className]);
}
throw new Exception("Migration class already in PASSED state");
}
/**
* Executes UP (commit) command on Script, updates Cache and returns Result
*
* @param string $className
* @param Script $instance
* @return Result
*/
private function goUp(string $className, Script $instance): Result
{
try {
$instance->up();
$this->cache->add($className, Status::PASSED);
return new Result($className, Status::PASSED);
} catch (\Throwable $throwable) {
$this->cache->add($className, Status::FAILED);
$result = new Result($className, Status::FAILED);
$result->setThrowable($throwable);
return $result;
}
}
/**
* Executes DOWN (roll back) command on Script, updates Cache and returns Result
*
* @param string $className
* @param Script $instance
* @return Result
*/
private function goDown(string $className, Script $instance): Result
{
try {
$instance->down();
$this->cache->remove($className);
return new Result($className, Status::PASSED);
} catch (\Throwable $throwable) {
$result = new Result($className, Status::FAILED);
$result->setThrowable($throwable);
return $result;
}
}
}