elviss há 2 anos atrás
pai
commit
bde42a63ff
6 ficheiros alterados com 327 adições e 0 exclusões
  1. 4 0
      .gitignore
  2. 19 0
      composer.json
  3. 4 0
      src/Config.php
  4. 44 0
      src/HelperClass.php
  5. 112 0
      src/PackageClass.php
  6. 144 0
      src/Utility.php

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+.idea/
+vendor/
+*.lock
+*.zip

+ 19 - 0
composer.json

@@ -0,0 +1,19 @@
+{
+    "name": "util/distribute-helper",
+    "type": "library",
+    "description": "distribute helper",
+    "license": "MIT",
+	"minimum-stability": "dev",
+	"autoload":{
+		"psr-4":{"DistributeHelper\\": "/src"}
+	},
+	"extra": {
+        "class": "DistributeHelper\\HelperClass"
+    },
+    "repositories": [
+        { "type": "composer", "url": "http://satis-dist.lysis.com.cn:4000/" }
+    ],
+    "config": {
+        "secure-http": false
+    }
+}

+ 4 - 0
src/Config.php

@@ -0,0 +1,4 @@
+<?php
+return array(
+	'OSS_URL' => "http://192.168.3.10:8992/",
+);

+ 44 - 0
src/HelperClass.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace DistributeHelper;
+
+use Composer\Script\Event;
+use Composer\Installer\PackageEvent;
+use DistributeHelper\PackageClass;
+error_reporting(E_ALL & ~E_NOTICE);
+
+class HelperClass
+{
+    public static function postUpdate(Event $event)
+    {
+        $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
+		$devMode = (getenv("COMPOSER_DEV_MODE") == "1");
+		PackageClass::setDevMode($devMode);
+		PackageClass::index($vendorDir, "");
+		print("---postUpdate finished---");
+    }
+
+    public static function postAutoloadDump(Event $event)
+    {
+        $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
+        require $vendorDir . '/autoload.php';
+        some_function_from_an_autoloaded_file();
+    }
+
+    public static function postPackageInstall(PackageEvent $event)
+    {      
+		$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
+		$package = $event->getOperation()->getPackage();
+		PackageClass::setDevMode( $event->isDevMode() );
+		PackageClass::index($vendorDir, $package->getName());
+    }
+
+    public static function postPackageUpdate(PackageEvent $event)
+    {
+		$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
+		$package = $event->getOperation()->getPackage();
+		PackageClass::setDevMode( $event->isDevMode() );
+		PackageClass::index($vendorDir, $package->getName());
+    }
+	
+}

+ 112 - 0
src/PackageClass.php

@@ -0,0 +1,112 @@
+<?php
+namespace DistributeHelper;
+error_reporting(E_ALL & ~E_NOTICE);
+include "Utility.php";
+
+class PackageClass
+{
+	private static $config;
+	public static function index($vendorDir, $repoName)
+	{
+		//读取配置信息
+		$config = include "Config.php";
+		
+		//获取当前包信息
+		$packageInfo = self::getPackageInfo($config,$vendorDir, $repoName);
+		
+		//下载当前包的文件
+		$packageFiles = getPackageFiles($packageInfo['configPath']);
+        self::fetchFiles($packageInfo,$packageFiles['files']);
+		
+		//下载当前包的bin文件
+		self::fetchBinFiles($packageInfo,$packageFiles['bin-files']);
+	}
+	public static function setDevMode($devMode){
+	}
+	//获取包相关信息
+	private static function getPackageInfo($config,$vendorDir, $repoName){
+		if($repoName == ""){ //最上层包的信息
+			$packageDir = dirname($vendorDir);
+			$json = file_get_contents($packageDir . '/composer.json');
+			$data = json_decode($json, true);
+			$repoName = $data["name"];
+		}
+		else{ //其他依赖包的信息
+			$packageDir = $vendorDir . '/' . $repoName;
+		}
+		$cacheDir = dirname($vendorDir) . '/.cache/' . $repoName;
+		
+		//获取最上层包配置的 bin-dir
+		$cfgPath = dirname($vendorDir)."/composer.json";
+		$binDir = getBinDir($cfgPath);
+		
+		//
+		return array(
+			"repoName" => $repoName,
+			"packageDir" => $packageDir,
+			"configPath" => $packageDir."/composer.json",
+			"cacheDir" => $cacheDir,
+			"binDir" => $binDir,
+			"ossUrl" => $config['OSS_URL'],
+		);
+	}
+	
+	//下载包文件
+	private static function fetchFiles($packageInfo,$files){
+		$cacheDir = $packageInfo['cacheDir'];
+		$repoName = $packageInfo['repoName'];
+		$packageDir = $packageInfo['packageDir'];
+		$ossUrl = $packageInfo['ossUrl'];
+		if(! $files)
+			return;
+		
+		foreach($files as $fileName){
+			printf("[%s] fetching file -- %s\n",$repoName,$fileName);
+
+			//下载文件到缓存区
+			$filePath = downloadToDir($ossUrl,$repoName,$fileName,$cacheDir);
+			if(! $filePath ){
+				printf("[%s] fetch file failed -- %s\n",$repoName,$fileName);
+				continue;
+			}
+
+			// 解压文件
+			if( isZipFile($fileName) ){
+				if(! unzipFile($filePath,$packageDir)) {
+					printf("[%s] unzip file failed -- %s\n ",$repoName,$fileName);
+					unlink($filePath);
+				}
+			}
+		}
+	}
+	
+	//下载包bin文件
+	private static function fetchBinFiles($packageInfo,$binFiles){
+		$cacheDir = $packageInfo['cacheDir'];
+		$repoName = $packageInfo['repoName'];
+		$binDir = $packageInfo['binDir'];
+		$ossUrl = $packageInfo['ossUrl'];
+		if(! $binDir)
+			return;
+		if(! $binFiles)
+			return;
+		foreach($binFiles as $fileName){
+			printf("[%s] fetching binfile -- %s\n",$repoName,$fileName);
+			//下载文件到缓存区
+			$filePath = downloadToDir($ossUrl,$repoName,$fileName,$cacheDir);
+			if(! $filePath ){
+				printf("[%s] fetch binfile failed -- %s\n",$repoName,$fileName);
+				continue;
+			}
+
+			// 解压文件
+			if( isZipFile($fileName) ){
+				if(! unzipFile($filePath,$binDir)) {
+					printf("[%s] unzip binfile failed -- %s\n ",$repoName,$fileName);
+					unlink($filePath);
+				}
+			}
+		}
+	}
+	
+}

