-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
89 lines (84 loc) · 2.63 KB
/
update.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
<?php
$base = 'base';
$commonDockerFileReplace = [
'${COMMENT}' => "This File Is Updated From {$base}/Dockerfile By `update.php`, Don't Modify!",
'${USE_MIRROR}' => '#', // 本地测试时使用镜像可以加速 apt-get
'${PHP_VERSION}' => '{$VERSION}',
'ARG PHP_VERSION' => '',
'${MCRYPT}' => 'mcrypt \\', // php 7.2 已经完全移除 mcrypt 扩展到 pecl 中。@link http://php.net/manual/zh/migration71.deprecated.php
'${SQLSRV_VERSION}' => '5.3.0', // 5.3.0以上版本不支持 php7.0,5.5以上可能才支持 php7.3 @link https://pecl.php.net/package/sqlsrv
'${NODE_JS_VERSION}' => '10', // 大版本,6、8、10、11
];
$replaceFileData = [
'7.0' => [
'/Dockerfile' => array_merge($commonDockerFileReplace, [
]),
],
'7.1' => [
'/Dockerfile' => array_merge($commonDockerFileReplace, [
]),
],
'7.2' => [
'/Dockerfile' => array_merge($commonDockerFileReplace, [
'${MCRYPT}' => '\\',
]),
],
];
foreach ($replaceFileData as $version => $replaceFileItem) {
rmdirs($version);
copydir($base, $version);
foreach ($replaceFileItem as $filename => $searchAndReplace) {
$searchAndReplace = array_map(function ($item) use ($version) {
return $item == '{$VERSION}' ? $version : $item;
}, $searchAndReplace);
replaceContent($version . $filename, $searchAndReplace);
}
}
/**
* 删除文件夹
* @param $path
* @return bool
*/
function rmdirs($path)
{
if (!is_dir($path)) return true;
$handle = opendir($path);
while (($item = readdir($handle)) !== false) {
if ($item == '.' || $item == '..') continue;
$_path = $path . '/' . $item;
if (is_file($_path)) unlink($_path);
if (is_dir($_path)) rmdirs($_path);
}
closedir($handle);
return rmdir($path);
}
/**
* 复制文件夹
* @param $source
* @param $dest
*/
function copydir($source, $dest)
{
if (!file_exists($dest)) mkdir($dest);
$handle = opendir($source);
while (($item = readdir($handle)) !== false) {
if ($item == '.' || $item == '..') continue;
$_source = $source . '/' . $item;
$_dest = $dest . '/' . $item;
if (is_file($_source)) copy($_source, $_dest);
if (is_dir($_source)) copydir($_source, $_dest);
}
closedir($handle);
}
/**
* 替换文件内容
* @param $filename
* @param $searchAndReplace
*/
function replaceContent($filename, $searchAndReplace)
{
if (!file_exists($filename)) return;
$content = file_get_contents($filename);
$content = strtr($content, $searchAndReplace);
file_put_contents($filename, $content);
}