Install.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace app\admin\command;
  3. use PDO;
  4. use think\Config;
  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 Install extends Command
  12. {
  13. protected $model = null;
  14. protected function configure()
  15. {
  16. $this
  17. ->setName('install')
  18. ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', FALSE)
  19. ->setDescription('New installation of FastAdmin');
  20. }
  21. protected function execute(Input $input, Output $output)
  22. {
  23. //覆盖安装
  24. $force = $input->getOption('force');
  25. $installLockFile = __DIR__ . "/Install/install.lock";
  26. if (is_file($installLockFile) && !$force)
  27. {
  28. throw new Exception("\nFastAdmin already installed!\nIf you need to reinstall again, use the parameter --force=true ");
  29. }
  30. $sql = file_get_contents(__DIR__ . '/Install/fastadmin.sql');
  31. // 先尝试能否自动创建数据库
  32. $config = Config::get('database');
  33. $pdo = new PDO("{$config['type']}:host={$config['hostname']}" . ($config['hostport'] ? ";port={$config['hostport']}" : ''), $config['username'], $config['password']);
  34. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35. $pdo->query("CREATE DATABASE IF NOT EXISTS `{$config['database']}` CHARACTER SET utf8 COLLATE utf8_general_ci;");
  36. // 查询一次SQL,判断连接是否正常
  37. Db::execute("SELECT 1");
  38. // 调用原生PDO对象进行批量查询
  39. Db::getPdo()->exec($sql);
  40. file_put_contents($installLockFile, 1);
  41. $output->info("Install Successed!");
  42. }
  43. }