+ 144 - 0
src/Utility.php

@@ -0,0 +1,144 @@
+<?php
+
+//获取操作系统名称
+function getOsName(){
+	if(substr(PHP_OS,0,3)==='WIN')
+		return "win";
+	else
+		return "linux";
+}
+
+//判断是否键值对类型的数组
+function isAssocArray($array){
+	if(! is_array($array) )
+		return false;
+	return array_keys($array) !== range(0, count($array) - 1);
+}
+
+//获取文件扩展名,小写
+function getFileExt($fileName){
+	return strtolower(pathinfo($fileName,PATHINFO_EXTENSION));
+}
+
+//是否压缩文件
+function isZipFile($fileName){
+	$fileExt = getFileExt($fileName);
+	return in_array($fileExt,array("zip","7z"));
+}
+//获取 gopeed.exe 路径
+function getGoPeedPath(){
+	return dirname(dirname(__FILE__)). "/bin/gopeed.exe";
+}
+
+//获取 7z.exe 路径
+function get7ZipPath(){
+	return dirname(dirname(__FILE__)). "/bin/7z.exe";
+}
+
+//多线程下载文件
+function downloadToDir($ossUrl,$repoName,$fileName,$targetDir){
+	if (!file_exists($targetDir))
+		mkdir($targetDir,0777,true);
+
+	$targetPath = pathJoin($targetDir,$fileName);
+	if( file_exists($targetPath) ) {
+		return $targetPath;
+	}
+	$gopeed = getGoPeedPath();
+	
+	$remoteUrl = pathJoin($ossUrl,$repoName,$fileName);
+	$cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
+	system($cmd, $ret);
+	if ($ret != 0) {
+		return false;
+	}
+	return $targetPath;
+}
+
+//解压缩文件
+function unzipFile($zipFile, $targetDir){
+	$fileExt = getFileExt($zipFile);
+	if($fileExt == "zip"){
+		$zip= new \ZipArchive;
+		if($zip->open($zipFile)==true){
+			$zip->extractTo($targetDir);
+			$zip->close();
+			return true;
+		}
+		else{
+			return false;
+		}
+	}
+	if($fileExt == "7z"){
+		$sevenZip = get7ZipPath();
+		$logPath = $zipFile . '.7zlog';
+		$cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath;
+		system($cmd, $ret);
+		if ($ret != 0) {
+			return false;
+		}
+		return true;
+	}
+	return false;
+}
+function twoPathJoin($base, $path) {
+	return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
+}
+function pathJoin($base, $path) {
+	$newPath = twoPathJoin($base, $path);
+	$args = func_get_args();
+	$argc = func_num_args();
+	for($i=2;$i<$argc;$i++){
+		$newPath = twoPathJoin($newPath,$args[$i]);
+	}
+    return $newPath;
+}
+//过滤得到一个最终文件列表
+function filterFiles($files){
+	if(!is_array($files) )
+		return array();
+	if( isAssocArray($files) ){
+
+		$osname = getOsName();
+		return $files[$osname] ? : array();
+	}
+	else{
+		return $files;
+	}
+}
+
+//获取最上层包配置的bin-dir路径
+function  getBinDir($cfgPath) {
+	$data = json_decode(file_get_contents($cfgPath), true);
+	$binDir = $data["composer-helper"]["bin-dir"];
+	if(! $binDir){
+		return "";
+	}
+	$osname = getOsName();
+	if(isAssocArray($binDir))
+		$binDir = $binDir[$osname];
+	if($binDir){
+		return dirname($cfgPath) . '/' . $binDir;
+	}else{
+		return "";
+	}
+}
+//获取依赖包的待下载的文件列表
+function getPackageFiles($cfgPath)
+{
+	// 解析 composer.json
+	if (!file_exists($cfgPath)) {
+		return false;
+	}
+	$data = json_decode(file_get_contents($cfgPath), true);
+	if (!$data) {
+		print("bad json format - " . $cfgPath . PHP_EOL);
+		return false;
+	}
+	
+	//过滤并组合得到待下载文件列表
+	return array(
+		'files'=>filterFiles($data["composer-helper"]["files"]),
+		'bin-files'=>filterFiles($data["composer-helper"]["bin-files"])
+	);
+}