Repository navigation
Expand file tree
/
Copy pathPage_VersionController.php
More file actions
234 lines (186 loc) · 7.25 KB
/
Copy pathPage_VersionController.php
File metadata and controls
234 lines (186 loc) · 7.25 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* check current version need to upgrade
* User: anguoyue
* Date: 13/10/2018
* Time: 3:54 PM
*/
abstract class Page_VersionController extends UpgradeController
{
protected $needUpgrade = false;
protected $versions = [
10011 => "1.0.11",
10012 => "1.0.12",
10013 => "1.0.13",
];
abstract function doRequest();
public function index()
{
//set is latest version
$currentVersionCode = ZalyConfig::getConfig(ZalyConfig::$configSiteVersionCodeKey);
if (!is_numeric($currentVersionCode)) {
$currentVersionCode = 10011;
}
$latestVersionCode = ZalyConfig::getSampleConfig(ZalyConfig::$configSiteVersionCodeKey);
//check if need upgrade
if ($currentVersionCode < $latestVersionCode) {
$this->needUpgrade = true;
} else {
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET') {
$upgradeUrl = './index.php';
header("Location:" . $upgradeUrl);
exit;
}
}
$this->initUpgradeVersion();
$this->doRequest();
}
protected function initUpgradeVersion()
{
$siteVersion = [
"versionCode" => 10011, //默认第一个版本,从1.0.11版本开始支持升级
"versionName" => "",
"upgradeErrCode" => "",
"upgradeErrInfo" => "",
"passwordFileName" => $this->generatePasswordFileName(),//升级口令文件名称
];
$fileName = dirname(__FILE__) . "/../../upgrade.php";
if (!file_exists($fileName)) {
$contents = var_export($siteVersion, true);
file_put_contents($fileName, "<?php\n return {$contents};\n ");
$passwordFileName = dirname(__FILE__) . "/../../" . $siteVersion['passwordFileName'];
file_put_contents($passwordFileName, ZalyHelper::generateNumberKey());
} else {
//upgrade.php 存在,但是 ***.upgrade 不存在
$passwordFilePath = $this->getPasswordFilePath();
if (!file_exists($passwordFilePath)) {
$newPasswordFileName = $this->generatePasswordFileName();
$passwordFilePath = dirname(__FILE__) . "/../../" . $newPasswordFileName;
file_put_contents($passwordFilePath, ZalyHelper::generateNumberKey());
//update upgrade.php
$this->updateUpgradePasswordFileName($newPasswordFileName);
}
}
}
protected function getUpgradeVersion()
{
$fileName = dirname(__FILE__) . "/../../upgrade.php";
if (!file_exists($fileName)) {
$this->initUpgradeVersion();
}
$versionArrays = require($fileName);
return $versionArrays;
}
private function generatePasswordFileName()
{
$dateDir = date("Ymd");
$fileName = ZalyHelper::generateStrKey(10) . "-" . $dateDir . ".upgrade";
return $fileName;
}
protected function getPasswordFilePath()
{
$versionInfos = $this->getUpgradeVersion();
$fileName = $versionInfos['passwordFileName'];
$filePath = dirname(__FILE__) . "/../../" . $fileName;
return $filePath;
}
protected function getPasswordFileName()
{
$versionInfos = $this->getUpgradeVersion();
$fileName = $versionInfos['passwordFileName'];
return $fileName;
}
protected function deleteUpgradePasswordFile()
{
$fileName = $this->getPasswordFileName();
$passwordFileName = dirname(__FILE__) . "/../../" . $fileName;
$result = unlink($passwordFileName);
$this->logger->error("==================", "delete file=" . $passwordFileName . " result=" . $result);
}
/**
* @param $versionCode
* @param $versionName
* @param $upgradeErrCode success/doing/error
* @param $upgradeErrInfo
*/
protected function setUpgradeVersion($versionCode, $versionName, $upgradeErrCode, $upgradeErrInfo)
{
$siteVersion = [
"versionCode" => $versionCode,
"versionName" => $versionName,
"upgradeErrCode" => $upgradeErrCode,
"upgradeErrInfo" => $upgradeErrInfo,
"passwordFileName" => $this->getPasswordFileName(),//升级口令文件名称
];
$fileName = dirname(__FILE__) . "/../../upgrade.php";
$contents = var_export($siteVersion, true);
file_put_contents($fileName, "<?php\n return {$contents};\n ");
}
protected function setUpgradeErrInfo($upgradeErrCode, $upgradeErrInfo)
{
$currentVersion = $this->getUpgradeVersion();
$siteVersion = [
"versionCode" => $currentVersion["versionCode"],
"versionName" => $currentVersion['versionName'],
"upgradeErrCode" => $upgradeErrCode,
"upgradeErrInfo" => $upgradeErrInfo,
"passwordFileName" => $this->getPasswordFileName(),//升级口令文件名称
];
$fileName = dirname(__FILE__) . "/../../upgrade.php";
$contents = var_export($siteVersion, true);
file_put_contents($fileName, "<?php\n return {$contents};\n ");
}
protected function updateUpgradePasswordFileName($passwordFileName)
{
$versionInfos = $this->getUpgradeVersion();
$versionInfos['passwordFileName'] = $passwordFileName;
$fileName = dirname(__FILE__) . "/../../upgrade.php";
$contents = var_export($versionInfos, true);
file_put_contents($fileName, "<?php\n return {$contents};\n ");
}
protected function executeMysqlScript()
{
$tag = __CLASS__ . "->" . __FUNCTION__;
$mysqlScriptPath = dirname(__DIR__) . "/../model/database-sql/site_mysql.sql";
$this->logger->error("site.install.db", "mysql script=" . $mysqlScriptPath);
$_sqlContent = file_get_contents($mysqlScriptPath);//写自己的.sql文件
$_sqlArr = explode(';', $_sqlContent);
try {
$this->ctx->db->beginTransaction();
foreach ($_sqlArr as $sql) {
$this->ctx->db->exec($sql);
}
$this->ctx->db->commit();
} catch (Throwable $e) {
$this->ctx->db->rollBack();
$this->logger->error($tag, $e);
throw $e;
}
}
protected function executeSqliteScript()
{
$tag = __CLASS__ . "->" . __FUNCTION__;
$mysqlScriptPath = dirname(__DIR__) . "/../model/database-sql/site_sqlite.sql";
$_sqlContent = file_get_contents($mysqlScriptPath);//写自己的.sql文件
$_sqlArr = explode(';', $_sqlContent);
try {
$this->ctx->db->beginTransaction();
foreach ($_sqlArr as $sql) {
$this->ctx->db->exec($sql);
}
$this->ctx->db->commit();
} catch (Exception $e) {
$this->ctx->db->rollBack();
$this->ctx->logger->error($tag, $e);
throw $e;
}
}
protected function updateSiteConfigAsUpgrade($newVersionCode, $newVersionName)
{
$siteConfig = ZalyConfig::getAllConfig();
$siteConfig["siteVersionCode"] = $newVersionCode;
$siteConfig["siteVersionName"] = $newVersionName;
ZalyConfig::updateConfigFile($siteConfig);
}
}