DatabaseFactory.php
- Pfad:
src/Infrastructure/Config/DatabaseFactory.php - Namespace: Infrastructure\Config
- Zeilen: 89 | Größe: 2,236 Bytes
- Geändert: 2025-12-25 16:55:28 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 2
- use PDO
- use RuntimeException
Klassen 1
-
DatabaseFactoryclass Zeile 12
Funktionen 5
-
dev()public Zeile 27 -
content()public Zeile 37 -
getConnection()public Zeile 47 -
closeAll()Zeile 63 -
createConnection()Zeile 71
Verwendet von 1
- services.php use
Versionen 3
-
v3
2025-12-25 16:55 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v2
2025-12-23 08:00 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-23 08:00 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace Infrastructure\Config;
// @responsibility: Factory für benannte Datenbankverbindungen (dev, content)
use PDO;
use RuntimeException;
final class DatabaseFactory
{
private const array DATABASES = [
'dev' => 'ki_dev',
'content' => 'ki_content',
];
/** @var array<string, PDO> */
private static array $connections = [];
/**
* Gets the 'dev' database connection (ki_dev).
*
* Contains: dokumentation, dokumentation_chunks, tasks, contracts, protokoll
*/
public static function dev(): PDO
{
return self::getConnection('dev');
}
/**
* Gets the 'content' database connection (ki_content).
*
* Contains: content studio data, semantic entities, relations
*/
public static function content(): PDO
{
return self::getConnection('content');
}
/**
* Gets a connection by name.
*
* @param string $name Connection name ('dev' or 'content')
*/
public static function getConnection(string $name): PDO
{
if (!isset(self::DATABASES[$name])) {
throw new RuntimeException("Unknown database connection: {$name}");
}
if (!isset(self::$connections[$name])) {
self::$connections[$name] = self::createConnection(self::DATABASES[$name]);
}
return self::$connections[$name];
}
/**
* Closes all connections and clears the cache.
*/
public static function closeAll(): void
{
self::$connections = [];
}
/**
* Creates a new PDO connection.
*/
private static function createConnection(string $database): PDO
{
$host = CredentialService::getMariaDbHost();
$user = CredentialService::getMariaDbUser();
$password = CredentialService::getMariaDbPassword();
return new PDO(
"mysql:host={$host};dbname={$database};charset=utf8mb4",
$user,
$password,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
);
}
}