Utility.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. //默认配置信息
  3. function defaultConfig(){
  4. return array(
  5. 'distribute-mode' => 'install',
  6. 'cache-dir' => 'd:/dist-cache/',
  7. 'tool-dir' => '',
  8. //'oss-host' => 'http://sis31-disthelp.oss-cn-hangzhou.aliyuncs.com',
  9. 'oss-host' => 'http://192.168.3.10:8996',
  10. 'mysql-host'=> '',
  11. );
  12. }
  13. //记录信息日志
  14. function logInfo(){
  15. $content = '[INF] ';
  16. $args = func_get_args();
  17. $argc = func_num_args();
  18. for($i=0;$i<$argc;$i++){
  19. $content .= $args[$i] . ' ';
  20. }
  21. $content .= PHP_EOL;
  22. print($content);
  23. file_put_contents(LOG_PATH, $content, FILE_APPEND);
  24. }
  25. //记录错误日志
  26. function logError(){
  27. $content = '[ERR] ';
  28. $args = func_get_args();
  29. $argc = func_num_args();
  30. for($i=0;$i<$argc;$i++){
  31. $content .= $args[$i] . ' ';
  32. }
  33. $content .= PHP_EOL;
  34. print($content);
  35. file_put_contents(LOG_PATH, $content, FILE_APPEND);
  36. }
  37. //获取操作系统名称
  38. function getOsName(){
  39. if(substr(PHP_OS,0,3)==='WIN')
  40. return "win";
  41. else
  42. return "linux";
  43. }
  44. //判断是否键值对类型的数组
  45. function isAssocArray($array){
  46. if(! is_array($array) )
  47. return false;
  48. return array_keys($array) !== range(0, count($array) - 1);
  49. }
  50. //获取文件扩展名,小写
  51. function getFileExt($fileName){
  52. return strtolower(pathinfo($fileName,PATHINFO_EXTENSION));
  53. }
  54. //是否压缩文件
  55. function isZipFile($fileName){
  56. $fileExt = getFileExt($fileName);
  57. return in_array($fileExt,array("zip","7z"));
  58. }
  59. //获取 gopeed.exe 路径
  60. function getGoPeedPath($toolDir){
  61. if(!$toolDir)
  62. return 'gopeed.exe';
  63. return pathJoin($toolDir,'gopeed.exe');
  64. }
  65. //获取 7z.exe 路径
  66. function get7ZipPath($toolDir){
  67. if(!$toolDir)
  68. return '7z.exe';
  69. return pathJoin($toolDir,'7z.exe');
  70. }
  71. //两个参数的路径拼接
  72. function twoPathJoin($base, $path) {
  73. $base = str_replace('\\','/',$base);
  74. $path = str_replace('\\','/',$path);
  75. return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
  76. }
  77. //支持中文的递归创建目录
  78. //iconv方法是为了防止中文乱码,保证可以创建识别中文目录
  79. //不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
  80. function createDirectory($path,$encoding="GBK"){
  81. mkdir(iconv("UTF-8", $encoding ,$path),0777,true);
  82. }
  83. //不定参数路径拼接
  84. function pathJoin($base, $path) {
  85. $newPath = twoPathJoin($base, $path);
  86. $args = func_get_args();
  87. $argc = func_num_args();
  88. for($i=2;$i<$argc;$i++){
  89. $newPath = twoPathJoin($newPath,$args[$i]);
  90. }
  91. return $newPath;
  92. }
  93. //拷贝文件,自动创建目录,自动覆盖
  94. function fileCopy(string $sourcePath, string $targetPath){
  95. $targetDir = dirname($targetPath);
  96. if(!is_dir($targetDir)){
  97. createDirectory($targetDir);
  98. }
  99. if( file_exists($targetPath) ){
  100. unlink($targetPath);
  101. }
  102. copy($sourcePath,$targetPath);
  103. return file_exists($targetPath);
  104. }
  105. //拷贝目录,自动创建目录,自动覆盖
  106. function dirCopy(string $sourceDir, string $targetDir){
  107. if(! is_dir($targetDir)){
  108. createDirectory($targetDir);
  109. }
  110. $dir = opendir($sourceDir);
  111. while(false !== ( $file = readdir($dir)) ) {
  112. if($file == '.' ){
  113. continue;
  114. }
  115. if($file == '..' ){
  116. continue;
  117. }
  118. $filePath = $sourceDir . '/' . $file;
  119. $targetPath = $targetDir . '/' . $file;
  120. if ( is_dir($filePath) ) {
  121. if(! dirCopy($filePath,$targetPath) ){
  122. logError('failed to copy dir',$filePath,'->',$targetPath);
  123. return false;
  124. }
  125. }
  126. else {
  127. copy($filePath,$targetPath);
  128. if(! file_exists($targetPath) ){
  129. logError('failed to copy file',$filePath,'->',$targetPath);
  130. return false;
  131. }
  132. }
  133. }
  134. closedir($dir);
  135. return true;
  136. }
  137. //拷贝文件或目录
  138. function filedirCopy(string $sourcePath, string $targetPath){
  139. if(! file_exists($sourcePath)){
  140. logError('failed to copy file',$sourcePath,'->',$targetPath);
  141. return false;
  142. }
  143. if( is_dir($sourcePath)){
  144. return dirCopy($sourcePath,$targetPath);
  145. }else{
  146. return fileCopy($sourcePath,$targetPath);
  147. }
  148. }
  149. //替换文件中的宏变量
  150. function replaceMacro(string $filePath,string $targetPath,array $macros){
  151. $content = file_get_contents($filePath);
  152. foreach($macros as $key=>$value){
  153. $content = str_replace($key,$value,$content);
  154. }
  155. return file_put_contents($targetPath,$content);
  156. }
  157. //枚举子目录
  158. function scanSubDir($dir,$exclude_names = array()){
  159. $filelist = scandir($dir);
  160. $subdirs = array();
  161. foreach($filelist as $filename){
  162. //过滤掉当前目录和上级目录
  163. if($filename == "." || $filename ==".."){
  164. continue;
  165. }
  166. //过滤掉排查的名称
  167. if(in_array($filename,$exclude_names)){
  168. continue;
  169. }
  170. //判断是否是文件夹
  171. $filepath = pathJoin($dir,$filename);
  172. if(is_dir($filepath) ){
  173. array_push($subdirs,$filename);
  174. }
  175. }
  176. return $subdirs;
  177. }
  178. //枚举所有仓库名
  179. function scanRepoNames($vendoDir){
  180. $repos = array();
  181. $users = scanSubDir($vendorDir);
  182. foreach($users as $user){
  183. $userdir = pathJoin($vendorDir,$user);
  184. $names = scanSubDir($userdir);
  185. foreach($names as $name){
  186. array_push($repos,$user . '/' . $name);
  187. }
  188. }
  189. array_push($repos,"."); //.名称表示顶层包,放最后
  190. return $repos;
  191. }
  192. //解析所有仓库配置
  193. function loadRepoConfigs($vendorDir,$repoNames){
  194. $configs = array();
  195. foreach($repoNames as $repoName){
  196. if($repoName == '.')
  197. $cfgPath = pathJoin(dirname($vendorDir),'composer.json');
  198. else
  199. $cfgPath = pathJoin($vendorDir,$repoName,'composer.json');
  200. $data = json_decode(file_get_contents($cfgPath), true);
  201. if(!$data){
  202. logError('invalid composer.json',$cfgPath);
  203. return false;
  204. }
  205. $configs[$repoName] = $data;
  206. }
  207. return $configs;
  208. }
  209. //解析部署配置文件
  210. function loadConfig($cfgPath){
  211. $config = json_decode(file_get_contents($cfgPath), true);
  212. if(!$config){
  213. logError("failed to decode config file",$cfgPath);
  214. return false;
  215. }
  216. if(! $config['distribute-mode']){
  217. logError("distribute-mode node not exists");
  218. return false;
  219. }
  220. if(! $config['cache-dir'] ){
  221. logError("cache-dir node not exists");
  222. return false;
  223. }
  224. return $config;
  225. }
  226. //多线程下载文件
  227. function downloadToDir($toolDir,$ossHost,$repoName,$fileName,$targetDir){
  228. if (!file_exists($targetDir)){
  229. mkdir($targetDir,0777,true);
  230. }
  231. $targetPath = pathJoin($targetDir,$fileName);
  232. if( file_exists($targetPath) ) {
  233. return $targetPath;
  234. }
  235. $gopeed = getGoPeedPath($toolDir);
  236. $remoteUrl = pathJoin($ossHost,$repoName,$fileName);
  237. $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
  238. logInfo("start to fetch common file --",$cmd);
  239. system($cmd, $ret);
  240. if ($ret != 0) {
  241. return false;
  242. }
  243. return $targetPath;
  244. }
  245. //解压缩文件
  246. function unzipFile($toolDir,$zipFile, $targetDir){
  247. $fileExt = getFileExt($zipFile);
  248. if($fileExt == "zip"){
  249. $zip= new \ZipArchive;
  250. if($zip->open($zipFile)==true){
  251. $zip->extractTo($targetDir);
  252. $zip->close();
  253. return true;
  254. }
  255. else{
  256. return false;
  257. }
  258. }
  259. if($fileExt == "7z"){
  260. $sevenZip = get7ZipPath($toolDir);
  261. $logPath = $zipFile . '.log';
  262. $cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath;
  263. system($cmd, $ret);
  264. if ($ret != 0) {
  265. return false;
  266. }
  267. return true;
  268. }
  269. return false;
  270. }