elviss 2 ani în urmă
părinte
comite
d5d0d2114b
2 a modificat fișierele cu 152 adăugiri și 3 ștergeri
  1. 59 3
      src/PackageClass.php
  2. 93 0
      src/WscriptShell.php

+ 59 - 3
src/PackageClass.php

@@ -1,8 +1,8 @@
 <?php
 namespace DistributeHelper;
 error_reporting(E_ALL & ~E_NOTICE);
-include "Utility.php";
-include "WscriptShell.php";
+include_once "Utility.php";
+include_once "WscriptShell.php";
 
 class PackageClass
 {
@@ -24,7 +24,13 @@ class PackageClass
 			return false;
 		}
 		logInfo("distribute mode: ",self::$config['distribute-mode']);
-		
+	
+		//预先系统准备操作
+		if(! self::prepare($vendorDir,$rootPkg) ){
+			self::setProgressFailed('PREPARE_FAILED');
+			return false;
+		}
+	
 		//分发模式
 		if( self::$config['distribute-mode'] == 'install'){
 			self::install($vendorDir,$rootPkg,$vendorPkgs);
@@ -35,6 +41,20 @@ class PackageClass
 		return true;
 	}
 	
+	private static function prepare($vendorDir,$pkg)
+	{
+		//获取包信息
+		$repoDir = dirname($vendorDir);
+		$packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
+		
+		//关闭进程
+		if(! self::removeProcess($packageInfo) ){
+			return false;
+		}
+		
+		return true;
+	}
+	
 	private static function install($vendorDir,$rootPkg,$vendorPkgs){
 		//依赖库都已下载,默认先给个进度
 		self::setProgressStep1(100);
@@ -176,6 +196,9 @@ class PackageClass
 		if( !file_exists($distributeDir) ){
 			createDirectory($distributeDir);
 		}
+		if(! defined('DIST_DIR')){
+			define('DIST_DIR',$distributeDir);
+		}
 		
 		//设置日志文件路径
 		if(! defined('LOG_PATH')){
@@ -385,6 +408,39 @@ class PackageClass
 		return true;
 	}*/
 	
+	private static function removeProcess($packageInfo)
+	{
+		$app = self::$app;	
+		$cacheDir = $packageInfo['cacheDir'];
+		$repoName = $packageInfo['repoName'];
+		$version = $packageInfo['version'];
+		$rootDir = $packageInfo['rootDir'];
+		$removeProcess = $app["distribute-root"]["remove-process"] ? : array();
+		$proc = new WinProcess(DIST_DIR);
+		foreach($removeProcess as $process){
+			$name = $process['name'];
+			//获取进程PIDs
+			$pids = $proc->find($name);
+			if( $pids === false ){
+				logError("failed to find process, action failed.",$name);
+				return false;
+			}
+			if( count($pids) === 0 ){
+				logInfo("process not exists.no need to remove process",$name);
+				continue;
+			}
+			//移除进程
+			$ret = $proc->remove($name);
+			if( $ret === false ){
+				logError("failed to remove process, remove action failed.",$name);
+				return false;
+			}
+			else{
+				logInfo("succeeded to remove process",$name);
+			}
+		}
+		return true;		
+	}
 	//拷贝copy-files文件
 	private static function copyFiles($packageInfo)
 	{

+ 93 - 0
src/WscriptShell.php

@@ -1,6 +1,7 @@
 <?php
 namespace DistributeHelper;
 error_reporting(E_ALL & ~E_NOTICE);
+include_once "Utility.php";
 
 class WscriptShell
 {
@@ -14,6 +15,32 @@ class WscriptShell
 			 logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
 		}
 	}
+	/* style
+	0 SW_HIDE
+	1 SW_SHOWNORMAL
+	2 SW_SHOWMINIMIZED
+	3 SW_SHOWMAXIMIZED
+	4 SW_SHOWNOACTIVATE
+	5 SW_SHOW
+	6 SW_MINIMIZE
+	7 SW_SHOWMINNOACTIVE
+	8 SW_SHOWNA
+	9 SW_RESTORE
+	*/
+	public static function Run($cmd,$style,$wait)
+	{
+		$shell = self::$shell;
+		if(! $shell){
+			logError('failed to Run. shell is null.');
+			return false;
+		}
+		try{
+			return $shell->Run($cmd,$style,$wait);
+		} catch (\Exception $e) {
+			logError('failed to Run',$cmd, $e->getCode(),$e->getMessage());
+			return false;
+		}
+	}
 	public static function RegRead($key)
 	{
 		$shell = self::$shell;
@@ -100,6 +127,72 @@ class WscriptShell
 		}
 	}
 }
+//windows进程操作
+class WinProcess extends WscriptShell
+{
+	private static $tmp_dir;
+	public function __construct($tmp_dir)
+	{
+		WscriptShell::__construct();
+		self::$tmp_dir = $tmp_dir;
+	}
+	private static function pathJoin($base, $path) {
+		$base = str_replace('\\','/',$base);
+		$path = str_replace('\\','/',$path);
+		return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
+	}
+	public static function find($name){
+		$out_file = pathJoin(self::$tmp_dir,$name . '.txt');
+		if( file_exists($out_file) ){
+			unlink($out_file);
+		}
+		$cmd = "cmd.exe /C tasklist /FI \"IMAGENAME eq {$name}\" /FO list > {$out_file}";
+		if( self::Run($cmd,0,1) != 0){
+			logError('failed to Run',$cmd);
+			return false;
+		}
+		if( !file_exists($out_file) ){
+			logError('failed to find out file',$out_file);
+			return false;
+		}
+		$pids = array();
+		$fp = fopen($out_file,"r");
+		while($line = fgets($fp) ){
+			if( substr($line,0,4) == 'PID:'){
+				$pid = trim(substr($line,4));
+				array_push($pid);
+			}
+		}
+		fclose($fp);
+		return $pids;
+	}
+	
+	public static function remove($name)
+	{
+		//关闭进程
+		$cmd = "cmd.exe /C taskkill /F /IM {name}";
+		if( self::Run($cmd,0,1) != 0){
+			logError('failed to Run',$cmd);
+			return false;
+		}
+		
+		//检测是否已经没有该任务进程
+		$pids = self::find($name);
+		if($pids === false){
+			logError('failed to execute find',$name);
+			return false;
+		}
+		if( count($pids) == 0){
+			logInfo('no process exists',$name);
+			return true;
+		}
+		else{
+			logInfo('process still exists after stop',$name);
+			return false;
+		}
+	}
+};
+
 //windows系统是否有管理员权限,写入一个注册表键值测试
 class WinAdminPriv extends WscriptShell
 {