Addon.php 7.7 KB

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