<?php
declare(strict_types=1);

function pdo(): PDO {
  static $pdo;
  if ($pdo instanceof PDO) return $pdo;

  $app = require __DIR__ . '/../config/app.php';
  $db = $app['db'];

  $dsn = sprintf(
    'mysql:host=%s;dbname=%s;charset=%s',
    $db['host'],
    $db['name'],
    $db['charset']
  );

  $pdo = new PDO($dsn, $db['user'], $db['pass'], [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
  ]);

  return $pdo;
}
