Addon.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\admin\command;
  3. use think\addons\AddonException;
  4. use think\addons\Service;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\Exception;
  10. class Addon extends Command
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('addon')
  16. ->addOption('name', 'a', Option::VALUE_REQUIRED, 'addon name', null)
  17. ->addOption('action', 'c', Option::VALUE_REQUIRED, 'action(create/enable/disable/install/uninstall/refresh)', 'create')
  18. ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', null)
  19. ->setDescription('Addon manager');
  20. }
  21. protected function execute(Input $input, Output $output)
  22. {
  23. $name = $input->getOption('name') ?: '';
  24. $action = $input->getOption('action') ?: '';
  25. //强制覆盖
  26. $force = $input->getOption('force');
  27. include dirname(__DIR__) . DS . 'common.php';
  28. if (!$name)
  29. {
  30. throw new Exception('Addon name could not be empty');
  31. }
  32. if (!$action || !in_array($action, ['create', 'disable', 'enable', 'install', 'uninstall', 'refresh']))
  33. {
  34. throw new Exception('Please input correct action name');
  35. }
  36. // 查询一次SQL,判断连接是否正常
  37. Db::execute("SELECT 1");
  38. $addonDir = ADDON_PATH . $name;
  39. switch ($action)
  40. {
  41. case 'create':
  42. //非覆盖模式时如果存在则报错
  43. if (is_dir($addonDir) && !$force)
  44. {
  45. throw new Exception("addon already exists!\nIf you need to create again, use the parameter --force=true ");
  46. }
  47. //如果存在先移除
  48. if (is_dir($addonDir))
  49. {
  50. rmdirs($addonDir);
  51. }
  52. mkdir($addonDir);
  53. $data = [
  54. 'name' => $name,
  55. 'addon' => $name,
  56. 'addonClassName' => ucfirst($name)
  57. ];
  58. $this->writeToFile("addon", $data, $addonDir . DS . ucfirst($name) . '.php');
  59. $this->writeToFile("config", $data, $addonDir . DS . 'config.php');
  60. $this->writeToFile("info", $data, $addonDir . DS . 'info.ini');
  61. $output->info("Create Successed!");
  62. break;
  63. case 'disable':
  64. case 'enable':
  65. try
  66. {
  67. //调用启用、禁用的方法
  68. Service::$action($name, 0);
  69. }
  70. catch (AddonException $e)
  71. {
  72. if ($e->getCode() != -3)
  73. {
  74. throw new Exception($e->getMessage());
  75. }
  76. //如果有冲突文件则提醒
  77. $data = $e->getData();
  78. foreach ($data['conflictlist'] as $k => $v)
  79. {
  80. $output->warning($v);
  81. }
  82. $output->info("Are you sure you want to " . ($action == 'enable' ? 'override' : 'delete') . " all those files? Type 'yes' to continue: ");
  83. $line = fgets(STDIN);
  84. if (trim($line) != 'yes')
  85. {
  86. throw new Exception("Operation is aborted!");
  87. }
  88. //调用启用、禁用的方法
  89. Service::$action($name, 1);
  90. }
  91. catch (Exception $e)
  92. {
  93. throw new Exception($e->getMessage());
  94. }
  95. $output->info(ucfirst($action) . " Successed!");
  96. break;
  97. case 'install':
  98. //非覆盖模式时如果存在则报错
  99. if (is_dir($addonDir) && !$force)
  100. {
  101. throw new Exception("addon already exists!\nIf you need to install again, use the parameter --force=true ");
  102. }
  103. //如果存在先移除
  104. if (is_dir($addonDir))
  105. {
  106. rmdirs($addonDir);
  107. }
  108. try
  109. {
  110. Service::install($name, 0);
  111. }
  112. catch (AddonException $e)
  113. {
  114. if ($e->getCode() != -3)
  115. {
  116. throw new Exception($e->getMessage());
  117. }
  118. //如果有冲突文件则提醒
  119. $data = $e->getData();
  120. foreach ($data['conflictlist'] as $k => $v)
  121. {
  122. $output->warning($v);
  123. }
  124. $output->info("Are you sure you want to override all those files? Type 'yes' to continue: ");
  125. $line = fgets(STDIN);
  126. if (trim($line) != 'yes')
  127. {
  128. throw new Exception("Operation is aborted!");
  129. }
  130. Service::install($name, 1);
  131. }
  132. catch (Exception $e)
  133. {
  134. throw new Exception($e->getMessage());
  135. }
  136. $output->info("Install Successed!");
  137. break;
  138. case 'uninstall':
  139. //非覆盖模式时如果存在则报错
  140. if (!$force)
  141. {
  142. throw new Exception("If you need to uninstall addon, use the parameter --force=true ");
  143. }
  144. try
  145. {
  146. Service::uninstall($name, 0);
  147. }
  148. catch (AddonException $e)
  149. {
  150. if ($e->getCode() != -3)
  151. {
  152. throw new Exception($e->getMessage());
  153. }
  154. //如果有冲突文件则提醒
  155. $data = $e->getData();
  156. foreach ($data['conflictlist'] as $k => $v)
  157. {
  158. $output->warning($v);
  159. }
  160. $output->info("Are you sure you want to delete all those files? Type 'yes' to continue: ");
  161. $line = fgets(STDIN);
  162. if (trim($line) != 'yes')
  163. {
  164. throw new Exception("Operation is aborted!");
  165. }
  166. Service::uninstall($name, 1);
  167. }
  168. catch (Exception $e)
  169. {
  170. throw new Exception($e->getMessage());
  171. }
  172. $output->info("Uninstall Successed!");
  173. break;
  174. case 'refresh':
  175. Service::refresh();
  176. $output->info("Refresh Successed!");
  177. break;
  178. default :
  179. break;
  180. }
  181. }
  182. /**
  183. * 写入到文件
  184. * @param string $name
  185. * @param array $data
  186. * @param string $pathname
  187. * @return mixed
  188. */
  189. protected function writeToFile($name, $data, $pathname)
  190. {
  191. $search = $replace = [];
  192. foreach ($data as $k => $v)
  193. {
  194. $search[] = "{%{$k}%}";
  195. $replace[] = $v;
  196. }
  197. $stub = file_get_contents($this->getStub($name));
  198. $content = str_replace($search, $replace, $stub);
  199. if (!is_dir(dirname($pathname)))
  200. {
  201. mkdir(strtolower(dirname($pathname)), 0755, true);
  202. }
  203. return file_put_contents($pathname, $content);
  204. }
  205. /**
  206. * 获取基础模板
  207. * @param string $name
  208. * @return string
  209. */
  210. protected function getStub($name)
  211. {
  212. return __DIR__ . '/Addon/stubs/' . $name . '.stub';
  213. }
  214. }