Manage.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. <?php
  2. namespace ba\module;
  3. use ba\Depends;
  4. use think\Exception;
  5. use think\facade\Config;
  6. use FilesystemIterator;
  7. use RecursiveDirectoryIterator;
  8. use RecursiveIteratorIterator;
  9. /**
  10. * 模块管理类
  11. */
  12. class Manage
  13. {
  14. public const UNINSTALLED = 0;
  15. public const INSTALLED = 1;
  16. public const WAIT_INSTALL = 2;
  17. public const CONFLICT_PENDING = 3;
  18. public const DEPENDENT_WAIT_INSTALL = 4;
  19. public const DIRECTORY_OCCUPIED = 5;
  20. public const DISABLE = 6;
  21. /**
  22. * @var Manage 对象实例
  23. */
  24. protected static $instance;
  25. /**
  26. * @var string 安装目录
  27. */
  28. protected $installDir = null;
  29. /**
  30. * @var string 备份目录
  31. */
  32. protected $ebakDir = null;
  33. /**
  34. * @var string 模板唯一标识
  35. */
  36. protected $uid = null;
  37. /**
  38. * @var string 模板根目录
  39. */
  40. protected $modulesDir = null;
  41. /**
  42. * 初始化
  43. * @access public
  44. * @param string $uid
  45. * @return Manage
  46. */
  47. public static function instance(string $uid = ''): Manage
  48. {
  49. if (is_null(self::$instance)) {
  50. self::$instance = new static($uid);
  51. }
  52. return self::$instance;
  53. }
  54. public function __construct(string $uid)
  55. {
  56. $this->installDir = root_path() . 'modules' . DIRECTORY_SEPARATOR;
  57. $this->ebakDir = $this->installDir . 'ebak' . DIRECTORY_SEPARATOR;
  58. if (!is_dir($this->installDir)) {
  59. mkdir($this->installDir, 0755, true);
  60. }
  61. if (!is_dir($this->ebakDir)) {
  62. mkdir($this->ebakDir, 0755, true);
  63. }
  64. if ($uid) {
  65. $this->uid = $uid;
  66. $this->modulesDir = $this->installDir . $uid . DIRECTORY_SEPARATOR;
  67. }
  68. }
  69. public function getInstallState()
  70. {
  71. if (!is_dir($this->modulesDir)) {
  72. return self::UNINSTALLED;
  73. }
  74. $info = $this->getInfo();
  75. if ($info && isset($info['state'])) {
  76. return $info['state'];
  77. }
  78. // 目录已存在,但非正常的模块
  79. return dir_is_empty($this->modulesDir) ? self::UNINSTALLED : self::DIRECTORY_OCCUPIED;
  80. }
  81. public function download(string $token, int $orderId)
  82. {
  83. if (!$orderId) {
  84. throw new Exception('Order not found');
  85. }
  86. // 下载
  87. $sysVersion = Config::get('buildadmin.version');
  88. $installed = Server::installedList($this->installDir);
  89. foreach ($installed as $item) {
  90. $installedUids[] = $item['uid'];
  91. }
  92. unset($installed);
  93. $zipFile = Server::download($this->uid, $this->installDir, [
  94. 'sysVersion' => $sysVersion,
  95. 'ba-user-token' => $token,
  96. 'order_id' => $orderId,
  97. // 传递已安装模块,做互斥检测
  98. 'installed' => $installedUids ?? [],
  99. ]);
  100. // 删除旧版本代码
  101. deldir($this->modulesDir);
  102. // 解压
  103. Server::unzip($zipFile);
  104. // 删除下载的zip
  105. @unlink($zipFile);
  106. // 检查是否完整
  107. $this->checkPackage();
  108. // 设置为待安装状态
  109. $this->setInfo([
  110. 'state' => self::WAIT_INSTALL,
  111. ]);
  112. return $zipFile;
  113. }
  114. public function upload(string $file)
  115. {
  116. $file = path_transform(root_path() . 'public' . $file);
  117. if (!is_file($file)) {
  118. throw new Exception('Zip file not found');
  119. }
  120. $copyTo = $this->installDir . 'uploadTemp' . date('YmdHis') . '.zip';
  121. copy($file, $copyTo);
  122. // 解压
  123. $copyToDir = Server::unzip($copyTo);
  124. $copyToDir .= DIRECTORY_SEPARATOR;
  125. // 删除zip
  126. @unlink($copyTo);
  127. // 读取ini
  128. $info = Server::getIni($copyToDir);
  129. if (!isset($info['uid']) || !$info['uid']) {
  130. deldir($copyToDir);
  131. throw new Exception('Basic configuration of the Module is incomplete');
  132. }
  133. $this->uid = $info['uid'];
  134. $this->modulesDir = $this->installDir . $info['uid'] . DIRECTORY_SEPARATOR;
  135. if (is_dir($this->modulesDir)) {
  136. $info = $this->getInfo();
  137. if ($info && isset($info['uid'])) {
  138. deldir($copyToDir);
  139. throw new Exception('Module already exists');
  140. }
  141. if (!dir_is_empty($this->modulesDir)) {
  142. deldir($copyToDir);
  143. throw new Exception('The directory required by the module is occupied');
  144. }
  145. }
  146. rename($copyToDir, $this->modulesDir);
  147. // 检查是否完整
  148. $this->checkPackage();
  149. // 设置为待安装状态
  150. $this->setInfo([
  151. 'state' => self::WAIT_INSTALL,
  152. ]);
  153. return $info;
  154. }
  155. public function update(string $token, int $orderId)
  156. {
  157. $state = $this->getInstallState();
  158. if ($state != self::DISABLE) {
  159. throw new Exception('Please disable the module before updating');
  160. }
  161. $this->download($token, $orderId);
  162. // 标记需要执行更新脚本,并在安装请求执行(当前请求未自动加载到新文件不方便执行)
  163. $info = $this->getInfo();
  164. $info['update'] = 1;
  165. $this->setInfo([], $info);
  166. }
  167. /**
  168. * 安装模板或案例
  169. * @param string $token 用户token
  170. * @param int $orderId 订单号
  171. * @throws moduleException
  172. * @throws Exception
  173. */
  174. public function install(string $token, int $orderId)
  175. {
  176. $state = $this->getInstallState();
  177. if ($state == self::INSTALLED || $state == self::DIRECTORY_OCCUPIED || $state == self::DISABLE) {
  178. throw new Exception('Module already exists');
  179. }
  180. if ($state == self::UNINSTALLED) {
  181. $this->download($token, $orderId);
  182. }
  183. // 导入sql
  184. Server::importSql($this->modulesDir);
  185. // 如果是更新,先执行更新脚本
  186. $info = $this->getInfo();
  187. if (isset($info['update']) && $info['update']) {
  188. Server::execEvent($this->uid, 'update');
  189. unset($info['update']);
  190. $this->setInfo([], $info);
  191. }
  192. // 执行安装脚本
  193. Server::execEvent($this->uid, 'install');
  194. // 启用插件
  195. $this->enable('install');
  196. return $info;
  197. }
  198. public function uninstall()
  199. {
  200. $info = $this->getInfo();
  201. if ($info['state'] != self::DISABLE) {
  202. throw new moduleException('Please disable the module first', 0, [
  203. 'uid' => $this->uid,
  204. ]);
  205. }
  206. // 执行卸载脚本
  207. Server::execEvent($this->uid, 'uninstall');
  208. deldir($this->modulesDir);
  209. }
  210. /**
  211. * 启禁用模块
  212. */
  213. public function changeState(bool $state)
  214. {
  215. $info = $this->getInfo();
  216. $canDisable = [
  217. self::INSTALLED,
  218. self::CONFLICT_PENDING,
  219. self::DEPENDENT_WAIT_INSTALL,
  220. ];
  221. if (!$state) {
  222. if (!in_array($info['state'], $canDisable)) {
  223. throw new moduleException('The current state of the module cannot be set to disabled', 0, [
  224. 'uid' => $this->uid,
  225. 'state' => $info['state'],
  226. ]);
  227. }
  228. return $this->disable();
  229. }
  230. if ($info['state'] != self::DISABLE) {
  231. throw new moduleException('The current state of the module cannot be set to enabled', 0, [
  232. 'uid' => $this->uid,
  233. 'state' => $info['state'],
  234. ]);
  235. }
  236. $this->setInfo([
  237. 'state' => self::WAIT_INSTALL,
  238. ]);
  239. return $info;
  240. }
  241. public function enable(string $trigger)
  242. {
  243. // 安装 WebBootstrap
  244. Server::installWebBootstrap($this->uid, $this->modulesDir);
  245. $this->conflictHandle($trigger);
  246. // 执行启用脚本
  247. Server::execEvent($this->uid, 'enable');
  248. $this->dependUpdateHandle();
  249. }
  250. public function disable()
  251. {
  252. $update = request()->post("update/b", false);
  253. $confirmConflict = request()->post("confirmConflict/b", false);
  254. $dependConflictSolution = request()->post("dependConflictSolution/a", []);
  255. $info = $this->getInfo();
  256. $zipFile = $this->ebakDir . $this->uid . '-install.zip';
  257. $zipDir = false;
  258. if (is_file($zipFile)) {
  259. try {
  260. $zipDir = Server::unzip($zipFile);
  261. } catch (moduleException|Exception $e) {
  262. // skip
  263. }
  264. }
  265. $conflictFile = Server::getFileList($this->modulesDir, true);
  266. $dependConflict = $this->disableDependCheck();
  267. if (($conflictFile || !self::emptyArray($dependConflict)) && !$confirmConflict) {
  268. $dependConflictTemp = [];
  269. foreach ($dependConflict as $env => $item) {
  270. $dev = !(stripos($env, 'dev') === false);
  271. foreach ($item as $depend => $v) {
  272. $dependConflictTemp[] = [
  273. 'env' => $env,
  274. 'depend' => $depend,
  275. 'dependTitle' => $depend . ' ' . $v,
  276. 'solution' => 'delete',
  277. ];
  278. }
  279. }
  280. throw new moduleException('Module file updated', -1, [
  281. 'uid' => $this->uid,
  282. 'conflictFile' => $conflictFile,
  283. 'dependConflict' => $dependConflictTemp,
  284. ]);
  285. }
  286. // 执行禁用脚本
  287. Server::execEvent($this->uid, 'disable', ['update' => $update]);
  288. // 是否需要备份依赖?
  289. $delNpmDepend = false;
  290. $delNuxtNpmDepend = false;
  291. $delComposerDepend = false;
  292. foreach ($dependConflictSolution as $env => $depends) {
  293. if (!$depends) continue;
  294. if ($env == 'require' || $env == 'require-dev') {
  295. $delComposerDepend = true;
  296. } elseif ($env == 'dependencies' || $env == 'devDependencies') {
  297. $delNpmDepend = true;
  298. } elseif ($env == 'nuxtDependencies' || $env == 'nuxtDevDependencies') {
  299. $delNuxtNpmDepend = true;
  300. }
  301. }
  302. // 备份
  303. $dependWaitInstall = [];
  304. if ($delComposerDepend) {
  305. $conflictFile[] = 'composer.json';
  306. $dependWaitInstall[] = [
  307. 'pm' => false,
  308. 'command' => 'composer.update',
  309. 'type' => 'composer_dependent_wait_install',
  310. ];
  311. }
  312. if ($delNpmDepend) {
  313. $conflictFile[] = 'web' . DIRECTORY_SEPARATOR . 'package.json';
  314. $dependWaitInstall[] = [
  315. 'pm' => true,
  316. 'command' => 'web-install',
  317. 'type' => 'npm_dependent_wait_install',
  318. ];
  319. }
  320. if ($delNuxtNpmDepend) {
  321. $conflictFile[] = 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json';
  322. $dependWaitInstall[] = [
  323. 'pm' => true,
  324. 'command' => 'nuxt-install',
  325. 'type' => 'nuxt_npm_dependent_wait_install',
  326. ];
  327. }
  328. if ($conflictFile) {
  329. $ebakZip = $this->ebakDir . $this->uid . '-disable-' . date('YmdHis') . '.zip';
  330. Server::createZip($conflictFile, $ebakZip);
  331. }
  332. // 删除依赖
  333. $serverDepend = new Depends(root_path() . 'composer.json', 'composer');
  334. $webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
  335. $webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
  336. foreach ($dependConflictSolution as $env => $depends) {
  337. if (!$depends) continue;
  338. $dev = !(stripos($env, 'dev') === false);
  339. if ($env == 'require' || $env == 'require-dev') {
  340. $serverDepend->removeDepends($depends, $dev);
  341. } elseif ($env == 'dependencies' || $env == 'devDependencies') {
  342. $webDep->removeDepends($depends, $dev);
  343. } elseif ($env == 'nuxtDependencies' || $env == 'nuxtDevDependencies') {
  344. $webNuxtDep->removeDepends($depends, $dev);
  345. }
  346. }
  347. // 删除模块文件
  348. $protectedFiles = Server::getConfig($this->modulesDir, 'protectedFiles');
  349. foreach ($protectedFiles as &$protectedFile) {
  350. $protectedFile = path_transform(root_path() . $protectedFile);
  351. }
  352. $moduleFile = Server::getFileList($this->modulesDir);
  353. foreach ($moduleFile as $item) {
  354. $file = path_transform(root_path() . $item);
  355. if (in_array($file, $protectedFiles)) {
  356. continue;
  357. }
  358. if (file_exists($file)) {
  359. unlink($file);
  360. }
  361. del_empty_dir(dirname($file));
  362. }
  363. // 恢复备份文件
  364. if ($zipDir) {
  365. foreach (
  366. $iterator = new RecursiveIteratorIterator(
  367. new RecursiveDirectoryIterator($zipDir, FilesystemIterator::SKIP_DOTS),
  368. RecursiveIteratorIterator::SELF_FIRST
  369. ) as $item
  370. ) {
  371. $ebakFile = path_transform(root_path() . $iterator->getSubPathName());
  372. if ($item->isDir()) {
  373. if (!is_dir($ebakFile)) {
  374. mkdir($ebakFile, 0755, true);
  375. }
  376. } else {
  377. if ($ebakFile != path_transform(root_path() . 'composer.json') && $ebakFile != path_transform(root_path() . 'web/package.json')) {
  378. copy($item, $ebakFile);
  379. }
  380. }
  381. }
  382. }
  383. // 删除解压后的备份文件
  384. deldir($zipDir);
  385. // 卸载 WebBootstrap
  386. Server::uninstallWebBootstrap($this->uid, $this->modulesDir);
  387. $this->setInfo([
  388. 'state' => self::DISABLE,
  389. ]);
  390. if ($update) {
  391. $token = request()->post("token/s", '');
  392. $order = request()->post("order/d", 0);
  393. $this->update($token, $order);
  394. throw new moduleException('update', -3, [
  395. 'uid' => $this->uid,
  396. ]);
  397. }
  398. if ($dependWaitInstall) {
  399. throw new moduleException('dependent wait install', -2, [
  400. 'uid' => $this->uid,
  401. 'wait_install' => $dependWaitInstall,
  402. ]);
  403. }
  404. return $info;
  405. }
  406. /**
  407. * 处理依赖和文件冲突,并完成与前端的冲突处理交互
  408. * @throws moduleException|Exception
  409. */
  410. public function conflictHandle(string $trigger): bool
  411. {
  412. $info = $this->getInfo();
  413. if ($info['state'] != self::WAIT_INSTALL && $info['state'] != self::CONFLICT_PENDING) {
  414. return false;
  415. }
  416. $fileConflict = Server::getFileList($this->modulesDir, true);// 文件冲突
  417. $dependConflict = Server::dependConflictCheck($this->modulesDir);// 依赖冲突
  418. $installFiles = Server::getFileList($this->modulesDir);// 待安装文件
  419. $depends = Server::getDepend($this->modulesDir);// 待安装依赖
  420. $coverFiles = [];// 要覆盖的文件-备份
  421. $discardFiles = [];// 抛弃的文件-复制时不覆盖
  422. $serverDep = new Depends(root_path() . 'composer.json', 'composer');
  423. $webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
  424. $webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
  425. if ($fileConflict || !self::emptyArray($dependConflict)) {
  426. $extend = request()->post('extend/a', []);
  427. if (!$extend) {
  428. // 发现冲突->手动处理->转换为方便前端使用的格式
  429. $fileConflictTemp = [];
  430. foreach ($fileConflict as $key => $item) {
  431. $fileConflictTemp[$key] = [
  432. 'newFile' => $this->uid . DIRECTORY_SEPARATOR . $item,
  433. 'oldFile' => $item,
  434. 'solution' => 'cover',
  435. ];
  436. }
  437. $dependConflictTemp = [];
  438. foreach ($dependConflict as $env => $item) {
  439. $dev = !(stripos($env, 'dev') === false);
  440. foreach ($item as $depend => $v) {
  441. $oldDepend = '';
  442. if (in_array($env, ['require', 'require-dev'])) {
  443. $oldDepend = $depend . ' ' . $serverDep->hasDepend($depend, $dev);
  444. } elseif (in_array($env, ['dependencies', 'devDependencies'])) {
  445. $oldDepend = $depend . ' ' . $webDep->hasDepend($depend, $dev);
  446. } elseif (in_array($env, ['nuxtDependencies', 'nuxtDevDependencies'])) {
  447. $oldDepend = $depend . ' ' . $webNuxtDep->hasDepend($depend, $dev);
  448. }
  449. $dependConflictTemp[] = [
  450. 'env' => $env,
  451. 'newDepend' => $depend . ' ' . $v,
  452. 'oldDepend' => $oldDepend,
  453. 'depend' => $depend,
  454. 'solution' => 'cover',
  455. ];
  456. }
  457. }
  458. $this->setInfo([
  459. 'state' => self::CONFLICT_PENDING,
  460. ]);
  461. throw new moduleException('Module file conflicts', -1, [
  462. 'fileConflict' => $fileConflictTemp,
  463. 'dependConflict' => $dependConflictTemp,
  464. 'uid' => $this->uid,
  465. 'state' => self::CONFLICT_PENDING,
  466. ]);
  467. }
  468. // 处理冲突
  469. if ($fileConflict && isset($extend['fileConflict'])) {
  470. foreach ($installFiles as $ikey => $installFile) {
  471. if (isset($extend['fileConflict'][$installFile])) {
  472. if ($extend['fileConflict'][$installFile] == 'discard') {
  473. $discardFiles[] = $installFile;
  474. unset($installFiles[$ikey]);
  475. } else {
  476. $coverFiles[] = $installFile;
  477. }
  478. }
  479. }
  480. }
  481. if (!self::emptyArray($dependConflict) && isset($extend['dependConflict'])) {
  482. foreach ($depends as $fKey => $fItem) {
  483. foreach ($fItem as $cKey => $cItem) {
  484. if (isset($extend['dependConflict'][$fKey][$cKey])) {
  485. if ($extend['dependConflict'][$fKey][$cKey] == 'discard') {
  486. unset($depends[$fKey][$cKey]);
  487. }
  488. }
  489. }
  490. }
  491. }
  492. }
  493. // 如果有依赖更新,增加要备份的文件
  494. if ($depends) {
  495. foreach ($depends as $key => $item) {
  496. if (!$item) {
  497. continue;
  498. }
  499. if ($key == 'require' || $key == 'require-dev') {
  500. $coverFiles[] = 'composer.json';
  501. continue;
  502. }
  503. if ($key == 'dependencies' || $key == 'devDependencies') {
  504. $coverFiles[] = 'web' . DIRECTORY_SEPARATOR . 'package.json';
  505. }
  506. }
  507. }
  508. // 备份将被覆盖的文件
  509. if ($coverFiles) {
  510. $ebakZip = $trigger == 'install' ? $this->ebakDir . $this->uid . '-install.zip' : $this->ebakDir . $this->uid . '-cover-' . date('YmdHis') . '.zip';
  511. Server::createZip($coverFiles, $ebakZip);
  512. }
  513. if ($depends) {
  514. $npm = false;
  515. $composer = false;
  516. $nuxtNpm = false;
  517. foreach ($depends as $key => $item) {
  518. if (!$item) {
  519. continue;
  520. }
  521. if ($key == 'require') {
  522. $composer = true;
  523. $serverDep->addDepends($item, false, true);
  524. } elseif ($key == 'require-dev') {
  525. $composer = true;
  526. $serverDep->addDepends($item, true, true);
  527. } elseif ($key == 'dependencies') {
  528. $npm = true;
  529. $webDep->addDepends($item, false, true);
  530. } elseif ($key == 'devDependencies') {
  531. $npm = true;
  532. $webDep->addDepends($item, true, true);
  533. } elseif ($key == 'nuxtDependencies') {
  534. $nuxtNpm = true;
  535. $webNuxtDep->addDepends($item, false, true);
  536. } elseif ($key == 'nuxtDevDependencies') {
  537. $nuxtNpm = true;
  538. $webNuxtDep->addDepends($item, true, true);
  539. }
  540. }
  541. if ($npm) {
  542. $info['npm_dependent_wait_install'] = 1;
  543. $info['state'] = self::DEPENDENT_WAIT_INSTALL;
  544. }
  545. if ($composer) {
  546. $info['composer_dependent_wait_install'] = 1;
  547. $info['state'] = self::DEPENDENT_WAIT_INSTALL;
  548. }
  549. if ($nuxtNpm) {
  550. $info['nuxt_npm_dependent_wait_install'] = 1;
  551. $info['state'] = self::DEPENDENT_WAIT_INSTALL;
  552. }
  553. if ($info['state'] != self::DEPENDENT_WAIT_INSTALL) {
  554. // 无冲突
  555. $this->setInfo([
  556. 'state' => self::INSTALLED,
  557. ]);
  558. } else {
  559. $this->setInfo([], $info);
  560. }
  561. } else {
  562. // 无冲突
  563. $this->setInfo([
  564. 'state' => self::INSTALLED,
  565. ]);
  566. }
  567. // 复制文件
  568. $overwriteDir = Server::getOverwriteDir();
  569. foreach ($overwriteDir as $dirItem) {
  570. $baseDir = $this->modulesDir . $dirItem;
  571. $destDir = root_path() . $dirItem;
  572. if (!is_dir($baseDir)) {
  573. continue;
  574. }
  575. foreach (
  576. $iterator = new RecursiveIteratorIterator(
  577. new RecursiveDirectoryIterator($baseDir, FilesystemIterator::SKIP_DOTS),
  578. RecursiveIteratorIterator::SELF_FIRST
  579. ) as $item
  580. ) {
  581. $destDirItem = $destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
  582. if ($item->isDir()) {
  583. $this->createDirectory($destDirItem);
  584. } else {
  585. if (!in_array(str_replace(root_path(), '', $destDirItem), $discardFiles)) {
  586. $this->createDirectory(dirname($destDirItem));
  587. copy($item, $destDirItem);
  588. }
  589. }
  590. }
  591. }
  592. return true;
  593. }
  594. public function createDirectory(string $dirName)
  595. {
  596. if (!is_dir($dirName)) {
  597. mkdir($dirName, 0755, true);
  598. }
  599. }
  600. public function dependUpdateHandle()
  601. {
  602. $info = $this->getInfo();
  603. if ($info['state'] == self::DEPENDENT_WAIT_INSTALL) {
  604. $waitInstall = [];
  605. if (isset($info['composer_dependent_wait_install'])) {
  606. $waitInstall[] = 'composer_dependent_wait_install';
  607. }
  608. if (isset($info['npm_dependent_wait_install'])) {
  609. $waitInstall[] = 'npm_dependent_wait_install';
  610. }
  611. if (isset($info['nuxt_npm_dependent_wait_install'])) {
  612. $waitInstall[] = 'nuxt_npm_dependent_wait_install';
  613. }
  614. if ($waitInstall) {
  615. throw new moduleException('dependent wait install', -2, [
  616. 'uid' => $this->uid,
  617. 'state' => self::DEPENDENT_WAIT_INSTALL,
  618. 'wait_install' => $waitInstall,
  619. ]);
  620. } else {
  621. $this->setInfo([
  622. 'state' => self::INSTALLED,
  623. ]);
  624. }
  625. }
  626. }
  627. public function dependentInstallComplete(string $type)
  628. {
  629. $info = $this->getInfo();
  630. if ($info['state'] == self::DEPENDENT_WAIT_INSTALL) {
  631. if ($type == 'npm') {
  632. unset($info['npm_dependent_wait_install']);
  633. }
  634. if ($type == 'nuxt_npm') {
  635. unset($info['nuxt_npm_dependent_wait_install']);
  636. }
  637. if ($type == 'composer') {
  638. unset($info['composer_dependent_wait_install']);
  639. }
  640. if ($type == 'all') {
  641. unset($info['npm_dependent_wait_install'], $info['composer_dependent_wait_install'], $info['nuxt_npm_dependent_wait_install']);
  642. }
  643. if (!isset($info['npm_dependent_wait_install']) && !isset($info['composer_dependent_wait_install']) && !isset($info['nuxt_npm_dependent_wait_install'])) {
  644. $info['state'] = self::INSTALLED;
  645. }
  646. $this->setInfo([], $info);
  647. }
  648. }
  649. public function disableDependCheck()
  650. {
  651. // 读取模块所有依赖
  652. $depend = Server::getDepend($this->modulesDir);
  653. if (!$depend) {
  654. return [];
  655. }
  656. // 读取所有依赖中,系统上已经安装的依赖
  657. $serverDep = new Depends(root_path() . 'composer.json', 'composer');
  658. $webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
  659. $webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
  660. foreach ($depend as $key => $depends) {
  661. $dev = !(stripos($key, 'dev') === false);
  662. if ($key == 'require' || $key == 'require-dev') {
  663. foreach ($depends as $dependKey => $dependItem) {
  664. if (!$serverDep->hasDepend($dependKey, $dev)) {
  665. unset($depends[$dependKey]);
  666. }
  667. }
  668. $depend[$key] = $depends;
  669. } elseif ($key == 'dependencies' || $key == 'devDependencies') {
  670. foreach ($depends as $dependKey => $dependItem) {
  671. if (!$webDep->hasDepend($dependKey, $dev)) {
  672. unset($depends[$dependKey]);
  673. }
  674. }
  675. $depend[$key] = $depends;
  676. } elseif ($key == 'nuxtDependencies' || $key == 'nuxtDevDependencies') {
  677. foreach ($depends as $dependKey => $dependItem) {
  678. if (!$webNuxtDep->hasDepend($dependKey, $dev)) {
  679. unset($depends[$dependKey]);
  680. }
  681. }
  682. $depend[$key] = $depends;
  683. }
  684. }
  685. return $depend;
  686. }
  687. public function disableDependConflictCheck(string $ebakDir): array
  688. {
  689. if (!$ebakDir) {
  690. return [];
  691. }
  692. $dependFile = [
  693. path_transform($ebakDir . DIRECTORY_SEPARATOR . 'composer.json') => [
  694. 'path' => path_transform(root_path() . 'composer.json'),
  695. 'type' => 'composer',
  696. ],
  697. path_transform($ebakDir . DIRECTORY_SEPARATOR . 'web/package.json') => [
  698. 'path' => path_transform(root_path() . 'web/package.json'),
  699. 'type' => 'npm',
  700. ],
  701. ];
  702. $conflict = [];
  703. foreach ($dependFile as $key => $item) {
  704. if (is_file($key) && (filesize($key) != filesize($item['path']) || md5_file($key) != md5_file($item['path']))) {
  705. $conflict[] = $item['type'];
  706. }
  707. }
  708. return $conflict;
  709. }
  710. public function checkPackage(): bool
  711. {
  712. if (!is_dir($this->modulesDir)) {
  713. throw new Exception('Module package file does not exist');
  714. }
  715. $info = $this->getInfo();
  716. $infoKeys = ['uid', 'title', 'intro', 'author', 'version', 'state'];
  717. foreach ($infoKeys as $value) {
  718. if (!array_key_exists($value, $info)) {
  719. deldir($this->modulesDir);
  720. throw new Exception('Basic configuration of the Module is incomplete');
  721. }
  722. }
  723. return true;
  724. }
  725. public function getInfo()
  726. {
  727. return Server::getIni($this->modulesDir);
  728. }
  729. public function setInfo(array $kv = [], array $arr = []): bool
  730. {
  731. if ($kv) {
  732. $info = $this->getInfo();
  733. foreach ($kv as $k => $v) {
  734. $info[$k] = $v;
  735. }
  736. return Server::setIni($this->modulesDir, $info);
  737. } elseif ($arr) {
  738. return Server::setIni($this->modulesDir, $arr);
  739. }
  740. throw new Exception('Parameter error');
  741. }
  742. /**
  743. * 检查多维数组是否为空
  744. */
  745. public static function emptyArray($arr)
  746. {
  747. $empty = true;
  748. foreach ($arr as $item) {
  749. if (is_array($item)) {
  750. $empty = self::emptyArray($item);
  751. if (!$empty) return false;
  752. } elseif ($item) {
  753. return false;
  754. }
  755. }
  756. return $empty;
  757. }
  758. }