/** * Function to return the database instance * @return Database */ public static function singleton() { try { if (!is_object(self::$_instance)) { self::$_instance = new Database(); } } catch (Exception $e) { // Convert mysqli_sql_exceptions into 500 errors if (!Config::get('productionEnv')) { switch ($e->getCode()) { case 1049: // Error: 1049 SQLSTATE: 42000 (ER_BAD_DB_ERROR) // throw new Exception("Please <a href=\"#/admin/installer\" class=\"alert-link\">install database</a>",500); self::createDB(); self::$_instance = new Database(); self::$_instance->logger->info("Automatically installing database for the first time"); self::$_instance->reinstallDB(); break; default: throw new Exception("{$e->getCode()}: {$e->getMessage()}", 500); } } else { throw new Exception("Cannot connect to database", 500); } } return self::$_instance; }