SaiPlugin.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace plugin\saiadmin\command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Style\SymfonyStyle;
  8. /**
  9. * SaiAdmin 插件创建命令
  10. * 用于创建 saiadmin 插件
  11. */
  12. class SaiPlugin extends Command
  13. {
  14. protected static $defaultName = 'sai:plugin';
  15. protected static $defaultDescription = '创建 SaiAdmin 插件';
  16. /**
  17. * @return void
  18. */
  19. protected function configure()
  20. {
  21. $this->addArgument('name', InputArgument::REQUIRED, 'App plugin name');
  22. }
  23. /**
  24. * @param InputInterface $input
  25. * @param OutputInterface $output
  26. * @return int
  27. */
  28. protected function execute(InputInterface $input, OutputInterface $output): int
  29. {
  30. $io = new SymfonyStyle($input, $output);
  31. $io->title('SaiAdmin 插件创建工具');
  32. $io->text('此命令用于创建基于webman的 saiadmin 插件, 用于扩展 saiadmin 框架功能!');
  33. $io->newLine();
  34. $name = $input->getArgument('name');
  35. $io->text("创建 SaiAdmin 插件 $name");
  36. if (strpos($name, '/') !== false) {
  37. $io->error('名称错误,名称必须不包含字符 \'/\'');
  38. return self::FAILURE;
  39. }
  40. // Create dir config/plugin/$name
  41. if (is_dir($plugin_config_path = base_path() . "/plugin/$name")) {
  42. $io->error("目录 $plugin_config_path 已存在");
  43. return self::FAILURE;
  44. }
  45. $this->createAll($name);
  46. $io->newLine();
  47. $io->success("SaiAdmin 插件创建成功!");
  48. return self::SUCCESS;
  49. }
  50. /**
  51. * @param $name
  52. * @return void
  53. */
  54. protected function createAll($name)
  55. {
  56. $base_path = base_path();
  57. $this->mkdir("$base_path/plugin/$name/app", 0777, true);
  58. $this->mkdir("$base_path/plugin/$name/app/admin/controller", 0777, true);
  59. $this->mkdir("$base_path/plugin/$name/app/admin/logic", 0777, true);
  60. $this->mkdir("$base_path/plugin/$name/app/api/controller", 0777, true);
  61. $this->mkdir("$base_path/plugin/$name/app/api/logic", 0777, true);
  62. $this->mkdir("$base_path/plugin/$name/app/cache", 0777, true);
  63. $this->mkdir("$base_path/plugin/$name/app/event", 0777, true);
  64. $this->mkdir("$base_path/plugin/$name/app/model", 0777, true);
  65. $this->mkdir("$base_path/plugin/$name/app/middleware", 0777, true);
  66. $this->mkdir("$base_path/plugin/$name/config", 0777, true);
  67. $this->createControllerFile("$base_path/plugin/$name/app/api/controller/IndexController.php", $name);
  68. $this->createFunctionsFile("$base_path/plugin/$name/app/functions.php");
  69. $this->createConfigFiles("$base_path/plugin/$name/config", $name);
  70. }
  71. /**
  72. * @param $path
  73. * @return void
  74. */
  75. protected function mkdir($path, $mode = 0777, $recursive = false)
  76. {
  77. if (is_dir($path)) {
  78. return;
  79. }
  80. echo "Create $path\r\n";
  81. mkdir($path, $mode, $recursive);
  82. }
  83. /**
  84. * @param $path
  85. * @param $name
  86. * @return void
  87. */
  88. protected function createControllerFile($path, $name)
  89. {
  90. $content = <<<EOF
  91. <?php
  92. namespace plugin\\$name\\app\\api\\controller;
  93. use plugin\\saiadmin\\basic\\OpenController;
  94. class IndexController extends OpenController
  95. {
  96. public function index()
  97. {
  98. return \$this->success([
  99. 'app' => '$name',
  100. 'version' => '1.0.0',
  101. ]);
  102. }
  103. }
  104. EOF;
  105. file_put_contents($path, $content);
  106. }
  107. /**
  108. * @param $file
  109. * @return void
  110. */
  111. protected function createFunctionsFile($file)
  112. {
  113. $content = <<<EOF
  114. <?php
  115. /**
  116. * Here is your custom functions.
  117. */
  118. EOF;
  119. file_put_contents($file, $content);
  120. }
  121. /**
  122. * @param $base
  123. * @param $name
  124. * @return void
  125. */
  126. protected function createConfigFiles($base, $name)
  127. {
  128. // app.php
  129. $content = <<<EOF
  130. <?php
  131. use support\\Request;
  132. return [
  133. 'debug' => true,
  134. 'controller_suffix' => 'Controller',
  135. 'controller_reuse' => false,
  136. 'version' => '1.0.0'
  137. ];
  138. EOF;
  139. file_put_contents("$base/app.php", $content);
  140. // autoload.php
  141. $content = <<<EOF
  142. <?php
  143. return [
  144. 'files' => [
  145. base_path() . '/plugin/$name/app/functions.php',
  146. ]
  147. ];
  148. EOF;
  149. file_put_contents("$base/autoload.php", $content);
  150. // container.php
  151. $content = <<<EOF
  152. <?php
  153. return new Webman\\Container;
  154. EOF;
  155. file_put_contents("$base/container.php", $content);
  156. // exception.php
  157. $content = <<<EOF
  158. <?php
  159. return [
  160. '' => \\plugin\\saiadmin\\app\\exception\\Handler::class,
  161. ];
  162. EOF;
  163. file_put_contents("$base/exception.php", $content);
  164. // log.php
  165. $content = <<<EOF
  166. <?php
  167. return [
  168. 'default' => [
  169. 'handlers' => [
  170. [
  171. 'class' => Monolog\\Handler\\RotatingFileHandler::class,
  172. 'constructor' => [
  173. runtime_path() . '/logs/$name.log',
  174. 7,
  175. Monolog\\Logger::DEBUG,
  176. ],
  177. 'formatter' => [
  178. 'class' => Monolog\\Formatter\\LineFormatter::class,
  179. 'constructor' => [null, 'Y-m-d H:i:s', true],
  180. ],
  181. ]
  182. ],
  183. ],
  184. ];
  185. EOF;
  186. file_put_contents("$base/log.php", $content);
  187. // middleware.php
  188. $content = <<<EOF
  189. <?php
  190. use plugin\saiadmin\app\middleware\SystemLog;
  191. use plugin\saiadmin\app\middleware\CheckLogin;
  192. use plugin\saiadmin\app\middleware\CheckAuth;
  193. return [
  194. 'admin' => [
  195. CheckLogin::class,
  196. CheckAuth::class,
  197. SystemLog::class,
  198. ],
  199. 'api' => [
  200. ]
  201. ];
  202. EOF;
  203. file_put_contents("$base/middleware.php", $content);
  204. // process.php
  205. $content = <<<EOF
  206. <?php
  207. return [];
  208. EOF;
  209. file_put_contents("$base/process.php", $content);
  210. // route.php
  211. $content = <<<EOF
  212. <?php
  213. use Webman\\Route;
  214. EOF;
  215. file_put_contents("$base/route.php", $content);
  216. // static.php
  217. $content = <<<EOF
  218. <?php
  219. return [
  220. 'enable' => true,
  221. 'middleware' => [], // Static file Middleware
  222. ];
  223. EOF;
  224. file_put_contents("$base/static.php", $content);
  225. // translation.php
  226. $content = <<<EOF
  227. <?php
  228. return [
  229. // Default language
  230. 'locale' => 'zh_CN',
  231. // Fallback language
  232. 'fallback_locale' => ['zh_CN', 'en'],
  233. // Folder where language files are stored
  234. 'path' => base_path() . "/plugin/$name/resource/translations",
  235. ];
  236. EOF;
  237. file_put_contents("$base/translation.php", $content);
  238. }
  239. }