Exposed more steps of the DbTools::create process.

This commit is contained in:
flash 2023-07-11 22:42:57 +00:00
parent 31798a6b53
commit 939dcd10fe
2 changed files with 24 additions and 8 deletions

View file

@ -1 +1 @@
0.2307.112211
0.2307.112242

View file

@ -1,7 +1,7 @@
<?php
// DbTools.php
// Created: 2021-05-02
// Updated: 2022-02-28
// Updated: 2023-07-11
namespace Index\Data;
@ -20,12 +20,15 @@ final class DbTools {
'sqlite3' => SQLite\SQLiteBackend::class,
];
public static function create(string $dsn): IDbConnection {
static $backends = [];
private static function parseDsnUri(string $dsn): array {
$uri = parse_url($dsn);
if($uri === false)
throw new InvalidArgumentException('$dsn is not a valid uri.');
return $uri;
}
private static function resolveBackend(array $uri): IDbBackend {
static $backends = [];
$scheme = $uri['scheme'];
@ -52,9 +55,22 @@ final class DbTools {
$backends[$name] = $backend;
}
return $backend->createConnection(
$backend->parseDsn($uri)
);
return $backend;
}
public static function backend(string $dsn): IDbBackend {
return self::resolveBackend(self::parseDsnUri($dsn));
}
public static function parse(string $dsn): IDbConnectionInfo {
$uri = self::parseDsnUri($dsn);
return self::resolveBackend($uri)->parseDsn($uri);
}
public static function create(string $dsn): IDbConnection {
$uri = self::parseDsnUri($dsn);
$backend = self::resolveBackend($uri);
return $backend->createConnection($backend->parseDsn($uri));
}
/**