🚀 مُصادَق: الإطلاق الأولي للنظام المتكامل
This commit is contained in:
41
app/Core/Database.php
Normal file
41
app/Core/Database.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Exception;
|
||||
|
||||
final class Database
|
||||
{
|
||||
private static ?PDO $instance = null;
|
||||
|
||||
public static function getInstance(): PDO
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
$host = $_ENV['DB_HOST'];
|
||||
$db = $_ENV['DB_DATABASE'];
|
||||
$user = $_ENV['DB_USERNAME'];
|
||||
$pass = $_ENV['DB_PASSWORD'];
|
||||
$port = $_ENV['DB_PORT'];
|
||||
$charset = $_ENV['DB_CHARSET'] ?? 'utf8mb4';
|
||||
|
||||
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
|
||||
try {
|
||||
self::$instance = new PDO($dsn, $user, $pass, $options);
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("Database Connection Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user