From 59b7e573a2f4b21a70f8a714db93154a75d475e3 Mon Sep 17 00:00:00 2001 From: xiaomlove <353856593@qq.com> Date: Wed, 27 Jan 2021 17:50:24 +0800 Subject: [PATCH] perform install test --- include/globalfunctions.php | 6 +- nexus/Database/DB.php | 8 +-- nexus/Install/Install.php | 88 ++++++++++++++++++++++++++ public/install/install.php | 121 +++++------------------------------- 4 files changed, 111 insertions(+), 112 deletions(-) diff --git a/include/globalfunctions.php b/include/globalfunctions.php index d1c50a02..53448490 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -148,6 +148,10 @@ function dd($vars) function do_log($log, $level = 'info') { global $TWEAK; + $logging = sys_get_temp_dir() . '/nexus_' . date('Y-m-d') . '.log'; + if (!empty($TWEAK['logging'])) { + $logging = $TWEAK['logging']; + } if (!empty($TWEAK['logging'])) { $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $content = sprintf( @@ -163,7 +167,7 @@ function do_log($log, $level = 'info') $log, PHP_EOL ); - file_put_contents($TWEAK['logging'], $content, FILE_APPEND); + file_put_contents($logging, $content, FILE_APPEND); } } diff --git a/nexus/Database/DB.php b/nexus/Database/DB.php index b606b84d..8e0dcdd3 100644 --- a/nexus/Database/DB.php +++ b/nexus/Database/DB.php @@ -40,13 +40,11 @@ class DB return self::$instance = $instance; } - public function connect($host, $username, $password, $database, $port) { - if (!$this->isConnected()) { - $this->driver->connect($host, $username, $password, $database, $port); - $this->isConnected = true; - } + $this->driver->connect($host, $username, $password, $database, $port); + $this->isConnected = true; + do_log("do mysql_connect with: " . json_encode(func_get_args())); return true; } diff --git a/nexus/Install/Install.php b/nexus/Install/Install.php index 4edf059e..9af86428 100644 --- a/nexus/Install/Install.php +++ b/nexus/Install/Install.php @@ -2,6 +2,7 @@ namespace Nexus\Install; +use http\Exception\InvalidArgumentException; use Nexus\Database\DB; class Install @@ -354,6 +355,93 @@ class Install return DB::insert('users', $insert); } + public function createEnvFile($data) + { + $envExampleFile = ROOT_PATH . ".env.example"; + $envExampleData = readEnvFile($envExampleFile); + $envFile = ROOT_PATH . ".env"; + $newData = []; + if (file_exists($envFile) && is_readable($envFile)) { + //already exists, read it ,and merge post data + $newData = readEnvFile($envFile); + } + foreach ($envExampleData as $key => $value) { + if (isset($data[$key])) { + $value = trim($data[$key]); + } + $newData[$key] = $value; + } + unset($key, $value); + mysql_connect($newData['MYSQL_HOST'], $newData['MYSQL_USERNAME'], $newData['MYSQL_PASSWORD'], $newData['MYSQL_DATABASE'], $newData['MYSQL_PORT']); + if (extension_loaded('redis') && !empty($newData['REDIS_HOST'])) { + $redis = new \Redis(); + $redis->connect($newData['REDIS_HOST'], $newData['REDIS_PORT'] ?: 6379); + if (isset($newData['REDIS_DATABASE'])) { + if (!ctype_digit($newData['REDIS_DATABASE']) || $newData['REDIS_DATABASE'] < 0 || $newData['REDIS_DATABASE'] > 15) { + throw new \InvalidArgumentException("invalid redis database: " . $newData['redis_database']); + } + $redis->select($newData['REDIS_DATABASE']); + } + } + $content = ""; + foreach ($newData as $key => $value) { + $content .= "{$key}={$value}\n"; + } + $fp = @fopen($envFile, 'w'); + if ($fp === false) { + throw new \RuntimeException("can't open env file, make sure php has permission to create file at: " . ROOT_PATH); + } + fwrite($fp, $content); + fclose($fp); + $this->doLog("create env file: $envFile with content: \n $content"); + return true; + } + public function listShouldCreateTable() + { + $existsTable = $this->listExistsTable(); + $tableCreate = $this->listAllTableCreate(); + $shouldCreateTable = []; + foreach ($tableCreate as $table => $sql) { + if (in_array($table, $existsTable)) { + continue; + } + $shouldCreateTable[$table] = $sql; + } + return $shouldCreateTable; + } + public function createTable(array $createTable) + { + foreach ($createTable as $table => $sql) { + $this->doLog("create table: $table \n $sql"); + sql_query($sql); + } + return true; + } + + public function saveSettings($settings) + { + foreach ($settings as $prefix => &$group) { + $this->doLog("[SAVE SETTING], prefix: $prefix, nameAndValues: " . json_encode($group)); + saveSetting($prefix, $group); + } + } + + public function createSymbolicLinks($symbolicLinks) + { + foreach ($symbolicLinks as $path) { + $linkName = ROOT_PATH . 'public/' . basename($path); + if (is_dir($linkName)) { + $this->doLog("path: $linkName already exits, skip create symbolic link $linkName -> $path"); + continue; + } + $linkResult = symlink($path, $linkName); + if ($linkResult === false) { + throw new \RuntimeException("can't not make symbolic link: $linkName -> $path"); + } + $this->doLog("success make symbolic link: $linkName -> $path"); + } + return true; + } } \ No newline at end of file diff --git a/public/install/install.php b/public/install/install.php index 201a0374..5bfbb137 100644 --- a/public/install/install.php +++ b/public/install/install.php @@ -29,63 +29,15 @@ if ($currentStep == 2) { $envFormControls = $install->listEnvFormControls(); $newData = array_column($envFormControls, 'value', 'name'); while ($isPost) { -// $envFile = "$rootpath.env." . time(); -// $newData = $formData = []; -// if (file_exists($envFile) && is_readable($envFile)) { -// //already exists, read it ,and merge post data -// $newData = readEnvFile($envFile); -// } -// foreach ($envExampleData as $key => $value) { -// $postValue = trim($_POST[$key] ?? ''); -// if ($postValue) { -// $value = $postValue; -// } -// $newData[$key] = $value; -// $envExampleData[] = [ -// 'label' => $key, -// 'name' => $key, -// 'value' => $value, -// ]; -// } -// unset($key); - //check try { - $connectMysql = mysql_connect($newData['MYSQL_HOST'], $newData['MYSQL_USERNAME'], $newData['MYSQL_PASSWORD'], $newData['MYSQL_DATABASE'], $newData['MYSQL_PORT']); - } catch (\Exception $e) { - $_SESSION['error'] = "Mysql: " . $e->getMessage(); + $install->createEnvFile($_POST); + $install->nextStep(); + } catch (\Exception $exception) { + $_SESSION['error'] = $exception->getMessage(); break; } - if (extension_loaded('redis') && !empty($newData['REDIS_HOST'])) { - try { - $redis = new Redis(); - $redis->connect($newData['REDIS_HOST'], $newData['REDIS_PORT'] ?: 6379); - } catch (\Exception $e) { - $_SESSION['error'] = "Redis: " . $e->getMessage(); - break; - } - if (!ctype_digit($newData['REDIS_DATABASE']) || $newData['REDIS_DATABASE'] < 0 || $newData['REDIS_DATABASE'] > 15) { - $_SESSION['error'] = "invalid REDIS_DATABASE"; - break; - } - } - -// $content = ""; -// foreach ($newData as $key => $value) { -// $content .= "{$key}={$value}\n"; -// } -// $fp = @fopen($envFile, 'w'); -// if ($fp === false) { -// $msg = "文件无法打开, 确保 PHP 有权限在根目录创建文件"; -// $msg .= "\n也可以复制以下内容保存到 $envFile 中"; -// $_SESSION['error'] = $msg; -// $_SESSION['copy'] = $content; -// } -// fwrite($fp, $content); -// fclose($fp); - $install->nextStep(); break; } - $tableRows = [ [ 'label' => '.env.example', @@ -100,41 +52,15 @@ if ($currentStep == 2) { if ($currentStep == 3) { $pass = true; - $existsTable = $install->listExistsTable(); - $tableCreate = $install->listAllTableCreate(); - $shouldCreateTable = []; - foreach ($tableCreate as $table => $sql) { - if (in_array($table, $existsTable)) { - continue; - } - $shouldCreateTable[$table] = $sql; - } - - while (true) { -// $sqlFile = $rootpath . '_db/dbstructure_v1.6.sql'; -// try { -// $tableCreate = listAllTableCreate($sqlFile); -// } catch (\Exception $e) { -// $_SESSION['error'] = $e->getMessage(); -// break; -// } -// if (empty($tableCreate)) { -// $_SESSION['error'] = "no table create, make sure sql file is correct"; -// break; -// } - - if ($isPost) { - try { - foreach ($shouldCreateTable as $table => $sql) { - sql_query($sql); - } - } catch (\Exception $e) { - $_SESSION['error'] = $e->getMessage(); - break; - } + $shouldCreateTable = $install->listShouldCreateTable(); + while ($isPost) { + try { + $install->createTable($shouldCreateTable); $install->nextStep(); + } catch (\Exception $exception) { + $_SESSION['error'] = $exception->getMessage(); + break; } - break; } } @@ -178,32 +104,15 @@ if ($currentStep == 4) { $symbolicLinks = $settingTableRows['symbolic_links']; $tableRows = $settingTableRows['table_rows']; $pass = $settingTableRows['pass']; - while (true) { + while ($isPost) { try { - foreach ($settings as $prefix => &$group) { - if ($isPost) { - $install->doLog("[SAVE] prefix: $prefix, nameAndValues: " . json_encode($group)); - foreach ($symbolicLinks as $path) { - $linkName = ROOT_PATH . 'public/' . basename($path); - if (is_dir($linkName)) { - $install->doLog("path: $linkName already exits, skip create symbolic link $linkName -> $path"); - continue; - } - //@todo -// $linkResult = symlink($path, $linkName); -// if ($linkResult === false) { -// throw new \Exception("can't not make symbolic link: $linkName -> $path"); -// } - } - //@todo -// saveSetting($prefix, $group); - } - } + $install->saveSettings($settings); + $install->createSymbolicLinks($symbolicLinks); + $install->nextStep(); } catch (\Exception $e) { $_SESSION['error'] = $e->getMessage(); break; } - $isPost && $install->nextStep(); break; } }