InstallDemo.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2020/2/4
  6. * Time: 12:47
  7. */
  8. namespace app\common\command;
  9. use app\admin\model\Admin;
  10. use app\common\model\Templet;
  11. use file\DirHelper;
  12. use think\console\input\Argument;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Option;
  16. use think\console\Output;
  17. use think\Db;
  18. use think\Env;
  19. use file\ZipHelper;
  20. class InstallDemo extends Command
  21. {
  22. /**
  23. * 安装测试数据
  24. * php think InstallDemo
  25. */
  26. protected function configure()
  27. {
  28. $this->setName('InstallDemo')
  29. ->addOption('delete', 'd', Option::VALUE_NONE, 'delete data')//清空数据,仅测试时使用,线上使用后记得跑路
  30. ->addArgument('admin_pass', Argument::OPTIONAL, "admin password", "123456")//需要验证管理员密码
  31. ->addArgument('demo_name', Argument::OPTIONAL, "demo source name", "blog_demo")//安装包名
  32. ->setDescription('install test data & demo');
  33. }
  34. protected function execute(Input $input, Output $output)
  35. {
  36. //验证管理员密码
  37. $admin = Admin::get(1);
  38. if (!$admin || $admin->password != password($input->getArgument("admin_pass"))) {
  39. $output->writeln("<error>admin pass error</error>");
  40. return;
  41. }
  42. if ($input->getOption("delete")) {
  43. //清空数据
  44. $prefix = \think\Env::get("db_prefix", "");
  45. Db::execute("TRUNCATE {$prefix}admin_log");
  46. Db::execute("TRUNCATE {$prefix}article");
  47. Db::execute("TRUNCATE {$prefix}article_cate");
  48. Db::execute("TRUNCATE {$prefix}attachment");
  49. Db::execute("TRUNCATE {$prefix}catalog");
  50. Db::execute("TRUNCATE {$prefix}cate_catalog");
  51. Db::execute("TRUNCATE {$prefix}config");
  52. Db::execute("TRUNCATE {$prefix}config_option");
  53. Db::execute("TRUNCATE {$prefix}config_tab");
  54. Db::execute("TRUNCATE {$prefix}messages");
  55. Db::execute("TRUNCATE {$prefix}point_log");
  56. Db::execute("TRUNCATE {$prefix}user");
  57. $output->writeln("delete success");
  58. } else {
  59. $demo_name = $input->getArgument("demo_name");
  60. $source = APP_PATH . "install/data/$demo_name.zip";
  61. if (!file_exists($source)) {
  62. $output->error("demo source not exists");
  63. exit;
  64. }
  65. //解压
  66. $dest = APP_PATH . 'install/data/';
  67. if (false === (new ZipHelper())->unzip($source, $dest)) {
  68. $output->error("unzip fail");
  69. exit;
  70. }
  71. //复制static
  72. $source = APP_PATH . "install/data/$demo_name/static";
  73. $dest = Templet::DEFAULT_STATIC_PATH . 'static';
  74. DirHelper::copyDir($source, $dest);
  75. //复制template
  76. $source = APP_PATH . "install/data/$demo_name/template";
  77. $dest = Templet::getTempletDir();
  78. DirHelper::copyDir($source, $dest);
  79. //connect db
  80. $db_prefix = Env::get('db_prefix', 'tplay_');
  81. $db_name = Env::get('db_name', 'tplay');
  82. $search = [
  83. "tplay_",
  84. "USE `tplay`",
  85. "CREATE DATABASE IF NOT EXISTS `tplay`",
  86. ];
  87. $replace = [
  88. $db_prefix,
  89. "USE `" . $db_name . "`",
  90. "CREATE DATABASE IF NOT EXISTS `" . $db_name . "`",
  91. ];
  92. //导入sql
  93. $demo_sql = file_get_contents(APP_PATH . "install/data/$demo_name/db.sql");
  94. $sql_array = preg_split("/;[\r\n]+/", str_replace($search, $replace, $demo_sql));
  95. foreach ($sql_array as $k => $v) {
  96. if (!empty($v)) {
  97. Db::query($v);
  98. }
  99. }
  100. $output->writeln("install success");
  101. }
  102. }
  103. }