-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.php
More file actions
110 lines (88 loc) · 3.02 KB
/
Copy pathDataBase.php
File metadata and controls
110 lines (88 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Pet\DataBase\Config;
use Pet\Errors\AppException;
/**
* Конфигурация подключений к базам данных.
*
* Поддерживает множество именованных подключений.
* Чтение настроек из .env с возможностью переопределения через код.
*
* @package Pet\DataBase\Config
*/
class DataBase
{
private static array $connections = [];
private static string $defaultConnection = 'default';
private static function loadFromEnv(): void
{
if (isset(self::$connections['default'])) {
return;
}
self::$connections['default'] = [
'type' => defined('DB_TYPE') ? DB_TYPE : 'mysql',
'host' => defined('DB_HOST') ? DB_HOST : 'localhost',
'port' => defined('DB_PORT') ? DB_PORT : '3306',
'name' => defined('DB_NAME') ? DB_NAME : '',
'user' => defined('DB_USER') ? DB_USER : 'root',
'password' => defined('DB_PASSWORD') ? DB_PASSWORD : '',
'charset' => 'utf8mb4',
'options' => [],
];
}
public static function set(string $name, array $config): void
{
self::loadFromEnv();
$defaults = self::$connections['default'] ?? [
'type' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'name' => '',
'user' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'options' => [],
];
self::$connections[$name] = array_merge($defaults, $config);
}
public static function get(?string $name = null): array
{
self::loadFromEnv();
$name = $name ?? self::$defaultConnection;
if (!isset(self::$connections[$name])) {
throw new AppException("Database connection '$name' not configured");
}
return self::$connections[$name];
}
public static function setDefault(string $name): void
{
self::loadFromEnv();
if (!isset(self::$connections[$name])) {
throw new AppException("Cannot set default: connection '$name' not configured");
}
self::$defaultConnection = $name;
}
public static function getDefault(): string
{
self::loadFromEnv();
return self::$defaultConnection;
}
public static function has(string $name): bool
{
self::loadFromEnv();
return isset(self::$connections[$name]);
}
public static function dsn(?string $name = null): string
{
$config = self::get($name);
$dsn = "{$config['type']}:host={$config['host']};port={$config['port']};dbname={$config['name']}";
if (!empty($config['charset'])) {
$dsn .= ";charset={$config['charset']}";
}
return $dsn;
}
public static function reset(): void
{
self::$connections = [];
self::$defaultConnection = 'default';
}
}