/
Connector.php
185 lines (170 loc) · 6.04 KB
/
Connector.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Connector;
use Connector\Backend\BackendInterface;
use PDO;
use RuntimeException;
class Connector
{
protected $backends = [];
public function registerBackend(BackendInterface $backend)
{
$this->backends[] = $backend;
}
public function getConfig($dsn)
{
if (filter_var($dsn, FILTER_VALIDATE_URL)) {
$config = new Config();
$config->setDriver(parse_url($dsn, PHP_URL_SCHEME));
$config->setUsername(parse_url($dsn, PHP_URL_USER));
$config->setPassword(parse_url($dsn, PHP_URL_PASS));
$config->setAddress(parse_url($dsn, PHP_URL_HOST));
$port = parse_url($dsn, PHP_URL_PORT);
if ($port) {
$config->setPort($port);
}
$urlPath = parse_url($dsn, PHP_URL_PATH);
if (parse_url($dsn, PHP_URL_SCHEME) == 'sqlite') {
$i = pathinfo($urlPath);
$config->setName($i['filename']);
$config->setFileName($urlPath);
} else {
$config->setName(substr($urlPath, 1));
}
// TODO: support further resolving cluster/server settings
// from backends based on address/host/server?
return $config;
}
foreach ($this->backends as $backend) {
$dsnKeys = $backend->getKeys($dsn);
if ($dsnKeys) {
$config = new Config();
$this->loadKeys($config, $dsnKeys);
if ($config->getCluster()) {
$clusterKeys = $backend->getKeys('clusters/' . $config->getCluster());
if (!$clusterKeys) {
throw new RuntimeException("No configuration keys found for cluster: " . $config->getCluster());
}
$this->loadKeys($config, $clusterKeys);
}
if ($config->getServer()) {
$serverKeys = $backend->getKeys('servers/' . $config->getServer());
if (!$serverKeys) {
throw new RuntimeException("No configuration keys found for server: " . $config->getServer());
}
$this->loadKeys($config, $serverKeys);
}
return $config;
}
}
throw new RuntimeException("Can't resolve DSN `" . $dsn . '`');
}
public function getPdoDsn(Config $config, $mode = 'db')
{
switch ($mode) {
case 'db':
case 'server':
break;
default:
throw new RuntimeException("Invalid mode: " . $mode);
}
$pdoDsn = $config->getDriver() . ':';
switch ($config->getDriver()) {
case 'pgsql':
case 'mysql':
$pdoDsn .= 'host=' . $config->getAddress() . ';';
if ($config->getPort()) {
$pdoDsn .= 'port=' . $config->getPort() . ';';
}
if ($mode=='db') {
$pdoDsn .= 'dbname=' . $config->getName() . ';';
}
$pdoDsn .= 'charset=utf8;';
break;
case 'sqlite':
$pdoDsn .= $config->getFileName();
break;
default:
throw new RuntimeException("Unsupported driver: " . $config->getDriver());
}
return $pdoDsn;
}
public function create(Config $config)
{
if ($this->exists($config)) {
throw new RuntimeException("Database already exists");
}
// TODO: validate dbname?
switch ($config->getDriver()) {
case 'mysql':
$pdo = $this->getPdo($config, 'server');
$stmt = $pdo->prepare(
"CREATE DATABASE " . $config->getName()
);
$stmt->execute();
break;
default:
throw new RuntimeException("Unsupported driver: " . $config->getDriver());
}
}
public function drop(Config $config)
{
if (!$this->exists($config)) {
throw new RuntimeException("Database does not exists");
}
// TODO: validate dbname?
switch ($config->getDriver()) {
case 'mysql':
$pdo = $this->getPdo($config, 'server');
$stmt = $pdo->prepare(
"DROP DATABASE " . $config->getName()
);
$stmt->execute();
break;
default:
throw new RuntimeException("Unsupported driver: " . $config->getDriver());
}
}
public function exists(Config $config)
{
switch ($config->getDriver()) {
case 'mysql':
$pdo = $this->getPdo($config, 'server');
$stmt = $pdo->prepare(
"SELECT COUNT(*) AS c FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = :dbname"
);
$stmt->execute(
[
':dbname' => $config->getName()
]
);
return (bool) $stmt->fetchColumn();
case 'sqlite':
return $this->getPdo($config, 'server');
break;
default:
throw new RuntimeException("Unsupported driver");
}
}
public function getPdo(Config $config, $mode = 'db')
{
$pdoDsn = $this->getPdoDsn($config, $mode);
$config->validate(); // throws if invalid
return new PDO($pdoDsn, $config->getUsername(), $config->getPassword());
}
public function loadKeys(Config $config, $keys)
{
if (!$keys) {
return;
}
foreach ($keys as $key => $value) {
$methodName = 'set' . ucfirst($key);
if (method_exists($config, $methodName)) {
$config->$methodName($value);
} else {
$config->setProperty($key, $value);
}
}
return $config;
}
}