Utility.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if(!$toolDir)
  51. return 'gopeed.exe';
  52. return pathJoin($toolDir,'gopeed.exe');
  53. }
  54. //获取 7z.exe 路径
  55. function get7ZipPath($toolDir){
  56. if(!$toolDir)
  57. return '7z.exe';
  58. return pathJoin($toolDir,'7z.exe');
  59. }
  60. //两个参数的路径拼接
  61. function twoPathJoin($base, $path) {
  62. return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
  63. }
  64. //支持中文的递归创建目录
  65. //iconv方法是为了防止中文乱码,保证可以创建识别中文目录
  66. //不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
  67. function createDirectory($path,$encoding="GBK"){
  68. mkdir(iconv("UTF-8", $encoding ,$path),0777,true);
  69. }
  70. //不定参数路径拼接
  71. function pathJoin($base, $path) {
  72. $newPath = twoPathJoin($base, $path);
  73. $args = func_get_args();
  74. $argc = func_num_args();
  75. for($i=2;$i<$argc;$i++){
  76. $newPath = twoPathJoin($newPath,$args[$i]);
  77. }
  78. return $newPath;
  79. }
  80. //拷贝文件到,自动创建目录,自动覆盖
  81. function fileCopy(string $filePath, string $targetPath){
  82. $targetDir = dirname($targetPath);
  83. if(!is_dir($targetDir)){
  84. createDirectory($targetDir);
  85. }
  86. if( file_exists($targetPath) ){
  87. unlink($targetPath);
  88. }
  89. copy($filePath,$targetPath);
  90. return true;
  91. }
  92. //枚举子目录
  93. function scanSubDir($dir,$exclude_names = array()){
  94. $filelist = scandir($dir);
  95. $subdirs = array();
  96. foreach($filelist as $filename){
  97. //过滤掉当前目录和上级目录
  98. if($filename == "." || $filename ==".."){
  99. continue;
  100. }
  101. //过滤掉排查的名称
  102. if(in_array($filename,$exclude_names)){
  103. continue;
  104. }
  105. //判断是否是文件夹
  106. $filepath = pathJoin($dir,$filename);
  107. if(is_dir($filepath) ){
  108. array_push($subdirs,$filename);
  109. }
  110. }
  111. return $subdirs;
  112. }
  113. //枚举所有仓库名
  114. function scanRepoNames($vendoDir){
  115. $repos = array();
  116. $users = scanSubDir($vendorDir);
  117. foreach($users as $user){
  118. $userdir = pathJoin($vendorDir,$user);
  119. $names = scanSubDir($userdir);
  120. foreach($names as $name){
  121. array_push($repos,$user . '/' . $name);
  122. }
  123. }
  124. array_push($repos,"."); //.名称表示顶层包,放最后
  125. return $repos;
  126. }
  127. //解析所有仓库配置
  128. function loadRepoConfigs($vendorDir,$repoNames){
  129. $configs = array();
  130. foreach($repoNames as $repoName){
  131. if($repoName == '.')
  132. $cfgPath = pathJoin(dirname($vendorDir),'composer.json');
  133. else
  134. $cfgPath = pathJoin($vendorDir,$repoName,'composer.json');
  135. $data = json_decode(file_get_contents($cfgPath), true);
  136. if(!$data){
  137. logError('invalid composer.json',$cfgPath);
  138. return false;
  139. }
  140. $configs[$repoName] = $data;
  141. }
  142. return $configs;
  143. }
  144. //默认配置信息
  145. function defaultConfig(){
  146. return array(
  147. 'distribute-mode' => 'install',
  148. 'cache-dir' => 'd:/dist-cache/',
  149. 'tool-dir' => '',
  150. //'oss-host' => 'http://distribute-helper.oss-cn-hangzhou.aliyuncs.com',
  151. 'oss-host' => 'http://192.168.3.10:8996',
  152. 'mysql-host'=> '',
  153. );
  154. }
  155. //解析部署配置文件
  156. function loadConfig($cfgPath){
  157. $config = json_decode(file_get_contents($cfgPath), true);
  158. if(!$config){
  159. logError("failed to decode config file",$cfgPath);
  160. return false;
  161. }
  162. if(! $config['distribute-mode']){
  163. logError("distribute-mode node not exists");
  164. return false;
  165. }
  166. if(! $config['cache-dir'] ){
  167. logError("cache-dir node not exists");
  168. return false;
  169. }
  170. return $config;
  171. }
  172. //多线程下载文件
  173. function downloadToDir($toolDir,$ossHost,$repoName,$fileName,$targetDir){
  174. if (!file_exists($targetDir)){
  175. mkdir($targetDir,0777,true);
  176. }
  177. $targetPath = pathJoin($targetDir,$fileName);
  178. if( file_exists($targetPath) ) {
  179. return $targetPath;
  180. }
  181. $gopeed = getGoPeedPath($toolDir);
  182. $remoteUrl = pathJoin($ossHost,$repoName,$fileName);
  183. $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
  184. system($cmd, $ret);
  185. if ($ret != 0) {
  186. return false;
  187. }
  188. return $targetPath;
  189. }
  190. //解压缩文件
  191. function unzipFile($toolDir,$zipFile, $targetDir){
  192. $fileExt = getFileExt($zipFile);
  193. if($fileExt == "zip"){
  194. $zip= new \ZipArchive;
  195. if($zip->open($zipFile)==true){
  196. $zip->extractTo($targetDir);
  197. $zip->close();
  198. return true;
  199. }
  200. else{
  201. return false;
  202. }
  203. }
  204. if($fileExt == "7z"){
  205. $sevenZip = get7ZipPath($toolDir);
  206. $logPath = $zipFile . '.log';
  207. $cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath;
  208. system($cmd, $ret);
  209. if ($ret != 0) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. return false;
  215. }