feat: add static method to create new Database instance from credentials (#22)

In this PR we add a new static method for creating a `vlw\Scaffold\Database\Database` instance from a set of provided credentials. This is of course not ideal and a better way to instance this class should be added in the future.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/22
This commit is contained in:
Victor Westerlund 2026-02-11 17:25:22 +01:00
parent c501201f11
commit 1536079fe3

View file

@ -15,6 +15,39 @@
public const DATE_FORMAT = "Y-m-d";
public const DATETIME_FORMAT = "Y-m-d H:i:s";
private const DEFAULT_HOSTNAME = "localhost";
private const DEFAULT_USERNAME = "www-data";
private const DEFAULT_PASSWORD = "";
/**
* Create a new Database instance from credentials
*
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @return static
*/
public static function from_credentials(
?string $host = self::DEFAULT_HOSTNAME,
?string $username = self::DEFAULT_USERNAME,
?string $password = self::DEFAULT_PASSWORD,
string $database
) {
// Create key if it does not exist
if (!$_ENV["mariadb"]) {
$_ENV["mariadb"] = [];
}
// Set environment variables from credentials
$_ENV["mariadb"]["host"] = $host;
$_ENV["mariadb"]["user"] = $username;
$_ENV["mariadb"]["pass"] = $password;
$_ENV["mariadb"]["db"] = $database;
return new static();
}
/**
* Wrap a new vlw/MySQL instance with credentials from project .env.ini
*/