作者 kira

修复执行install命令主机不是127.0.0.1的时候安装失败问题

执行php think install --hostname 172.17.0.1 --password 1234 --force true的时候,假如用的主机名不是127.0.0.1的话,会安装失败,因为DB实例连接到本地了
@@ -43,8 +43,7 @@ class Install extends Command @@ -43,8 +43,7 @@ class Install extends Command
43 $password = $input->getOption('password'); 43 $password = $input->getOption('password');
44 44
45 $installLockFile = __DIR__ . "/Install/install.lock"; 45 $installLockFile = __DIR__ . "/Install/install.lock";
46 - if (is_file($installLockFile) && !$force)  
47 - { 46 + if (is_file($installLockFile) && !$force) {
48 throw new Exception("\nFastAdmin already installed!\nIf you need to reinstall again, use the parameter --force=true "); 47 throw new Exception("\nFastAdmin already installed!\nIf you need to reinstall again, use the parameter --force=true ");
49 } 48 }
50 49
@@ -58,21 +57,30 @@ class Install extends Command @@ -58,21 +57,30 @@ class Install extends Command
58 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 57 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
59 $pdo->query("CREATE DATABASE IF NOT EXISTS `{$database}` CHARACTER SET utf8 COLLATE utf8_general_ci;"); 58 $pdo->query("CREATE DATABASE IF NOT EXISTS `{$database}` CHARACTER SET utf8 COLLATE utf8_general_ci;");
60 59
  60 + // 连接install命令中指定的数据库
  61 + $instance = Db::connect([
  62 + 'type' => 'mysql',
  63 + 'hostname' => "{$hostname}",
  64 + 'hostport' => "{$hostport}",
  65 + 'database' => "{$database}",
  66 + 'username' => "{$username}",
  67 + 'password' => "{$password}",
  68 + ]);
  69 +
61 // 查询一次SQL,判断连接是否正常 70 // 查询一次SQL,判断连接是否正常
62 - Db::execute("SELECT 1"); 71 + $instance->execute("SELECT 1");
63 72
64 // 调用原生PDO对象进行批量查询 73 // 调用原生PDO对象进行批量查询
65 - Db::getPdo()->exec($sql); 74 + $instance->getPdo()->exec($sql);
66 75
67 file_put_contents($installLockFile, 1); 76 file_put_contents($installLockFile, 1);
68 77
69 $dbConfigFile = APP_PATH . 'database.php'; 78 $dbConfigFile = APP_PATH . 'database.php';
70 $config = @file_get_contents($dbConfigFile); 79 $config = @file_get_contents($dbConfigFile);
71 - $callback = function($matches) use($hostname, $hostport, $username, $password, $database, $prefix) { 80 + $callback = function ($matches) use ($hostname, $hostport, $username, $password, $database, $prefix) {
72 $field = $matches[1]; 81 $field = $matches[1];
73 $replace = $$field; 82 $replace = $$field;
74 - if ($matches[1] == 'hostport' && $hostport == 3306)  
75 - { 83 + if ($matches[1] == 'hostport' && $hostport == 3306) {
76 $replace = ''; 84 $replace = '';
77 } 85 }
78 return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}Env::get('database.{$matches[1]}', '{$replace}'),"; 86 return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}Env::get('database.{$matches[1]}', '{$replace}'),";