Utility.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
  74. }
  75. //支持中文的递归创建目录
  76. //iconv方法是为了防止中文乱码,保证可以创建识别中文目录
  77. //不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
  78. function createDirectory($path,$encoding="GBK"){
  79. mkdir(iconv("UTF-8", $encoding ,$path),0777,true);
  80. }
  81. //不定参数路径拼接
  82. function pathJoin($base, $path) {
  83. $newPath = twoPathJoin($base, $path);
  84. $args = func_get_args();
  85. $argc = func_num_args();
  86. for($i=2;$i<$argc;$i++){
  87. $newPath = twoPathJoin($newPath,$args[$i]);
  88. }
  89. return $newPath;
  90. }
  91. //拷贝文件,自动创建目录,自动覆盖
  92. function fileCopy(string $sourcePath, string $targetPath){
  93. $targetDir = dirname($targetPath);
  94. if(!is_dir($targetDir)){
  95. createDirectory($targetDir);
  96. }
  97. if( file_exists($targetPath) ){
  98. unlink($targetPath);
  99. }
  100. copy($sourcePath,$targetPath);
  101. return file_exists($targetPath);
  102. }
  103. //拷贝目录,自动创建目录,自动覆盖
  104. function dirCopy(string $sourceDir, string $targetDir){
  105. if(! is_dir($targetDir)){
  106. createDirectory($targetDir);
  107. }
  108. $dir = opendir($sourceDir);
  109. while(false !== ( $file = readdir($dir)) ) {
  110. if($file == '.' ){
  111. continue;
  112. }
  113. if($file == '..' ){
  114. continue;
  115. }
  116. $filePath = $sourceDir . '/' . $file;
  117. $targetPath = $targetDir . '/' . $file;
  118. if ( is_dir($filePath) ) {
  119. if(! dirCopy($filePath,$targetPath) ){
  120. logError('failed to copy dir',$filePath,'->',$targetPath);
  121. return false;
  122. }
  123. }
  124. else {
  125. copy($filePath,$targetPath);
  126. if(! file_exists($targetPath) ){
  127. logError('failed to copy file',$filePath,'->',$targetPath);
  128. return false;
  129. }
  130. }
  131. }
  132. closedir($dir);
  133. return true;
  134. }
  135. //拷贝文件或目录
  136. function filedirCopy(string $sourcePath, string $targetPath){
  137. if(! file_exists($sourcePath)){
  138. logError('failed to copy file',$sourcePath,'->',$targetPath);
  139. return false;
  140. }
  141. if( is_dir($sourcePath)){
  142. return dirCopy($sourcePath,$targetPath);
  143. }else{
  144. return fileCopy($sourcePath,$targetPath);
  145. }
  146. }
  147. //替换文件中的宏变量
  148. function replaceMacro(string $filePath,string $targetPath,array $macros){
  149. $content = file_get_contents($filePath);
  150. foreach($macros as $key=>$value){
  151. $content = str_replace($key,$value,$content);
  152. }
  153. return file_put_contents($targetPath,$content);
  154. }
  155. //枚举子目录
  156. function scanSubDir($dir,$exclude_names = array()){
  157. $filelist = scandir($dir);
  158. $subdirs = array();
  159. foreach($filelist as $filename){
  160. //过滤掉当前目录和上级目录
  161. if($filename == "." || $filename ==".."){
  162. continue;
  163. }
  164. //过滤掉排查的名称
  165. if(in_array($filename,$exclude_names)){
  166. continue;
  167. }
  168. //判断是否是文件夹
  169. $filepath = pathJoin($dir,$filename);
  170. if(is_dir($filepath) ){
  171. array_push($subdirs,$filename);
  172. }
  173. }
  174. return $subdirs;
  175. }
  176. //枚举所有仓库名
  177. function scanRepoNames($vendoDir){
  178. $repos = array();
  179. $users = scanSubDir($vendorDir);
  180. foreach($users as $user){
  181. $userdir = pathJoin($vendorDir,$user);
  182. $names = scanSubDir($userdir);
  183. foreach($names as $name){
  184. array_push($repos,$user . '/' . $name);
  185. }
  186. }
  187. array_push($repos,"."); //.名称表示顶层包,放最后
  188. return $repos;
  189. }
  190. //解析所有仓库配置
  191. function loadRepoConfigs($vendorDir,$repoNames){
  192. $configs = array();
  193. foreach($repoNames as $repoName){
  194. if($repoName == '.')
  195. $cfgPath = pathJoin(dirname($vendorDir),'composer.json');
  196. else
  197. $cfgPath = pathJoin($vendorDir,$repoName,'composer.json');
  198. $data = json_decode(file_get_contents($cfgPath), true);
  199. if(!$data){
  200. logError('invalid composer.json',$cfgPath);
  201. return false;
  202. }
  203. $configs[$repoName] = $data;
  204. }
  205. return $configs;
  206. }
  207. //解析部署配置文件
  208. function loadConfig($cfgPath){
  209. $config = json_decode(file_get_contents($cfgPath), true);
  210. if(!$config){
  211. logError("failed to decode config file",$cfgPath);
  212. return false;
  213. }
  214. if(! $config['distribute-mode']){
  215. logError("distribute-mode node not exists");
  216. return false;
  217. }
  218. if(! $config['cache-dir'] ){
  219. logError("cache-dir node not exists");
  220. return false;
  221. }
  222. return $config;
  223. }
  224. //多线程下载文件
  225. function downloadToDir($toolDir,$ossHost,$repoName,$fileName,$targetDir){
  226. if (!file_exists($targetDir)){
  227. mkdir($targetDir,0777,true);
  228. }
  229. $targetPath = pathJoin($targetDir,$fileName);
  230. if( file_exists($targetPath) ) {
  231. return $targetPath;
  232. }
  233. $gopeed = getGoPeedPath($toolDir);
  234. $remoteUrl = pathJoin($ossHost,$repoName,$fileName);
  235. $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
  236. logInfo("start to fetch common file --",$cmd);
  237. system($cmd, $ret);
  238. if ($ret != 0) {
  239. return false;
  240. }
  241. return $targetPath;
  242. }
  243. //解压缩文件
  244. function unzipFile($toolDir,$zipFile, $targetDir){
  245. $fileExt = getFileExt($zipFile);
  246. if($fileExt == "zip"){
  247. $zip= new \ZipArchive;
  248. if($zip->open($zipFile)==true){
  249. $zip->extractTo($targetDir);
  250. $zip->close();
  251. return true;
  252. }
  253. else{
  254. return false;
  255. }
  256. }
  257. if($fileExt == "7z"){
  258. $sevenZip = get7ZipPath($toolDir);
  259. $logPath = $zipFile . '.log';
  260. $cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath;
  261. system($cmd, $ret);
  262. if ($ret != 0) {
  263. return false;
  264. }
  265. return true;
  266. }
  267. return false;
  268. }