You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

<?php
class DB {
private static $pdo = null;
private static $config = null;
private static function loadConfig() {
if (self::$config === null) {
$cfg = require __DIR__ . '/../config/config.php';
self::$config = [
'dsn' => sprintf(
'mysql:host=%s;dbname=%s;charset=utf8mb4',
$cfg['db']['host'],
$cfg['db']['name']
),
'user' => $cfg['db']['user'],
'pass' => $cfg['db']['pass'],
];
}
}
public static function get($forceNew = false) {
self::loadConfig();
if (self::$pdo === null || $forceNew) {
self::$pdo = new PDO(
self::$config['dsn'],
self::$config['user'],
self::$config['pass'],
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_TIMEOUT => 3,
PDO::ATTR_PERSISTENT => false,
]
);
}
return self::$pdo;
}
}