aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/Database.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/Database.php')
-rw-r--r--src/app/Database.php44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/app/Database.php b/src/app/Database.php
new file mode 100644
index 0000000..f4dff79
--- /dev/null
+++ b/src/app/Database.php
@@ -0,0 +1,44 @@
+<?php
+
+declare(strict_types=1);
+// PDO singleton for MariaDB connections.
+
+namespace App;
+
+class Database
+{
+ private static ?Database $instance = null;
+ private \PDO $pdo;
+
+ private function __construct()
+ {
+ $host = getenv('MYSQL_HOST') ?: 'mariadb';
+ $db = getenv('MYSQL_DATABASE');
+ $user = getenv('MYSQL_USER');
+ $pass = getenv('MYSQL_PASSWORD');
+
+ $this->pdo = new \PDO(
+ "mysql:host=$host;dbname=$db;charset=utf8mb4",
+ $user,
+ $pass,
+ [
+ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
+ \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
+ \PDO::ATTR_EMULATE_PREPARES => false,
+ ]
+ );
+ }
+
+ public static function getInstance(): self
+ {
+ if (self::$instance === null) {
+ self::$instance = new self();
+ }
+ return self::$instance;
+ }
+
+ public function getPdo(): \PDO
+ {
+ return $this->pdo;
+ }
+}