diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-21 20:50:43 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-21 20:50:43 +0100 |
| commit | d1ef15fa39935bfa0420c5ac2b8c269e294c9a6d (patch) | |
| tree | 618158449863123f6b9527b9db6183f8c3ce5c91 /src/app/Database.php | |
| download | camagru-d1ef15fa39935bfa0420c5ac2b8c269e294c9a6d.tar.gz camagru-d1ef15fa39935bfa0420c5ac2b8c269e294c9a6d.zip | |
Initial project scaffold
Set up MVC architecture with front controller, router, autoloader,
database singleton, and Docker Compose stack (Nginx + PHP-FPM + MariaDB).
Includes DB schema, responsive layout, dev tooling (php-cs-fixer,
parallel-lint), and documentation.
Diffstat (limited to 'src/app/Database.php')
| -rw-r--r-- | src/app/Database.php | 44 |
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; + } +} |
