Install.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $config = Config::get('database');
  17. $this
  18. ->setName('install')
  19. ->addOption('hostname', 'a', Option::VALUE_OPTIONAL, 'mysql hostname', $config['hostname'])
  20. ->addOption('hostport', 'o', Option::VALUE_OPTIONAL, 'mysql hostport', $config['hostport'])
  21. ->addOption('database', 'd', Option::VALUE_OPTIONAL, 'mysql database', $config['database'])
  22. ->addOption('prefix', 'r', Option::VALUE_OPTIONAL, 'table prefix', $config['prefix'])
  23. ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'mysql username', $config['username'])
  24. ->addOption('password', 'p', Option::VALUE_OPTIONAL, 'mysql password', $config['password'])
  25. ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', FALSE)
  26. ->setDescription('New installation of FastAdmin');
  27. }
  28. protected function execute(Input $input, Output $output)
  29. {
  30. // 覆盖安装
  31. $force = $input->getOption('force');
  32. $hostname = $input->getOption('hostname');
  33. $hostport = $input->getOption('hostport');
  34. $database = $input->getOption('database');
  35. $prefix = $input->getOption('prefix');
  36. $username = $input->getOption('username');
  37. $password = $input->getOption('password');
  38. $installLockFile = __DIR__ . "/Install/install.lock";
  39. if (is_file($installLockFile) && !$force)
  40. {
  41. throw new Exception("\nFastAdmin already installed!\nIf you need to reinstall again, use the parameter --force=true ");
  42. }
  43. $sql = file_get_contents(__DIR__ . '/Install/fastadmin.sql');
  44. $sql = str_replace("`fa_", "`{$prefix}", $sql);
  45. // 先尝试能否自动创建数据库
  46. $config = Config::get('database');
  47. $pdo = new PDO("{$config['type']}:host={$hostname}" . ($hostport ? ";port={$hostport}" : ''), $username, $password);
  48. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  49. $pdo->query("CREATE DATABASE IF NOT EXISTS `{$database}` CHARACTER SET utf8 COLLATE utf8_general_ci;");
  50. // 查询一次SQL,判断连接是否正常
  51. Db::execute("SELECT 1");
  52. // 调用原生PDO对象进行批量查询
  53. Db::getPdo()->exec($sql);
  54. file_put_contents($installLockFile, 1);
  55. $dbConfigFile = APP_PATH . 'database.php';
  56. $config = @file_get_contents($dbConfigFile);
  57. $callback = function($matches) use($hostname, $hostport, $username, $password, $database, $prefix) {
  58. $field = $matches[1];
  59. $replace = $$field;
  60. if ($matches[1] == 'hostport' && $hostport == 3306)
  61. {
  62. $replace = '';
  63. }
  64. return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}Env::get('database.{$matches[1]}', '{$replace}'),";
  65. };
  66. $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)Env::get\((.*)\)\,/", $callback, $config);
  67. // 写入数据库配置
  68. file_put_contents($dbConfigFile, $config);
  69. \think\Cache::rm('__menu__');
  70. $output->info("Install Successed!");
  71. }
  72. }