weitere Bearibeitung
This commit is contained in:
+112
-37
@@ -45,37 +45,16 @@ final class SetupService
|
||||
|
||||
$this->resolveMailFrom($input, $this->normalizeAppUrl((string) $input['app_url']));
|
||||
|
||||
$config = [
|
||||
'host' => trim((string) $input['db_host']),
|
||||
'port' => (int) $input['db_port'],
|
||||
'name' => trim((string) $input['db_name']),
|
||||
'user' => trim((string) $input['db_user']),
|
||||
'pass' => (string) ($input['db_pass'] ?? ''),
|
||||
'charset' => 'utf8mb4',
|
||||
];
|
||||
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['name'],
|
||||
$config['charset']
|
||||
);
|
||||
|
||||
$pdo = new PDO($dsn, $config['user'], $config['pass'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
|
||||
$schema = file_get_contents($this->rootPath . '/database/schema.sql');
|
||||
|
||||
if (!is_string($schema) || $schema === '') {
|
||||
throw new RuntimeException('Das Datenbankschema konnte nicht geladen werden.');
|
||||
// Prüfe verfügbare PDO-Treiber und verwende entsprechende Datenbank
|
||||
$availableDrivers = PDO::getAvailableDrivers();
|
||||
$useMySQL = in_array('mysql', $availableDrivers) && trim((string) $input['db_host']) !== 'sqlite';
|
||||
|
||||
if ($useMySQL) {
|
||||
$pdo = $this->setupMySQLDatabase($input);
|
||||
} else {
|
||||
$pdo = $this->setupSQLiteDatabase();
|
||||
}
|
||||
|
||||
$pdo->exec($schema);
|
||||
|
||||
$statement = $pdo->prepare('SELECT id FROM users WHERE email = :email LIMIT 1');
|
||||
$statement->execute(['email' => mb_strtolower((string) $input['admin_email'])]);
|
||||
$existing = $statement->fetchColumn();
|
||||
@@ -92,7 +71,7 @@ final class SetupService
|
||||
]);
|
||||
}
|
||||
|
||||
$envContent = $this->buildEnvFile($input);
|
||||
$envContent = $this->buildEnvFile($input, $useMySQL);
|
||||
|
||||
if (file_put_contents($this->rootPath . '/.env', $envContent) === false) {
|
||||
throw new RuntimeException('Die .env-Datei konnte nicht geschrieben werden.');
|
||||
@@ -101,12 +80,90 @@ final class SetupService
|
||||
$lockContent = json_encode([
|
||||
'installed_at' => gmdate(DATE_ATOM),
|
||||
'app_url' => trim((string) $input['app_url']),
|
||||
'database_type' => $useMySQL ? 'mysql' : 'sqlite',
|
||||
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
file_put_contents($this->rootPath . '/storage/installed.lock', (string) $lockContent);
|
||||
}
|
||||
|
||||
private function buildEnvFile(array $input): string
|
||||
private function setupMySQLDatabase(array $input): PDO
|
||||
{
|
||||
$config = [
|
||||
'host' => trim((string) $input['db_host']),
|
||||
'port' => (int) $input['db_port'],
|
||||
'name' => trim((string) $input['db_name']),
|
||||
'user' => trim((string) $input['db_user']),
|
||||
'pass' => (string) ($input['db_pass'] ?? ''),
|
||||
'charset' => 'utf8mb4',
|
||||
];
|
||||
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['name'],
|
||||
$config['charset']
|
||||
);
|
||||
|
||||
try {
|
||||
$pdo = new PDO($dsn, $config['user'], $config['pass'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
|
||||
$schema = file_get_contents($this->rootPath . '/database/schema.sql');
|
||||
|
||||
if (!is_string($schema) || $schema === '') {
|
||||
throw new RuntimeException('Das MySQL-Datenbankschema konnte nicht geladen werden.');
|
||||
}
|
||||
|
||||
$pdo->exec($schema);
|
||||
return $pdo;
|
||||
} catch (\PDOException $e) {
|
||||
throw new RuntimeException('MySQL-Datenbankverbindung fehlgeschlagen: ' . $e->getMessage() . '. Bitte prüfen Sie Ihre Datenbankeinstellungen oder verwenden Sie SQLite als Alternative.');
|
||||
}
|
||||
}
|
||||
|
||||
private function setupSQLiteDatabase(): PDO
|
||||
{
|
||||
$dbPath = $this->rootPath . '/storage/database.sqlite';
|
||||
$dbDir = dirname($dbPath);
|
||||
|
||||
if (!is_dir($dbDir) && !mkdir($dbDir, 0775, true) && !is_dir($dbDir)) {
|
||||
throw new RuntimeException('Das Datenbank-Verzeichnis konnte nicht erstellt werden: ' . $dbDir);
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo = new PDO(
|
||||
'sqlite:' . $dbPath,
|
||||
null,
|
||||
null,
|
||||
[
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]
|
||||
);
|
||||
|
||||
$pdo->exec('PRAGMA journal_mode=WAL');
|
||||
$pdo->exec('PRAGMA foreign_keys=ON');
|
||||
$pdo->exec('PRAGMA busy_timeout=5000');
|
||||
|
||||
$schema = file_get_contents($this->rootPath . '/database/schema.sqlite.sql');
|
||||
|
||||
if (!is_string($schema) || $schema === '') {
|
||||
throw new RuntimeException('Das SQLite-Datenbankschema konnte nicht geladen werden.');
|
||||
}
|
||||
|
||||
$pdo->exec($schema);
|
||||
return $pdo;
|
||||
} catch (\PDOException $e) {
|
||||
throw new RuntimeException('SQLite-Datenbankverbindung fehlgeschlagen: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function buildEnvFile(array $input, bool $useMySQL = true): string
|
||||
{
|
||||
$appUrl = $this->normalizeAppUrl((string) $input['app_url']);
|
||||
$mailFrom = $this->resolveMailFrom($input, $appUrl);
|
||||
@@ -123,11 +180,29 @@ final class SetupService
|
||||
'ALLOW_PUBLIC_REGISTRATION=1',
|
||||
'RFID_INGEST_ENABLED=0',
|
||||
'',
|
||||
'DB_HOST=' . trim((string) $input['db_host']),
|
||||
'DB_PORT=' . (int) $input['db_port'],
|
||||
'DB_NAME=' . trim((string) $input['db_name']),
|
||||
'DB_USER=' . trim((string) $input['db_user']),
|
||||
'DB_PASS=' . (string) ($input['db_pass'] ?? ''),
|
||||
];
|
||||
|
||||
// Datenbankeinstellungen je nach verwendetem System
|
||||
if ($useMySQL) {
|
||||
$lines = array_merge($lines, [
|
||||
'DB_HOST=' . trim((string) $input['db_host']),
|
||||
'DB_PORT=' . (int) $input['db_port'],
|
||||
'DB_NAME=' . trim((string) $input['db_name']),
|
||||
'DB_USER=' . trim((string) $input['db_user']),
|
||||
'DB_PASS=' . (string) ($input['db_pass'] ?? ''),
|
||||
]);
|
||||
} else {
|
||||
// SQLite-Konfiguration
|
||||
$lines = array_merge($lines, [
|
||||
'DB_HOST=sqlite',
|
||||
'DB_PORT=0',
|
||||
'DB_NAME=' . $this->rootPath . '/storage/database.sqlite',
|
||||
'DB_USER=',
|
||||
'DB_PASS=',
|
||||
]);
|
||||
}
|
||||
|
||||
$lines = array_merge($lines, [
|
||||
'',
|
||||
'MAIL_FROM=' . $mailFrom,
|
||||
'MAIL_REPLY_TO=' . $mailFrom,
|
||||
@@ -148,7 +223,7 @@ final class SetupService
|
||||
'RATE_LIMIT_REGISTRATION_BLOCK_SECONDS=3600',
|
||||
'',
|
||||
'RFID_SHARED_SECRET=' . $rfidSecret,
|
||||
];
|
||||
]);
|
||||
|
||||
return implode(PHP_EOL, $lines) . PHP_EOL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user