-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.php
162 lines (134 loc) · 4.99 KB
/
install.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
<?php
declare(strict_types=1);
use Game\Engine\DBConnection;
final class Installer
{
private const DIR_LIB = __DIR__ . '/vendor';
private const FILE_LIB_LOADER = self::DIR_LIB . '/autoload.php';
private const DIR_DATASOURCE = __DIR__ . '/updates';
private const LOCK_FILE = __DIR__ . '/installed';
private const LOCK_FILE_TMP = self::LOCK_FILE . '.tmp';
private const FILE_CONFIG = __DIR__ . '/config.php';
/**
* @var array{dbHost:string, dbName: string, dbUser: string, dbPass:string}
*/
private array $config;
private string $currentVersion = '';
public function __construct()
{
if (realpath(__DIR__ . '/install.php') !== __FILE__) {
throw new RuntimeException('Attempted to perform installation from unexpected context');
}
$this->config = require self::FILE_CONFIG;
}
public function run(): void
{
if ($this->isRunning()) {
throw new RuntimeException('Installation is in progress');
}
$tmpLock = fopen(self::LOCK_FILE_TMP, 'w');
if ($tmpLock === false) {
throw new RuntimeException("Couldn't start installation");
}
if (!flock($tmpLock, LOCK_EX)) {
fclose($tmpLock);
throw new RuntimeException('Installation has already been performed');
}
try {
$this->installDependencies();
$this->installDatasource();
file_put_contents(self::LOCK_FILE, $this->currentVersion);
} finally {
fclose($tmpLock);
unlink(self::LOCK_FILE_TMP);
}
}
private function isRunning(): bool
{
return file_exists(self::LOCK_FILE_TMP);
}
private function installDependencies(): void
{
$output = [];
$resultCode = 0;
exec('php composer.phar install --no-interaction', $output, $resultCode);
if ($resultCode !== 0) {
throw new RuntimeException(implode(PHP_EOL, $output));
}
require_once self::FILE_LIB_LOADER;
}
private function installDatasource(): void
{
if (!$this->dependenciesAreInstalled()) {
throw new RuntimeException('Dependencies shall be installed first');
}
$db = new DBConnection(
$this->config['dbHost'],
$this->config['dbName'],
$this->config['dbUser'],
$this->config['dbPass']
);
/**
* @var SplFileInfo $file
*/
foreach (new DirectoryIterator(self::DIR_DATASOURCE) as $file) {
if (!$file->isFile() || $file->getExtension() !== 'sql') {
continue;
}
$dataSources[$file->getBasename('.sql')] = $file->getRealPath();
}
ksort($dataSources, SORT_NATURAL);
$isUpdate = [] !== $db->fetchRow("SHOW TABLES LIKE 'version'");
if ($isUpdate) {
$currentVersionDetails = $db->fetchRow('SELECT current from version LIMIT 1');
if ($currentVersionDetails === []) {
throw new RuntimeException('Version system does not contain current version information!');
}
$this->currentVersion = $currentVersionDetails['current'];
}
foreach ($dataSources as $sourceVersion => $dataSource) {
$sourceVersion = (string)$sourceVersion;
if ($this->isAlreadyInstalled($sourceVersion)) {
continue;
}
$migration = file_get_contents($dataSource);
// Implicit migrations break transaction and thus there is no reason to attempt performing it within
$containsImplicitCommit = preg_match('/\b(?:ALTER TABLE|DROP TABLE|CREATE TABLE)\b/i', $migration) === 1;
if ($containsImplicitCommit) {
$this->runMigration($db, $sourceVersion, $migration);
} else {
$db->transaction(fn(DBConnection $dbConnection) => $this->runMigration($dbConnection, $sourceVersion, $migration));
}
}
}
private function dependenciesAreInstalled(): bool
{
return file_exists(self::FILE_LIB_LOADER);
}
private function isAlreadyInstalled(string $version): bool
{
return strnatcmp($version, $this->currentVersion) <= 0;
}
private function runMigration(DBConnection $db, string $version, string $migration): void
{
foreach (explode(';' . PHP_EOL, $migration) as $query) {
$query = trim($query);
if ($query !== '') {
$db->execute($query);
}
}
if ([] !== $db->fetchRow('SELECT * from version LIMIT 1')) {
$db->execute('UPDATE version SET current=? LIMIT 1', [$version]);
} else {
$db->execute('INSERT INTO version(current) VALUE (?)', [$version]);
}
$this->currentVersion = $version;
}
}
try {
(new Installer())->run();
} catch (RuntimeException $e) {
echo $e->getMessage() . PHP_EOL;
echo $e->getTraceAsString() . PHP_EOL;
exit(1);
}