PackageClass.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace DistributeHelper;
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. include "Utility.php";
  5. class PackageClass
  6. {
  7. private static $config = null; //配置信息
  8. //当所有包都安装完毕后
  9. public static function postUpdate($vendorDir,$rootPkg,$vendorPkgs)
  10. {
  11. //初始化
  12. if(! self::initialize($vendorDir)){
  13. self::setProgressFailed('INITIALIZE_FAILED');
  14. return false;
  15. }
  16. //分发模式
  17. if( self::$config['distribute-mode'] == 'install'){
  18. self::install($vendorDir,$rootPkg,$vendorPkgs);
  19. }
  20. else{
  21. self::upgrade($vendorDir,$rootPkg,$vendorPkgs);
  22. }
  23. return true;
  24. }
  25. private static function install($vendorDir,$rootPkg,$vendorPkgs){
  26. //依赖库都已下载,默认先给个进度
  27. self::setProgressStep1(100);
  28. //生成module.ini文件
  29. self::createModuleIni($vendorDir,$vendorPkgs);
  30. //逐个依赖仓库操作
  31. self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
  32. //顶层仓库操作
  33. self::postUpdateRootPkg($vendorDir,$rootPkg);
  34. //全部操作完毕
  35. self::setProgressOK(true);
  36. }
  37. private static function upgrade($vendorDir,$rootPkg,$vendorPkgs){
  38. //依赖库都已下载,默认先给个进度
  39. self::setProgressStep1(100);
  40. //生成module.ini文件
  41. self::createModuleIni($vendorDir,$vendorPkgs);
  42. //逐个依赖仓库操作
  43. self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
  44. //顶层仓库操作
  45. self::postUpdateRootPkg($vendorDir,$rootPkg);
  46. //全部操作完毕
  47. self::setProgressOK(true);
  48. }
  49. private static function createModuleIni($vendorDir,$vendorPkgs)
  50. {
  51. //[MODULE]节点内容
  52. $content = '[MODULE]' . PHP_EOL;
  53. foreach($vendorPkgs as $i=>$pkg){
  54. if( in_array($pkg->getName(),['util/distribute-helper']) )
  55. continue;
  56. $cfgPath = pathJoin($vendorDir,$pkg->getName(),'composer.json');
  57. $data = json_decode(file_get_contents($cfgPath), true);
  58. $moduleName = $data["distribute-helper"]["module-name"];
  59. if(!$moduleName){
  60. logError('bad format. no module-name node exists.',$cfgPath);
  61. return false;
  62. }
  63. $content .= $moduleName . '=' . $pkg->getName() . ',' . $pkg->getPrettyVersion() . PHP_EOL;
  64. }
  65. //[CONFIG]节点内容
  66. //获取包信息
  67. $repoDir = dirname($vendorDir);
  68. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  69. $moduleConfig = $packageInfo['helper']['module-config'] ? : array();
  70. $content = '[CONFIG]' . PHP_EOL;
  71. foreach($moduleConfig as $i=>$cfg){
  72. $content .= $cfg['name'] . '=' . $cfg['value'] . PHP_EOL;
  73. }
  74. //保存文件
  75. file_put_contents(MODULEINI_PATH,$content);
  76. return true;
  77. }
  78. private static function postUpdateVendorPkgs($vendorDir,$vendorPkgs)
  79. {
  80. $count = count($vendorPkgs);
  81. foreach($vendorPkgs as $i=>$pkg){
  82. //获取包信息
  83. $repoDir = pathJoin($vendorDir,$pkg->getName());
  84. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  85. //获取当前包的module文件
  86. self::fetchModuleFiles($packageInfo);
  87. self::setProgressStep2($count,$i,1);
  88. //获取当前包的common文件
  89. self::fetchCommonFiles($packageInfo);
  90. self::setProgressStep2($count,$i,2);
  91. //拷贝当前包的config文件
  92. self::copyConfigFiles($packageInfo);
  93. self::setProgressStep2($count,$i,3);
  94. }
  95. }
  96. private static function postUpdateRootPkg($vendorDir,$pkg)
  97. {
  98. //获取包信息
  99. $repoDir = dirname($vendorDir);
  100. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  101. //注册全局路径
  102. self::copyCopyFiles($packageInfo);
  103. //注册全局路径
  104. self::registerPath($packageInfo);
  105. //注册自启动程序
  106. self::registerAutorun($packageInfo);
  107. }
  108. //初始化
  109. private static function initialize($vendorDir)
  110. {
  111. //创建.distribute目录
  112. $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
  113. if( !file_exists($distributeDir) ){
  114. createDirectory($distributeDir);
  115. }
  116. //设置日志文件路径
  117. if(! defined(LOG_PATH)){
  118. $logPath = pathJoin($distributeDir,'log.txt');
  119. define('LOG_PATH',$logPath);
  120. if(file_exists($logPath))
  121. unlink($logPath);
  122. }
  123. //设置进度文件路径
  124. if(! defined(PROGRESS_PATH)){
  125. $progressPath = pathJoin($distributeDir,'progress.txt');
  126. define('PROGRESS_PATH',$progressPath);
  127. if(file_exists($progressPath))
  128. unlink($progressPath);
  129. }
  130. //创建module目录
  131. $moduleDir = pathJoin(dirname($vendorDir),'module');
  132. if( !file_exists($moduleDir) ){
  133. createDirectory($moduleDir);
  134. }
  135. //设置 module.ini 路径
  136. if(! defined(MODULEINI_PATH)){
  137. $moduleiniPath = pathJoin($moduleDir,'module.ini');
  138. define('MODULEINI_PATH',$moduleiniPath);
  139. if(file_exists($moduleiniPath))
  140. unlink($moduleiniPath);
  141. }
  142. //获取部署配置信息
  143. $cfgPath = pathJoin($distributeDir,'config.json');
  144. if(! file_exists($cfgPath) ){
  145. logInfo(".distribute/config.json file not exists,use default config. ");
  146. self::$config = defaultConfig();
  147. }
  148. else{
  149. self::$config = loadConfig($cfgPath);
  150. if(!self::$config){
  151. logError("Failed to load distribute config",$cfgPath);
  152. return false;
  153. }
  154. logInfo("succeeded to decode config file -- .distribute/config.json");
  155. }
  156. return true;
  157. }
  158. private static function setProgressStep1($percent)
  159. {
  160. $progress = 0.1 * $percent;
  161. file_put_contents(PROGRESS_PATH,strval($progress));
  162. }
  163. private static function setProgressStep2($total,$index,$step)
  164. {
  165. $index = $index + $step/3.0;
  166. $progress = 10 + intval(90.0 * $index / $total);
  167. file_put_contents(PROGRESS_PATH,strval($progress));
  168. }
  169. private static function setProgressFailed($msg)
  170. {
  171. file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
  172. }
  173. private static function setProgressOK($restart)
  174. {
  175. if($restart){
  176. file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
  177. }
  178. else{
  179. file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
  180. }
  181. }
  182. //获取包相关信息
  183. private static function getPackageInfo($vendorDir,$repoDir,$pkg)
  184. {
  185. $config = self::$config;
  186. $cfgPath = pathJoin($repoDir,'composer.json');
  187. $data = json_decode(file_get_contents($cfgPath), true);
  188. $moduleName = $data["distribute-helper"]["module-name"];
  189. //获取安装目录
  190. $rootDir = dirname($vendorDir);
  191. $commonDir = pathJoin($rootDir,'common');
  192. $configDir = pathJoin($rootDir,'config',$moduleName);
  193. $moduleDir = pathJoin($rootDir,'module',$moduleName);
  194. //获取缓存目录
  195. $cacheDir = pathJoin($config['cache-dir'],str_replace('/','-',$pkg->getName()));
  196. //返回包的相关信息
  197. return array(
  198. "repoName" => $pkg->getName(),
  199. "version" => $pkg->getPrettyVersion(),
  200. "rootDir" => $rootDir,
  201. "cacheDir" => $cacheDir,
  202. "toolDir" => $config['tool-dir'],
  203. "ossHost" => $config['oss-host'],
  204. "moduleName" => $moduleName,
  205. "commonDir" => $commonDir,
  206. "configDir" => $configDir,
  207. "moduleDir" => $moduleDir,
  208. "helper" => $data["distribute-helper"],
  209. );
  210. }
  211. //下载包module文件
  212. private static function fetchModuleFiles($packageInfo)
  213. {
  214. $toolDir = $packageInfo['toolDir'];
  215. $cacheDir = $packageInfo['cacheDir'];
  216. $repoName = $packageInfo['repoName'];
  217. $version = $packageInfo['version'];
  218. $moduleDir = $packageInfo['moduleDir'];
  219. $ossHost = $packageInfo['ossHost'];
  220. $moduleFiles = $packageInfo['helper']['module-files'] ? : array();
  221. foreach($moduleFiles as $fileInfo){
  222. $fileName = $fileInfo['name'];
  223. //下载文件到缓存区
  224. $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
  225. if(! $filePath ){
  226. logError($repoName,"fetch module file failed",$fileName);
  227. return false;
  228. }
  229. logInfo("succeeded to fetch module file --",$repoName,$fileName);
  230. // 解压文件
  231. if( isZipFile($fileName) ){
  232. if(! unzipFile($toolDir,$filePath,$moduleDir)) {
  233. logError($repoName,"unzip module file failed",$fileName);
  234. return false;
  235. }
  236. }
  237. }
  238. return true;
  239. }
  240. //下载包common文件
  241. private static function fetchCommonFiles($packageInfo)
  242. {
  243. $toolDir = $packageInfo['toolDir'];
  244. $cacheDir = $packageInfo['cacheDir'];
  245. $repoName = $packageInfo['repoName'];
  246. $version = $packageInfo['version'];
  247. $commonDir = $packageInfo['commonDir'];
  248. $ossHost = $packageInfo['ossHost'];
  249. $commonFiles = $packageInfo['helper']['common-files']? : array();
  250. foreach($commonFiles as $fileInfo){
  251. $fileName = $fileInfo['name'];
  252. $target = $fileInfo['target'];
  253. //下载文件到缓存区
  254. $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
  255. if(! $filePath ){
  256. logError($repoName,"fetch common file failed",$fileName);
  257. return false;
  258. }
  259. logInfo("succeeded to fetch common file --",$repoName,$fileName);
  260. // 解压文件
  261. if( isZipFile($fileName) ){
  262. $targetDir = pathJoin($commonDir,$target);
  263. if(! unzipFile($toolDir,$filePath,$targetDir)) {
  264. logError($repoName,"unzip common file failed",$fileName);
  265. return false;
  266. }
  267. }
  268. }
  269. return true;
  270. }
  271. //拷贝config文件
  272. private static function copyConfigFiles($packageInfo)
  273. {
  274. $cacheDir = $packageInfo['cacheDir'];
  275. $repoName = $packageInfo['repoName'];
  276. $version = $packageInfo['version'];
  277. $configDir = $packageInfo['configDir'];
  278. $moduleDir = $packageInfo['moduleDir'];
  279. $configFiles = $packageInfo['helper']['config-files'] ? : array();
  280. foreach($configFiles as $fileInfo){
  281. $fileName = $fileInfo['name'];
  282. $targetName = $fileInfo['target'];
  283. $filePath = pathJoin($moduleDir,$fileName);
  284. if(! file_exists($filePath) ){
  285. logError($repoName,"config file not exists",$filePath);
  286. return false;
  287. }
  288. if(!$fileInfo['override'] && file_exists($targetPath)){
  289. logInfo($repoName,"skip copy config file",$targetPath);
  290. }
  291. $targetPath = pathJoin($configDir,$targetName);
  292. fileCopy($filePath,$targetPath);
  293. }
  294. return true;
  295. }
  296. //拷贝copy-files文件
  297. private static function copyCopyFiles($packageInfo)
  298. {
  299. $cacheDir = $packageInfo['cacheDir'];
  300. $repoName = $packageInfo['repoName'];
  301. $version = $packageInfo['version'];
  302. $rootDir = $packageInfo['rootDir'];
  303. $copyFiles = $packageInfo['helper']['copy-files'] ? : array();
  304. foreach($copyFiles as $fileInfo){
  305. $fileName = $fileInfo['name'];
  306. $targetName = $fileInfo['target'];
  307. $filePath = pathJoin($rootDir,$fileName);
  308. if(! file_exists($filePath) ){
  309. logError($repoName,"file not exists",$filePath);
  310. return false;
  311. }
  312. if(!$fileInfo['override'] && file_exists($targetPath)){
  313. logInfo($repoName,"skip copy file",$targetPath);
  314. }
  315. $targetPath = pathJoin($rootDir,$targetName);
  316. fileCopy($filePath,$targetPath);
  317. }
  318. return true;
  319. }
  320. private static function registerPath($packageInfo)
  321. {
  322. }
  323. private static function registerAutorun($packageInfo)
  324. {
  325. }
  326. }