fix: Strict env enforcement, rock-solid Alpine portal rendering, and Nabeh/Redis test diagnostics

This commit is contained in:
Hamza-Ayed
2026-08-26 22:25:35 +03:00
parent 388944cbff
commit 1f024d0c30
10 changed files with 990 additions and 702 deletions
+20 -13
View File
@@ -7,16 +7,17 @@ use PDOException;
/**
* PDO Database wrapper using Singleton pattern.
* Strict environment variable enforcement (No default fallbacks).
*/
class Database
{
private static ?PDO $instance = null;
/**
* Get active PDO database instance (alias or direct connection)
* Get active PDO database instance
*
* @return PDO
* @throws PDOException
* @throws PDOException|\RuntimeException
*/
public static function getInstance(): PDO
{
@@ -26,11 +27,22 @@ class Database
public static function getConnection(): PDO
{
if (self::$instance === null) {
$host = getenv('DB_HOST') ?: '127.0.0.1';
$port = getenv('DB_PORT') ?: '3306';
$dbName = getenv('DB_DATABASE') ?: 'saqelDB';
$username = getenv('DB_USERNAME') ?: 'saqelUser';
$password = getenv('DB_PASSWORD') ?: '';
$host = getenv('DB_HOST');
$port = getenv('DB_PORT');
$dbName = getenv('DB_DATABASE');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
$missing = [];
if ($host === false || $host === '') $missing[] = 'DB_HOST';
if ($port === false || $port === '') $missing[] = 'DB_PORT';
if ($dbName === false || $dbName === '') $missing[] = 'DB_DATABASE';
if ($username === false || $username === '') $missing[] = 'DB_USERNAME';
if ($password === false) $missing[] = 'DB_PASSWORD';
if (!empty($missing)) {
throw new \RuntimeException("Database Configuration Error: Missing environment variable(s): " . implode(', ', $missing));
}
$dsn = "mysql:host={$host};port={$port};dbname={$dbName};charset=utf8mb4";
@@ -43,9 +55,8 @@ class Database
try {
self::$instance = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
// Log the exact error internally but hide sensitive DSN on production
error_log("Database Connection Error: " . $e->getMessage());
throw new PDOException("Could not connect to the database. Check database settings.");
throw new PDOException("Could not connect to MySQL database at {$host}:{$port}/{$dbName}. Error: " . $e->getMessage());
}
}
@@ -54,10 +65,6 @@ class Database
/**
* Shorthand execute statement with parameters
*
* @param string $sql
* @param array $params
* @return \PDOStatement
*/
public static function query(string $sql, array $params = []): \PDOStatement
{