Add database port feature on installation step & configuration

This commit is contained in:
Muhammad Hari Suharto committed 2020-05-16 20:59:15 +07:00
1 parent c3809e3529
commit 82d005dd9c
7 files changed
+35 -9

No files matched your search

+3 -1
View File
@@ -17,9 +17,11 @@ $host = $config['db']['host'];
$dbname = $config['db']['name'];
$user = $config['db']['user'];
$password = $config['db']['password'];
$port = $config['db']['port'];
echo sprintf("Connecting to database %s@%s/%s", $user, $host, $dbname) . PHP_EOL;
$dbh = new PDO($type . ':host=' . $host, $user, $password, array(
$dbh = new PDO($type . ':host=' . $host . ';port=' . $port, $user, $password, array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+5
View File
@@ -94,6 +94,11 @@ return array(
* Database password
*/
'password' =>getenv('BB_DB_PASSWORD'),
/**
* Database port
*/
'port' =>getenv('BB_DB_PORT'),
),
'twig' => array(
+5
View File
@@ -94,6 +94,11 @@ return array(
* Database password
*/
'password' =>'foo',
/**
* Database Port
*/
'port' =>'3306',
),
'twig' => array(
+1 -1
View File
@@ -47,7 +47,7 @@ $di['crypt'] = function() use ($di) {
$di['pdo'] = function() use ($di) {
$c = $di['config']['db'];
$pdo = new PDO($c['type'].':host='.$c['host'].';dbname='.$c['name'],
$pdo = new PDO($c['type'].':host='.$c['host'].';port='.$c['port'].';dbname='.$c['name'],
$c['user'],
$c['password'],
array(
+13 -6
View File
@@ -64,13 +64,15 @@ final class Box_Installer
$host = $_POST['db_host'];
$pass = $_POST['db_pass'];
$name = $_POST['db_name'];
if (!$this->canConnectToDatabase($host, $name, $user, $pass)) {
$port = $_POST['db_port'];
if (!$this->canConnectToDatabase($host, $name, $user, $pass, $port)) {
print 'Could not connect to database. Please check database details.';
} else {
$this->session->set('db_host', $host);
$this->session->set('db_name', $name);
$this->session->set('db_user', $user);
$this->session->set('db_pass', $pass);
$this->session->set('db_port', $port);
print 'ok';
}
@@ -83,13 +85,15 @@ final class Box_Installer
$host = $_POST['db_host'];
$pass = $_POST['db_pass'];
$name = $_POST['db_name'];
if (!$this->canConnectToDatabase($host, $name, $user, $pass)) {
$port = $_POST['db_port'];
if (!$this->canConnectToDatabase($host, $name, $user, $pass, $port)) {
throw new Exception('Could not connect to database or database does not exist');
} else {
$this->session->set('db_host', $host);
$this->session->set('db_name', $name);
$this->session->set('db_user', $user);
$this->session->set('db_pass', $pass);
$this->session->set('db_port', $port);
}
// admin config
@@ -146,6 +150,7 @@ final class Box_Installer
'db_name' => $this->session->get('db_name'),
'db_user' => $this->session->get('db_user'),
'db_pass' => $this->session->get('db_pass'),
'db_port' => $this->session->get('db_port'),
'admin_email' => $this->session->get('admin_email'),
'admin_pass' => $this->session->get('admin_pass'),
@@ -195,9 +200,9 @@ final class Box_Installer
return file_get_contents($path);
}
private function canConnectToDatabase($host, $db, $user, $pass)
private function canConnectToDatabase($host, $db, $user, $pass, $port)
{
$con = new mysqli($host, $user, $pass, $db);
$con = new mysqli($host, $user, $pass, $db, $port);
if ($con->connect_error) {
return false;
}
@@ -242,7 +247,7 @@ final class Box_Installer
$this->_isValidInstallData($ns);
$this->_createConfigurationFile($ns);
$link = new mysqli($ns->get('db_host'), $ns->get('db_user'), $ns->get('db_pass'), $ns->get('db_name'));
$link = new mysqli($ns->get('db_host'), $ns->get('db_user'), $ns->get('db_pass'), $ns->get('db_name'), $ns->get('db_port'));
if ($link->connect_error) {
throw new Exception("Connect Error (" . $link->connect_errno . ") " . $link->connect_error);
@@ -353,6 +358,7 @@ final class Box_Installer
'name' => $ns->get('db_name'),
'user' => $ns->get('db_user'),
'password' => $ns->get('db_pass'),
'port' => $ns->get('db_port'),
],
'twig' => [
@@ -394,6 +400,7 @@ final class Box_Installer
$output .= sprintf($f, 'BB_DB_USER', $ns->get('db_user'));
$output .= sprintf($f, 'BB_DB_PASSWORD', $ns->get('db_pass'));
$output .= sprintf($f, 'BB_DB_HOST', $ns->get('db_host'));
$output .= sprintf($f, 'BB_DB_PORT', $ns->get('db_port'));
$output .= sprintf($f, 'BB_DB_TYPE', 'mysql');
$output .= sprintf($cf, 'Live site URL with trailing slash');
@@ -425,7 +432,7 @@ final class Box_Installer
private function _isValidInstallData($ns)
{
if (!$this->canConnectToDatabase($ns->get('db_host'), $ns->get('db_name'), $ns->get('db_user'), $ns->get('db_pass'))) {
if (!$this->canConnectToDatabase($ns->get('db_host'), $ns->get('db_name'), $ns->get('db_user'), $ns->get('db_pass'), $ns->get('db_port'))) {
throw new Exception('Can not connect to database');
}
+7
View File
@@ -156,6 +156,13 @@
</div>
<div class="fix"></div>
</div>
<div class="rowElem nobg">
<label>Database Port:</label>
<div class="formRight">
<input type="text" id="db_port" name="db_port" value="{{ db_port|default('3306') }}" required="required" placeholder="3306"/>
</div>
<div class="fix"></div>
</div>
<div class="rowElem">
<label>Database name:</label>
<div class="formRight">
+1 -1
View File
@@ -13,7 +13,7 @@ abstract class BBDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
{
if ($this->conn === null) {
if (self::$pdo == null) {
self::$pdo = new PDO( 'mysql:dbname='.BB_DB_NAME.';host=127.0.0.1', BB_DB_USER, BB_DB_PASSWORD );
self::$pdo = new PDO( 'mysql:dbname='.BB_DB_NAME.';host=127.0.0.1;port='.BB_DB_PORT, BB_DB_USER, BB_DB_PASSWORD );
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, BB_DB_NAME);
}