You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
<?php
|
|
|
|
use IQBall\Core\Connection;
|
|
use IQBall\Core\Gateway\AccountGateway;
|
|
use IQBall\Core\Model\AuthModel;
|
|
|
|
/**
|
|
* @return PDO The PDO instance of the configuration's database connexion.
|
|
*/
|
|
function get_database(): PDO {
|
|
// defined by profiles.
|
|
global $data_source_name;
|
|
$pdo = new PDO($data_source_name, DATABASE_USER, DATABASE_PASSWORD, [PDO::ERRMODE_EXCEPTION]);
|
|
|
|
$database_exists = $pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")->fetchColumn() > 0;
|
|
|
|
if ($database_exists) {
|
|
return $pdo;
|
|
}
|
|
|
|
foreach (scandir(__DIR__) as $file) {
|
|
if (preg_match("/.*\.sql$/i", $file)) {
|
|
$content = file_get_contents(__DIR__ . "/" . $file);
|
|
|
|
$pdo->exec($content);
|
|
}
|
|
}
|
|
|
|
init_database($pdo);
|
|
|
|
return $pdo;
|
|
}
|
|
|
|
|
|
function init_database(PDO $pdo): void {
|
|
$accounts = new AccountGateway(new Connection($pdo));
|
|
|
|
$defaultAccounts = ["maxime", "mael", "yanis", "vivien"];
|
|
|
|
foreach ($defaultAccounts as $name) {
|
|
$email = "$name@mail.com";
|
|
$id = $accounts->insertAccount($name, $email, AuthModel::generateToken(), password_hash("123456", PASSWORD_DEFAULT), "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png");
|
|
$accounts->setIsAdmin($id, true);
|
|
}
|
|
}
|