Addon.php 7.8 KB

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