-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstaller.php
213 lines (176 loc) · 7.12 KB
/
Installer.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Module Name: RdbAdmin module.
* Description: The administrator module for RundizBones framework.
* Requires PHP: 7.4.0
* Requires Modules: Languages
* Author: Vee W.
* Gettext Domain: rdbadmin
*
* @package RdbAdmin
* @version 1.2.12dev-20241226
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Rdb\Modules\RdbAdmin;
/**
* Module installer class for RdbAdmin.
*
* @since 0.1
*/
class Installer implements \Rdb\System\Interfaces\ModuleInstaller
{
/**
* @var \Rdb\System\Container
*/
protected $Container;
/**
* @var \Rdb\System\Libraries\Db
*/
protected $Db;
/**
* @var \Rdb\System\Libraries\Logger
*/
protected $Logger;
/**
* {@inheritDoc}
*/
public function __construct(\Rdb\System\Container $Container)
{
$this->Container = $Container;
if ($this->Container->has('Db')) {
$this->Db = $this->Container->get('Db');
} else {
$this->Db = new \Rdb\System\Libraries\Db($Container);
}
if ($this->Container->has('Logger')) {
$this->Logger = $this->Container->get('Logger');
} else {
$this->Logger = new \Rdb\System\Libraries\Logger($Container);
}
}// __construct
/**
* {@inheritDoc}
*/
public function install()
{
try {
$sqlString = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Installer.sql');
$expSql = explode(';' . "\n", str_replace(["\r\n", "\r", "\n"], "\n", $sqlString));
unset($sqlString);
if (is_array($expSql)) {
foreach ($expSql as $eachStatement) {
if (empty(trim($eachStatement))) {
continue;
}
$eachStatement = trim($eachStatement) . ';';
preg_match('/%\$(.[^ ]+)%/iu', $eachStatement, $matches);
if (isset($matches[1])) {
$tableName = $this->Db->tableName((string) $matches[1]);
}
unset($matches);
if (isset($tableName)) {
$eachStatement = preg_replace('/%\$(.[^ ]+)%/iu', $tableName, $eachStatement);
if (empty($eachStatement)) {
continue;
}
$this->Logger->write('modules/rdbadmin/installer', 0, $eachStatement);
$Sth = $this->Db->PDO()->prepare($eachStatement);
$execResult = $Sth->execute();
$Sth->closeCursor();
unset($Sth);
if ($execResult === true) {
$this->Db->convertCharsetAndCollation($tableName, null);
}
unset($execResult, $tableName);
}
}// endforeach;
unset($eachStatement);
}
unset($expSql);
} catch (\Exception $e) {
$this->Logger->write('modules/rdbadmin/installer', 3, $e->getMessage());
throw $e;
}
}// install
/**
* {@inheritDoc}
*/
public function uninstall()
{
try {
$sqlString = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Installer.sql');
// remove comments --------------------------------------------------------------------------
// @link https://regex101.com/r/GXb0a5/2 pattern original source code.
$pattern = '/["\'`][^"\'`]*(?!\\\\)["\'`](*SKIP)(*F) # Make sure we\'re not matching inside of quotes, double quotes or backticks
|(?m-s:\s*(?:\-{2}|\#)[^\n]*$) # Single line comment
|(?:
\/\*.*?\*\/ # Multi-line comment
(?(?=(?m-s:[\t ]+$)) # Get trailing whitespace if any exists and only if it\'s the rest of the line
[\t ]+
)
)/iusx';
$sqlString = preg_replace($pattern, '', $sqlString);
// end remove comments ---------------------------------------------------------------------
preg_match_all('/%\$(.[^ ]+)%/miu', $sqlString, $matches);
if (isset($matches[1]) && is_array($matches[1])) {
$tables = array_unique($matches[1]);
foreach ($tables as $table) {
$sql = 'DROP TABLE IF EXISTS `' . $this->Db->tableName($table) . '`;';
$stmt = $this->Db->PDO()->prepare($sql);
unset($sql);
$stmt->execute();
$stmt->closeCursor();
unset($stmt);
}// endforeach;
unset($table, $tables);
}
unset($sqlString);
} catch (\Exception $e) {
$this->Logger->write('modules/rdbadmin/installer', 3, $e->getMessage());
throw $e;
}
}// uninstall
/**
* {@inheritDoc}
*/
public function update()
{
try {
if (method_exists($this->Db, 'alterStructure')) {
$sqlString = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Installer.sql');
$sqlString = $this->Db->removeSQLComments($sqlString);
$expSql = explode(';' . "\n", str_replace(["\r\n", "\r", "\n"], "\n", $sqlString));
unset($sqlString);
if (is_array($expSql)) {
foreach ($expSql as $eachStatement) {
if (empty(trim($eachStatement))) {
continue;
}
$eachStatement = trim($eachStatement) . ';';
preg_match('/%\$(.[^ ]+)%/iu', $eachStatement, $matches);
if (isset($matches[1])) {
$tableName = $this->Db->tableName((string) $matches[1]);
}
unset($matches);
if (isset($tableName)) {
$eachStatement = preg_replace('/%\$(.[^ ]+)%/iu', $tableName, $eachStatement);
if (empty($eachStatement)) {
continue;
}
$this->Logger->write('modules/rdbadmin/installer', 0, $eachStatement);
$alterResults = $this->Db->alterStructure($eachStatement);
$this->Logger->write('modules/rdbadmin/installer', 0, 'Alter results: {alterResults}', ['alterResults' => $alterResults]);
$this->Db->convertCharsetAndCollation($tableName, null);
unset($alterResults, $tableName);
}
}// endforeach;
unset($eachStatement);
}// endif;
unset($expSql);
}// endif; `alterStructure` method exists
} catch (\Exception $e) {
$this->Logger->write('modules/rdbadmin/installer', 3, $e->getMessage());
throw $e;
}
}// update
}