-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathUpgradeUtil.php
More file actions
128 lines (119 loc) · 4.59 KB
/
Copy pathUpgradeUtil.php
File metadata and controls
128 lines (119 loc) · 4.59 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
<?php
namespace Module\AdminManager\Util;
use App\Constant\AppConstant;
use Chumper\Zipper\Zipper;
use Illuminate\Support\Facades\Artisan;
use ModStart\Core\Exception\BizException;
use ModStart\Core\Input\Response;
use ModStart\Core\Util\CurlUtil;
use ModStart\Core\Util\FileUtil;
use ModStart\ModStart;
class UpgradeUtil
{
// const REMOTE_BASE = 'http://org.demo.soft.host';
const REMOTE_BASE = 'https://modstart.com';
private static function baseRequest($api, $data = [], $token = null)
{
return CurlUtil::postJSONBody(self::REMOTE_BASE . $api, $data, [
'header' => [
'api-token' => $token,
'X-Requested-With' => 'XMLHttpRequest',
]
]);
}
public static function latest()
{
$ret = self::baseRequest('/api/app/latest', [
'app' => AppConstant::APP,
'version' => AppConstant::VERSION,
]);
BizException::throwsIfResponseError($ret);
return $ret['data'];
}
public static function downloadPackage($token, $app, $fromVersion, $toVersion)
{
$ret = self::baseRequest('/api/app/download_package', [
'app' => $app,
'fromVersion' => $fromVersion,
'toVersion' => $toVersion,
], $token);
BizException::throwsIfResponseError($ret);
$package = $ret['data']['package'];
$packageMd5 = $ret['data']['packageMd5'];
$diffContent = $ret['data']['diffContent'];
$data = CurlUtil::getRaw($package);
BizException::throwsIfEmpty('安装包获取失败', $data);
$zipTemp = FileUtil::generateLocalTempPath('zip');
file_put_contents($zipTemp, $data);
BizException::throwsIf('文件MD5校验失败', md5_file($zipTemp) != $packageMd5);
$diffContentFile = FileUtil::generateLocalTempPath('json');
file_put_contents($diffContentFile, $diffContent);
return Response::generateSuccessData([
'diffContentFile' => $diffContentFile,
'package' => $zipTemp,
'packageSize' => filesize($zipTemp),
]);
}
public static function upgradePackage($package, $diffContentFile)
{
BizException::throwsIf('package不存在', !file_exists($package));
BizException::throwsIf('diffContentFile不存在', !file_exists($diffContentFile));
$diffContent = @json_decode(file_get_contents($diffContentFile), true);
BizException::throwsIf('diffContent为空', empty($diffContent));
$checkFiles = [];
if (!empty($diffContent['add'])) {
$checkFiles = array_merge($checkFiles, $diffContent['add']);
}
if (!empty($diffContent['update'])) {
$checkFiles = array_merge($checkFiles, $diffContent['update']);
}
$ret = FileUtil::filePathWritableCheck($checkFiles);
BizException::throwsIfResponseError($ret);
$zipper = new Zipper();
$zipper->make($package);
$logs = [];
if (!empty($diffContent['add'])) {
$logs[] = "新增文件:";
foreach ($diffContent['add'] as $file) {
$content = $zipper->getFileContent($file);
FileUtil::write(base_path($file), $content);
$logs[] = "> $file";
}
$logs[] = "";
}
if (!empty($diffContent['update'])) {
$logs[] = "修改文件:";
foreach ($diffContent['update'] as $file) {
$content = $zipper->getFileContent($file);
FileUtil::write(base_path($file), $content);
$logs[] = "> $file";
}
$logs[] = "";
}
if (!empty($diffContent['delete'])) {
$logs[] = "删除文件:";
foreach ($diffContent['delete'] as $file) {
if (file_exists($f = base_path($file))) {
@unlink($f);
$logs[] = "> $file";
}
}
$logs[] = "";
}
$zipper->close();
ModStart::clearCache();
try {
$exitCode = Artisan::call("migrate");
} catch (\Exception $e) {
$exitCode = -1;
}
BizException::throwsIf("调用 php artisan migrate 失败", 0 != $exitCode);
$exitCode = Artisan::call("modstart:module-install-all");
BizException::throwsIf("调用 php artisan modstart:module-install-all 失败", 0 != $exitCode);
FileUtil::safeCleanLocalTemp($package);
FileUtil::safeCleanLocalTemp($diffContentFile);
return Response::generateSuccessData([
'logs' => $logs,
]);
}
}