mirror of
https://codeberg.org/vlw/scaffold.git
synced 2026-02-26 11:31:57 +01:00
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:
parent
c501201f11
commit
e910baed6b
1 changed files with 33 additions and 0 deletions
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue