Install.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Library for ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace think\admin\command;
  16. use think\admin\Command;
  17. use think\admin\service\ModuleService;
  18. use think\console\Input;
  19. use think\console\input\Argument;
  20. use think\console\Output;
  21. /**
  22. * 插件更新安装指令
  23. * Class Install
  24. * @package think\admin\command
  25. */
  26. class Install extends Command
  27. {
  28. /**
  29. * 指定模块名称
  30. * @var string
  31. */
  32. protected $name;
  33. /**
  34. * 查询规则
  35. * @var array
  36. */
  37. protected $rules = [];
  38. /**
  39. * 忽略规则
  40. * @var array
  41. */
  42. protected $ignore = [];
  43. /**
  44. * 规则配置
  45. * @var array
  46. */
  47. protected $bind = [
  48. 'admin' => [
  49. 'rules' => ['think', 'app/admin'],
  50. 'ignore' => [],
  51. ],
  52. 'wechat' => [
  53. 'rules' => ['app/wechat'],
  54. 'ignore' => [],
  55. ],
  56. 'config' => [
  57. 'rules' => [
  58. 'config/app.php',
  59. 'config/log.php',
  60. 'config/route.php',
  61. 'config/trace.php',
  62. 'config/view.php',
  63. 'public/index.php',
  64. 'public/router.php',
  65. ],
  66. 'ignore' => [],
  67. ],
  68. 'static' => [
  69. 'rules' => [
  70. 'public/static/plugs',
  71. 'public/static/theme',
  72. 'public/static/admin.js',
  73. 'public/static/login.js',
  74. ],
  75. 'ignore' => [],
  76. ],
  77. ];
  78. protected function configure()
  79. {
  80. $this->setName('xadmin:install');
  81. $this->addArgument('name', Argument::OPTIONAL, 'ModuleName', '');
  82. $this->setDescription("Source code Install and Update for ThinkAdmin");
  83. }
  84. /**
  85. * @param Input $input
  86. * @param Output $output
  87. * @return void
  88. */
  89. protected function execute(Input $input, Output $output)
  90. {
  91. $this->name = trim($input->getArgument('name'));
  92. if (empty($this->name)) {
  93. $this->output->writeln('Module name of online install cannot be empty');
  94. } elseif ($this->name === 'all') {
  95. foreach ($this->bind as $bind) {
  96. $this->rules = array_merge($this->rules, $bind['rules']);
  97. $this->ignore = array_merge($this->ignore, $bind['ignore']);
  98. }
  99. $this->installFile() && $this->installData();
  100. } elseif (isset($this->bind[$this->name])) {
  101. $this->rules = $this->bind[$this->name]['rules'] ?? [];
  102. $this->ignore = $this->bind[$this->name]['ignore'] ?? [];
  103. $this->installFile() && $this->installData();
  104. } else {
  105. $this->output->writeln("The specified module {$this->name} is not configured with install rules");
  106. }
  107. }
  108. /**
  109. * 安装本地文件
  110. * @return boolean
  111. */
  112. private function installFile(): bool
  113. {
  114. $module = ModuleService::instance();
  115. $data = $module->grenerateDifference($this->rules, $this->ignore);
  116. if (empty($data)) {
  117. $this->output->writeln('No need to update the file if the file comparison is consistent');
  118. return false;
  119. }
  120. [$total, $count] = [count($data), 0];
  121. foreach ($data as $file) {
  122. [$state, $mode, $name] = $module->updateFileByDownload($file);
  123. if ($state) {
  124. if ($mode === 'add') $this->queue->message($total, ++$count, "--- {$name} add successfully");
  125. if ($mode === 'mod') $this->queue->message($total, ++$count, "--- {$name} update successfully");
  126. if ($mode === 'del') $this->queue->message($total, ++$count, "--- {$name} delete successfully");
  127. } else {
  128. if ($mode === 'add') $this->queue->message($total, ++$count, "--- {$name} add failed");
  129. if ($mode === 'mod') $this->queue->message($total, ++$count, "--- {$name} update failed");
  130. if ($mode === 'del') $this->queue->message($total, ++$count, "--- {$name} delete failed");
  131. }
  132. }
  133. return true;
  134. }
  135. /**
  136. * 安装数据库
  137. * @return boolean
  138. */
  139. protected function installData(): bool
  140. {
  141. return true;
  142. }
  143. }