feat: GTSIT 사이트 구축
This commit is contained in:
41
app/Database.php
Normal file
41
app/Database.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
final class Database
|
||||
{
|
||||
private static ?PDO $connection = null;
|
||||
|
||||
public static function connection(): PDO
|
||||
{
|
||||
if (self::$connection instanceof PDO) {
|
||||
return self::$connection;
|
||||
}
|
||||
|
||||
$config = config('database');
|
||||
if (empty($config['name']) || empty($config['user'])) {
|
||||
throw new PDOException('Database configuration is unavailable.');
|
||||
}
|
||||
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['name'],
|
||||
$config['charset']
|
||||
);
|
||||
|
||||
self::$connection = new PDO($dsn, $config['user'], $config['password'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
|
||||
return self::$connection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user