作者 幻灵姬风
提交者 Karson

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

Merge pull request !66 from 小埋酱/master
@@ -20,15 +20,15 @@ class Install extends Command @@ -20,15 +20,15 @@ class Install extends Command
20 { 20 {
21 $config = Config::get('database'); 21 $config = Config::get('database');
22 $this 22 $this
23 - ->setName('install')  
24 - ->addOption('hostname', 'a', Option::VALUE_OPTIONAL, 'mysql hostname', $config['hostname'])  
25 - ->addOption('hostport', 'o', Option::VALUE_OPTIONAL, 'mysql hostport', $config['hostport'])  
26 - ->addOption('database', 'd', Option::VALUE_OPTIONAL, 'mysql database', $config['database'])  
27 - ->addOption('prefix', 'r', Option::VALUE_OPTIONAL, 'table prefix', $config['prefix'])  
28 - ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'mysql username', $config['username'])  
29 - ->addOption('password', 'p', Option::VALUE_OPTIONAL, 'mysql password', $config['password'])  
30 - ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', FALSE)  
31 - ->setDescription('New installation of FastAdmin'); 23 + ->setName('install')
  24 + ->addOption('hostname', 'a', Option::VALUE_OPTIONAL, 'mysql hostname', $config['hostname'])
  25 + ->addOption('hostport', 'o', Option::VALUE_OPTIONAL, 'mysql hostport', $config['hostport'])
  26 + ->addOption('database', 'd', Option::VALUE_OPTIONAL, 'mysql database', $config['database'])
  27 + ->addOption('prefix', 'r', Option::VALUE_OPTIONAL, 'table prefix', $config['prefix'])
  28 + ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'mysql username', $config['username'])
  29 + ->addOption('password', 'p', Option::VALUE_OPTIONAL, 'mysql password', $config['password'])
  30 + ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', FALSE)
  31 + ->setDescription('New installation of FastAdmin');
32 } 32 }
33 33
34 protected function execute(Input $input, Output $output) 34 protected function execute(Input $input, Output $output)
@@ -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' => "{$config['type']}",
  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}'),";
@@ -80,7 +88,7 @@ class Install extends Command @@ -80,7 +88,7 @@ class Install extends Command
80 $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)Env::get\((.*)\)\,/", $callback, $config); 88 $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)Env::get\((.*)\)\,/", $callback, $config);
81 // 写入数据库配置 89 // 写入数据库配置
82 file_put_contents($dbConfigFile, $config); 90 file_put_contents($dbConfigFile, $config);
83 - 91 +
84 \think\Cache::rm('__menu__'); 92 \think\Cache::rm('__menu__');
85 93
86 $output->info("Install Successed!"); 94 $output->info("Install Successed!");