AppPluginCreateCommand.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. namespace Webman\Console\Commands;
  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\Input\InputOption;
  8. use Webman\Console\Util;
  9. class AppPluginCreateCommand extends Command
  10. {
  11. protected static $defaultName = 'app-plugin:create';
  12. protected static $defaultDescription = 'App Plugin Create';
  13. /**
  14. * @return void
  15. */
  16. protected function configure()
  17. {
  18. $this->addArgument('name', InputArgument::REQUIRED, 'App plugin name');
  19. }
  20. /**
  21. * @param InputInterface $input
  22. * @param OutputInterface $output
  23. * @return int
  24. */
  25. protected function execute(InputInterface $input, OutputInterface $output): int
  26. {
  27. $name = $input->getArgument('name');
  28. $output->writeln("Create App Plugin $name");
  29. if (strpos($name, '/') !== false) {
  30. $output->writeln('<error>Bad name, name must not contain character \'/\'</error>');
  31. return self::FAILURE;
  32. }
  33. // Create dir config/plugin/$name
  34. if (is_dir($plugin_config_path = base_path()."/plugin/$name")) {
  35. $output->writeln("<error>Dir $plugin_config_path already exists</error>");
  36. return self::FAILURE;
  37. }
  38. $this->createAll($name);
  39. return self::SUCCESS;
  40. }
  41. /**
  42. * @param $name
  43. * @return void
  44. */
  45. protected function createAll($name)
  46. {
  47. $base_path = base_path();
  48. $this->mkdir("$base_path/plugin/$name/app/controller", 0777, true);
  49. $this->mkdir("$base_path/plugin/$name/app/model", 0777, true);
  50. $this->mkdir("$base_path/plugin/$name/app/middleware", 0777, true);
  51. $this->mkdir("$base_path/plugin/$name/app/view/index", 0777, true);
  52. $this->mkdir("$base_path/plugin/$name/config", 0777, true);
  53. $this->mkdir("$base_path/plugin/$name/public", 0777, true);
  54. $this->mkdir("$base_path/plugin/$name/api", 0777, true);
  55. $this->createFunctionsFile("$base_path/plugin/$name/app/functions.php");
  56. $this->createControllerFile("$base_path/plugin/$name/app/controller/IndexController.php", $name);
  57. $this->createViewFile("$base_path/plugin/$name/app/view/index/index.html");
  58. $this->createConfigFiles("$base_path/plugin/$name/config", $name);
  59. $this->createApiFiles("$base_path/plugin/$name/api", $name);
  60. $this->createInstallSqlFile("$base_path/plugin/$name/install.sql");
  61. }
  62. /**
  63. * @param $path
  64. * @return void
  65. */
  66. protected function mkdir($path)
  67. {
  68. if (is_dir($path)) {
  69. return;
  70. }
  71. echo "Create $path\r\n";
  72. mkdir($path, 0777, true);
  73. }
  74. /**
  75. * @param $path
  76. * @param $name
  77. * @return void
  78. */
  79. protected function createControllerFile($path, $name)
  80. {
  81. $content = <<<EOF
  82. <?php
  83. namespace plugin\\$name\\app\\controller;
  84. use support\\Request;
  85. class IndexController
  86. {
  87. public function index()
  88. {
  89. return view('index/index', ['name' => '$name']);
  90. }
  91. }
  92. EOF;
  93. file_put_contents($path, $content);
  94. }
  95. /**
  96. * @param $path
  97. * @return void
  98. */
  99. protected function createViewFile($path)
  100. {
  101. $content = <<<EOF
  102. <!doctype html>
  103. <html>
  104. <head>
  105. <meta charset="utf-8">
  106. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  107. <meta name="viewport" content="width=device-width, initial-scale=1">
  108. <link rel="shortcut icon" href="/favicon.ico"/>
  109. <title>webman app plugin</title>
  110. </head>
  111. <body>
  112. hello <?=htmlspecialchars(\$name)?>
  113. </body>
  114. </html>
  115. EOF;
  116. file_put_contents($path, $content);
  117. }
  118. /**
  119. * @param $file
  120. * @return void
  121. */
  122. protected function createFunctionsFile($file)
  123. {
  124. $content = <<<EOF
  125. <?php
  126. /**
  127. * Here is your custom functions.
  128. */
  129. EOF;
  130. file_put_contents($file, $content);
  131. }
  132. /**
  133. * @param $base
  134. * @param $name
  135. * @return void
  136. */
  137. protected function createApiFiles($base, $name)
  138. {
  139. $content = <<<EOF
  140. <?php
  141. namespace plugin\\$name\api;
  142. use plugin\admin\api\Menu;
  143. use support\Db;
  144. use Throwable;
  145. class Install
  146. {
  147. /**
  148. * 数据库连接
  149. */
  150. protected static \$connection = 'plugin.admin.mysql';
  151. /**
  152. * 安装
  153. *
  154. * @param \$version
  155. * @return void
  156. */
  157. public static function install(\$version)
  158. {
  159. // 安装数据库
  160. static::installSql();
  161. // 导入菜单
  162. if(\$menus = static::getMenus()) {
  163. Menu::import(\$menus);
  164. }
  165. }
  166. /**
  167. * 卸载
  168. *
  169. * @param \$version
  170. * @return void
  171. */
  172. public static function uninstall(\$version)
  173. {
  174. // 删除菜单
  175. foreach (static::getMenus() as \$menu) {
  176. Menu::delete(\$menu['key']);
  177. }
  178. // 卸载数据库
  179. static::uninstallSql();
  180. }
  181. /**
  182. * 更新
  183. *
  184. * @param \$from_version
  185. * @param \$to_version
  186. * @param \$context
  187. * @return void
  188. */
  189. public static function update(\$from_version, \$to_version, \$context = null)
  190. {
  191. // 删除不用的菜单
  192. if (isset(\$context['previous_menus'])) {
  193. static::removeUnnecessaryMenus(\$context['previous_menus']);
  194. }
  195. // 安装数据库
  196. static::installSql();
  197. // 导入新菜单
  198. if (\$menus = static::getMenus()) {
  199. Menu::import(\$menus);
  200. }
  201. // 执行更新操作
  202. \$update_file = __DIR__ . '/../update.php';
  203. if (is_file(\$update_file)) {
  204. include \$update_file;
  205. }
  206. }
  207. /**
  208. * 更新前数据收集等
  209. *
  210. * @param \$from_version
  211. * @param \$to_version
  212. * @return array|array[]
  213. */
  214. public static function beforeUpdate(\$from_version, \$to_version)
  215. {
  216. // 在更新之前获得老菜单,通过context传递给 update
  217. return ['previous_menus' => static::getMenus()];
  218. }
  219. /**
  220. * 获取菜单
  221. *
  222. * @return array|mixed
  223. */
  224. public static function getMenus()
  225. {
  226. clearstatcache();
  227. if (is_file(\$menu_file = __DIR__ . '/../config/menu.php')) {
  228. \$menus = include \$menu_file;
  229. return \$menus ?: [];
  230. }
  231. return [];
  232. }
  233. /**
  234. * 删除不需要的菜单
  235. *
  236. * @param \$previous_menus
  237. * @return void
  238. */
  239. public static function removeUnnecessaryMenus(\$previous_menus)
  240. {
  241. \$menus_to_remove = array_diff(Menu::column(\$previous_menus, 'name'), Menu::column(static::getMenus(), 'name'));
  242. foreach (\$menus_to_remove as \$name) {
  243. Menu::delete(\$name);
  244. }
  245. }
  246. /**
  247. * 安装SQL
  248. *
  249. * @return void
  250. */
  251. protected static function installSql()
  252. {
  253. static::importSql(__DIR__ . '/../install.sql');
  254. }
  255. /**
  256. * 卸载SQL
  257. *
  258. * @return void
  259. */
  260. protected static function uninstallSql() {
  261. // 如果卸载数据库文件存在责直接使用
  262. \$uninstallSqlFile = __DIR__ . '/../uninstall.sql';
  263. if (is_file(\$uninstallSqlFile)) {
  264. static::importSql(\$uninstallSqlFile);
  265. return;
  266. }
  267. // 否则根据install.sql生成卸载数据库文件uninstall.sql
  268. \$installSqlFile = __DIR__ . '/../install.sql';
  269. if (!is_file(\$installSqlFile)) {
  270. return;
  271. }
  272. \$installSql = file_get_contents(\$installSqlFile);
  273. preg_match_all('/CREATE TABLE `(.+?)`/si', \$installSql, \$matches);
  274. \$dropSql = '';
  275. foreach (\$matches[1] as \$table) {
  276. \$dropSql .= "DROP TABLE IF EXISTS `\$table`;\\n";
  277. }
  278. file_put_contents(\$uninstallSqlFile, \$dropSql);
  279. static::importSql(\$uninstallSqlFile);
  280. unlink(\$uninstallSqlFile);
  281. }
  282. /**
  283. * 导入数据库
  284. *
  285. * @return void
  286. */
  287. public static function importSql(\$mysqlDumpFile)
  288. {
  289. if (!\$mysqlDumpFile || !is_file(\$mysqlDumpFile)) {
  290. return;
  291. }
  292. foreach (explode(';', file_get_contents(\$mysqlDumpFile)) as \$sql) {
  293. if (\$sql = trim(\$sql)) {
  294. try {
  295. Db::connection(static::\$connection)->statement(\$sql);
  296. } catch (Throwable \$e) {}
  297. }
  298. }
  299. }
  300. }
  301. EOF;
  302. file_put_contents("$base/Install.php", $content);
  303. }
  304. /**
  305. * @return void
  306. */
  307. protected function createInstallSqlFile($file)
  308. {
  309. file_put_contents($file, '');
  310. }
  311. /**
  312. * @param $base
  313. * @param $name
  314. * @return void
  315. */
  316. protected function createConfigFiles($base, $name)
  317. {
  318. // app.php
  319. $content = <<<EOF
  320. <?php
  321. use support\\Request;
  322. return [
  323. 'debug' => true,
  324. 'controller_suffix' => 'Controller',
  325. 'controller_reuse' => false,
  326. 'version' => '1.0.0'
  327. ];
  328. EOF;
  329. file_put_contents("$base/app.php", $content);
  330. // menu.php
  331. $content = <<<EOF
  332. <?php
  333. return [];
  334. EOF;
  335. file_put_contents("$base/menu.php", $content);
  336. // autoload.php
  337. $content = <<<EOF
  338. <?php
  339. return [
  340. 'files' => [
  341. base_path() . '/plugin/$name/app/functions.php',
  342. ]
  343. ];
  344. EOF;
  345. file_put_contents("$base/autoload.php", $content);
  346. // container.php
  347. $content = <<<EOF
  348. <?php
  349. return new Webman\\Container;
  350. EOF;
  351. file_put_contents("$base/container.php", $content);
  352. // database.php
  353. $content = <<<EOF
  354. <?php
  355. return [];
  356. EOF;
  357. file_put_contents("$base/database.php", $content);
  358. // exception.php
  359. $content = <<<EOF
  360. <?php
  361. return [
  362. '' => support\\exception\\Handler::class,
  363. ];
  364. EOF;
  365. file_put_contents("$base/exception.php", $content);
  366. // log.php
  367. $content = <<<EOF
  368. <?php
  369. return [
  370. 'default' => [
  371. 'handlers' => [
  372. [
  373. 'class' => Monolog\\Handler\\RotatingFileHandler::class,
  374. 'constructor' => [
  375. runtime_path() . '/logs/$name.log',
  376. 7,
  377. Monolog\\Logger::DEBUG,
  378. ],
  379. 'formatter' => [
  380. 'class' => Monolog\\Formatter\\LineFormatter::class,
  381. 'constructor' => [null, 'Y-m-d H:i:s', true],
  382. ],
  383. ]
  384. ],
  385. ],
  386. ];
  387. EOF;
  388. file_put_contents("$base/log.php", $content);
  389. // middleware.php
  390. $content = <<<EOF
  391. <?php
  392. return [
  393. '' => [
  394. ]
  395. ];
  396. EOF;
  397. file_put_contents("$base/middleware.php", $content);
  398. // process.php
  399. $content = <<<EOF
  400. <?php
  401. return [];
  402. EOF;
  403. file_put_contents("$base/process.php", $content);
  404. // redis.php
  405. $content = <<<EOF
  406. <?php
  407. return [
  408. 'default' => [
  409. 'host' => '127.0.0.1',
  410. 'password' => null,
  411. 'port' => 6379,
  412. 'database' => 0,
  413. ],
  414. ];
  415. EOF;
  416. file_put_contents("$base/redis.php", $content);
  417. // route.php
  418. $content = <<<EOF
  419. <?php
  420. use Webman\\Route;
  421. EOF;
  422. file_put_contents("$base/route.php", $content);
  423. // static.php
  424. $content = <<<EOF
  425. <?php
  426. return [
  427. 'enable' => true,
  428. 'middleware' => [], // Static file Middleware
  429. ];
  430. EOF;
  431. file_put_contents("$base/static.php", $content);
  432. // translation.php
  433. $content = <<<EOF
  434. <?php
  435. return [
  436. // Default language
  437. 'locale' => 'zh_CN',
  438. // Fallback language
  439. 'fallback_locale' => ['zh_CN', 'en'],
  440. // Folder where language files are stored
  441. 'path' => base_path() . "/plugin/$name/resource/translations",
  442. ];
  443. EOF;
  444. file_put_contents("$base/translation.php", $content);
  445. // view.php
  446. $content = <<<EOF
  447. <?php
  448. use support\\view\\Raw;
  449. use support\\view\\Twig;
  450. use support\\view\\Blade;
  451. use support\\view\\ThinkPHP;
  452. return [
  453. 'handler' => Raw::class
  454. ];
  455. EOF;
  456. file_put_contents("$base/view.php", $content);
  457. // thinkorm.php
  458. $content = <<<EOF
  459. <?php
  460. return [];
  461. EOF;
  462. file_put_contents("$base/thinkorm.php", $content);
  463. }
  464. }