perform install test

This commit is contained in:
xiaomlove
2021-01-27 17:50:24 +08:00
parent 5c90a962e7
commit 59b7e573a2
4 changed files with 111 additions and 112 deletions
+5 -1
View File
@@ -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);
}
}
+3 -5
View File
@@ -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;
}
+88
View File
@@ -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;
}
}
+15 -106
View File
@@ -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;
}
}