Utility.php 5.1 KB

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