UploadCatchModuleCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @filename UploadCatchModuleCommand.php
  4. * @date 2020/7/11
  5. * @project https://github.com/yanwenwu/catch-admin
  6. * @document http://doc.catchadmin.com
  7. * @author JaguarJack <njphper@gmail.com>
  8. * @copyright By CatchAdmin
  9. * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
  10. */
  11. namespace catcher\command\install;
  12. use catcher\CatchAdmin;
  13. use catcher\facade\Http;
  14. use catcher\library\Compress;
  15. use think\console\Command;
  16. use think\console\Input;
  17. use think\console\input\Argument;
  18. use think\console\input\Option;
  19. use think\console\Output;
  20. class UploadCatchModuleCommand extends Command
  21. {
  22. protected $module;
  23. protected $path;
  24. /**
  25. * @var Compress
  26. */
  27. protected $compress;
  28. protected function configure()
  29. {
  30. $this->setName('upload:module')
  31. ->addArgument('module', Argument::REQUIRED, 'module name')
  32. ->addOption('path', '-p',Option::VALUE_OPTIONAL, 'path that you need')
  33. ->setDescription('install catch module');
  34. }
  35. protected function execute(Input $input, Output $output)
  36. {
  37. try {
  38. $this->module = $this->input->getArgument('module');
  39. $this->path = $this->getCompressPath($this->input->getOption('path'));
  40. $moduleInfo = $this->checking();
  41. // 打包项目
  42. $moduleZip = $this->compressModule();
  43. // 认证用户
  44. $name = $this->output->ask($this->input, 'please input your name');
  45. $password = $this->output->ask($this->input, 'please input your password');
  46. $token = $this->authenticate($name, $password);
  47. // 上传
  48. $this->upload($token, $moduleZip);
  49. $this->output->info('upload successfully!');
  50. }catch (\Throwable $e) {
  51. $this->error($e->getMessage(). ': Error happens at ' .$e->getFile() . ' '. $e->getLine() . '行');
  52. }
  53. }
  54. /**
  55. * 检查模块文件
  56. *
  57. * @time 2020年07月13日
  58. * @return mixed
  59. */
  60. public function checking()
  61. {
  62. if (!file_exists($this->path . 'module.json')) {
  63. $this->error('there is no module.json file');
  64. }
  65. if (!file_exists($this->path . 'route.php')) {
  66. $this->error('there is no route.php file');
  67. }
  68. $module = \json_decode(file_get_contents($this->path . 'module.json'), true);
  69. if (!isset($module['name']) && !$module['name']) {
  70. $this->error('module.json not set name');
  71. }
  72. if (!isset($module['version']) && !$module['name']) {
  73. $this->error('module.json not set version');
  74. }
  75. if (!isset($module['services']) && empty($module['services'])) {
  76. $this->error('module.json not set services');
  77. }
  78. $services = $module['services'];
  79. foreach ($services as $service) {
  80. $s = explode('\\', $service);
  81. $serviceName = array_pop($s);
  82. if (!file_exists($this->path. $serviceName . '.php')) {
  83. $this->error("[$serviceName] Service not found");
  84. }
  85. }
  86. $this->output->info('checking has no problem');
  87. return $module;
  88. }
  89. /**
  90. * 认证用户
  91. *
  92. * @time 2020年07月13日
  93. * @param $name
  94. * @param $password
  95. * @return mixed
  96. */
  97. protected function authenticate($name, $password)
  98. {
  99. $response = Http::form([
  100. 'username' => $name,
  101. 'password' => $password,
  102. ])->post($this->authenticateAddress());
  103. $data = $response->json();
  104. if ($data['code'] == 10000) {
  105. return $data['data'];
  106. }
  107. $this->error('login failed');
  108. }
  109. /**
  110. * 上传地址
  111. *
  112. * @time 2020年07月13日
  113. * @return mixed
  114. */
  115. protected function uploadAddress()
  116. {
  117. return env('API_URL') . '/upload/module';
  118. }
  119. protected function authenticateAddress()
  120. {
  121. return env('API_URL') . '/developer/authenticate';
  122. }
  123. /**
  124. * 上传
  125. *
  126. * @time 2020年07月13日
  127. * @param $token
  128. * @param $zip
  129. * @return bool
  130. */
  131. protected function upload($token, $zip)
  132. {
  133. return Http::token($token)
  134. ->attach('module', fopen($zip, 'r+'), pathinfo($zip, PATHINFO_FILENAME))
  135. ->post($this->uploadAddress())->ok();
  136. }
  137. /**
  138. * 打包模块
  139. *
  140. * @time 2020年07月13日
  141. * @return bool
  142. */
  143. protected function compressModule()
  144. {
  145. $composerZip = $this->compressZipPath() . $this->module . '_' . time() . '.zip';
  146. (new Compress())->moduleToZip($this->path, $composerZip);
  147. $this->output->info('compress module ' . $this->module . ' successfully');
  148. return $composerZip;
  149. }
  150. /**
  151. * 获取打包的path
  152. *
  153. * @time 2020年07月13日
  154. * @param $path
  155. * @return string
  156. */
  157. protected function getCompressPath($path)
  158. {
  159. if ($path) {
  160. return root_path($path) . $this->module. DIRECTORY_SEPARATOR;
  161. }
  162. return CatchAdmin::moduleDirectory($this->module);
  163. }
  164. /**
  165. * zip 打包路径
  166. *
  167. * @time 2020年07月13日
  168. * @return string
  169. */
  170. protected function compressZipPath()
  171. {
  172. return CatchAdmin::makeDirectory(runtime_path('catch'.DIRECTORY_SEPARATOR.'compress'));
  173. }
  174. /**
  175. * 输出错误信息
  176. *
  177. * @time 2020年07月13日
  178. * @param string $message
  179. * @return void
  180. */
  181. protected function error(string $message)
  182. {
  183. exit($this->output->error($message));
  184. }
  185. }