Exposed more steps of the DbTools::create process.

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