Utility.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 $sourcePath, 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($sourcePath,$targetPath);
  90. return file_exists($targetPath);
  91. }
  92. //拷贝目录,自动创建目录,自动覆盖
  93. function dirCopy(string $sourceDir, string $targetDir){
  94. if(! is_dir($targetDir)){
  95. createDirectory($targetDir);
  96. }
  97. $dir = opendir($sourceDir);
  98. while(false !== ( $file = readdir($dir)) ) {
  99. if($file == '.' ){
  100. continue;
  101. }
  102. if($file == '..' ){
  103. continue;
  104. }
  105. $filePath = $sourceDir . '/' . $file;
  106. $targetPath = $targetDir . '/' . $file;
  107. if ( is_dir($filePath) ) {
  108. if(! dirCopy($filePath,$targetPath) ){
  109. logError('failed to copy dir',$filePath,'->',$targetPath);
  110. return false;
  111. }
  112. }
  113. else {
  114. copy($filePath,$targetPath);
  115. if(! file_exists($targetPath) ){
  116. logError('failed to copy file',$filePath,'->',$targetPath);
  117. return false;
  118. }
  119. }
  120. }
  121. closedir($dir);
  122. return true;
  123. }
  124. //拷贝文件或目录
  125. function filedirCopy(string $sourcePath, string $targetPath){
  126. if(! file_exists($sourcePath)){
  127. logError('failed to copy file',$sourcePath,'->',$targetPath);
  128. return false;
  129. }
  130. if( is_dir($sourcePath)){
  131. return dirCopy($sourcePath,$targetPath);
  132. }else{
  133. return fileCopy($sourcePath,$targetPath);
  134. }
  135. }
  136. //替换文件中的宏变量
  137. function replaceMacro(string $filePath,string $targetPath,array $macros){
  138. $content = file_get_contents($filePath);
  139. foreach($macros as $key=>$value){
  140. $content = str_replace($key,$value,$content);
  141. }
  142. return file_put_contents($targetPath,$content);
  143. }
  144. //枚举子目录
  145. function scanSubDir($dir,$exclude_names = array()){
  146. $filelist = scandir($dir);
  147. $subdirs = array();
  148. foreach($filelist as $filename){
  149. //过滤掉当前目录和上级目录
  150. if($filename == "." || $filename ==".."){
  151. continue;
  152. }
  153. //过滤掉排查的名称
  154. if(in_array($filename,$exclude_names)){
  155. continue;
  156. }
  157. //判断是否是文件夹
  158. $filepath = pathJoin($dir,$filename);
  159. if(is_dir($filepath) ){
  160. array_push($subdirs,$filename);
  161. }
  162. }
  163. return $subdirs;
  164. }
  165. //枚举所有仓库名
  166. function scanRepoNames($vendoDir){
  167. $repos = array();
  168. $users = scanSubDir($vendorDir);
  169. foreach($users as $user){
  170. $userdir = pathJoin($vendorDir,$user);
  171. $names = scanSubDir($userdir);
  172. foreach($names as $name){
  173. array_push($repos,$user . '/' . $name);
  174. }
  175. }
  176. array_push($repos,"."); //.名称表示顶层包,放最后
  177. return $repos;
  178. }
  179. //解析所有仓库配置
  180. function loadRepoConfigs($vendorDir,$repoNames){
  181. $configs = array();
  182. foreach($repoNames as $repoName){
  183. if($repoName == '.')
  184. $cfgPath = pathJoin(dirname($vendorDir),'composer.json');
  185. else
  186. $cfgPath = pathJoin($vendorDir,$repoName,'composer.json');
  187. $data = json_decode(file_get_contents($cfgPath), true);
  188. if(!$data){
  189. logError('invalid composer.json',$cfgPath);
  190. return false;
  191. }
  192. $configs[$repoName] = $data;
  193. }
  194. return $configs;
  195. }
  196. //默认配置信息
  197. function defaultConfig(){
  198. return array(
  199. 'distribute-mode' => 'install',
  200. 'cache-dir' => 'd:/dist-cache/',
  201. 'tool-dir' => '',
  202. //'oss-host' => 'http://distribute-helper.oss-cn-hangzhou.aliyuncs.com',
  203. 'oss-host' => 'http://192.168.3.10:8996',
  204. 'mysql-host'=> '',
  205. );
  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. }