DSN based database connecting.
This commit is contained in:
parent
4c2add3861
commit
ada73c23bb
1 changed files with 10 additions and 26 deletions
32
makai.php
32
makai.php
|
@ -1,14 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Makai;
|
namespace Makai;
|
||||||
|
|
||||||
use ErrorException;
|
|
||||||
use Index\Autoloader;
|
|
||||||
use Index\Environment;
|
use Index\Environment;
|
||||||
use Index\Data\ConnectionFailedException;
|
use Index\Data\DbTools;
|
||||||
use Index\Data\MariaDB\MariaDBBackend;
|
|
||||||
use Index\Data\MariaDB\MariaDBConnectionInfo;
|
|
||||||
use Index\Data\NullDb\NullDbBackend;
|
|
||||||
use Index\Data\NullDb\NullDbConnectionInfo;
|
|
||||||
|
|
||||||
define('MKI_STARTUP', microtime(true));
|
define('MKI_STARTUP', microtime(true));
|
||||||
define('MKI_ROOT', __DIR__);
|
define('MKI_ROOT', __DIR__);
|
||||||
|
@ -25,27 +19,17 @@ require_once MKI_ROOT . '/vendor/autoload.php';
|
||||||
Environment::setDebug(MKI_DEBUG);
|
Environment::setDebug(MKI_DEBUG);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$dbConfig = parse_ini_file(MKI_DIR_CONFIG . DIRECTORY_SEPARATOR . 'database.ini');
|
$cfg = parse_ini_file(MKI_DIR_CONFIG . DIRECTORY_SEPARATOR . 'database.ini');
|
||||||
} catch(ErrorException $ex) {
|
} catch(\ErrorException $ex) {
|
||||||
$dbConfig = null;
|
$cfg = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!empty($dbConfig))
|
|
||||||
try {
|
try {
|
||||||
$db = (new MariaDBBackend)->createConnection(MariaDBConnectionInfo::create(
|
$db = DbTools::create($cfg['dsn'] ?? 'null:');
|
||||||
$dbConfig['host'] ?? '',
|
} catch(\Index\Data\ConnectionFailedException $ex) {
|
||||||
$dbConfig['user'] ?? '',
|
$db = DbTools::create('null:');
|
||||||
$dbConfig['pass'] ?? '',
|
|
||||||
$dbConfig['name'] ?? '',
|
|
||||||
$dbConfig['char'] ?? 'utf8mb4',
|
|
||||||
'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'',
|
|
||||||
));
|
|
||||||
} catch(ConnectionFailedException $ex) {
|
|
||||||
//echo '<h3>Unable to connect to database</h3>';
|
|
||||||
//die($ex->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(empty($db))
|
$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'');
|
||||||
$db = (new NullDbBackend)->createConnection(new NullDbConnectionInfo);
|
|
||||||
|
|
||||||
$ctx = new MakaiContext($db);
|
$ctx = new MakaiContext($db);
|
||||||
|
|
Loading…
Reference in a new issue