|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Yolk - Gamer Network's PHP Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2014 Gamer Network Ltd. |
|
6
|
|
|
* |
|
7
|
|
|
* Distributed under the MIT License, a copy of which is available in the |
|
8
|
|
|
* LICENSE file that was bundled with this package, or online at: |
|
9
|
|
|
* https://github.com/gamernetwork/yolk-database |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace yolk\database; |
|
13
|
|
|
|
|
14
|
|
|
use yolk\database\exceptions\ConfigurationException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Describes database connection details. |
|
18
|
|
|
* |
|
19
|
|
|
* @property-read string $type type of database being connected to |
|
20
|
|
|
* @property-read string $host hostname or ip address of database server |
|
21
|
|
|
* @property-read string $port network port to connect on |
|
22
|
|
|
* @property-read string $user user name used for authentication |
|
23
|
|
|
* @property-read string $pass password used for authentication |
|
24
|
|
|
* @property-read string $db name of the database schema to use |
|
25
|
|
|
* @property-read array $options array of database specific options |
|
26
|
|
|
*/ |
|
27
|
|
|
class DSN { |
|
28
|
|
|
|
|
29
|
|
|
const TYPE_MYSQL = 'mysql'; |
|
30
|
|
|
const TYPE_PGSQL = 'pgsql'; |
|
31
|
|
|
const TYPE_SQLITE = 'sqlite'; |
|
32
|
|
|
|
|
33
|
|
|
protected $config; |
|
34
|
|
|
|
|
35
|
|
|
public static function fromString( $config ) { |
|
36
|
|
|
|
|
37
|
|
|
// parse the string into some components |
|
38
|
|
|
$parts = parse_url(urldecode((string) $config)); |
|
39
|
|
|
|
|
40
|
|
|
// no point continuing if it went wrong |
|
41
|
|
|
if( !$parts || empty($parts['scheme']) ) |
|
42
|
|
|
throw new ConfigurationException('Invalid DSN string: '. $config); |
|
43
|
|
|
|
|
44
|
|
|
// use a closure to save loads of duplicate logic |
|
45
|
|
|
$select = function( $k, array $arr ) { |
|
46
|
|
|
return isset($arr[$k]) ? $arr[$k] : null; |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
// construct a well-formed array from the available components |
|
50
|
|
|
$config = array( |
|
51
|
|
|
'type' => $select('scheme', $parts), |
|
52
|
|
|
'host' => $select('host', $parts), |
|
53
|
|
|
'port' => $select('port', $parts), |
|
54
|
|
|
'user' => $select('user', $parts), |
|
55
|
|
|
'pass' => $select('pass', $parts), |
|
56
|
|
|
'db' => trim($select('path', $parts), '/'), |
|
57
|
|
|
'options' => [], |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
if( isset($parts['query']) ) { |
|
61
|
|
|
parse_str($parts['query'], $config['options']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return new static($config); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function fromJSON( $config ) { |
|
69
|
|
|
|
|
70
|
|
|
$config = json_decode($config, true); |
|
71
|
|
|
|
|
72
|
|
|
if( !$config ) |
|
73
|
|
|
throw new ConfigurationException('Invalid JSON configuration'); |
|
74
|
|
|
|
|
75
|
|
|
return new static($config); |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Create a DSN from an array of parameters. |
|
81
|
|
|
* scheme - type of database (mysql, pgsql, sqlite) required |
|
82
|
|
|
* host - hostname of database server |
|
83
|
|
|
* port - network port to connect on |
|
84
|
|
|
* user - user to connect as |
|
85
|
|
|
* pass - user's password |
|
86
|
|
|
* db - name of the database schema to connect to |
|
87
|
|
|
* options - an array of database specific options |
|
88
|
|
|
* @param array $dsn |
|
|
|
|
|
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct( array $config ) { |
|
91
|
|
|
|
|
92
|
|
|
if( empty($config['type']) ) |
|
93
|
|
|
throw new ConfigurationException('No database type specified'); |
|
94
|
|
|
|
|
95
|
|
|
$config = $config + array( |
|
96
|
|
|
'host' => 'localhost', |
|
97
|
|
|
'port' => null, |
|
98
|
|
|
'user' => null, |
|
99
|
|
|
'pass' => null, |
|
100
|
|
|
'db' => null, |
|
101
|
|
|
'options' => [], |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$this->configure($config); |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function isMySQL() { |
|
109
|
|
|
return $this->config['type'] == static::TYPE_MYSQL; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function isPgSQL() { |
|
113
|
|
|
return $this->config['type'] == static::TYPE_PGSQL; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function isSQLite() { |
|
117
|
|
|
return $this->config['type'] == static::TYPE_SQLITE; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Dynamic property access. |
|
122
|
|
|
* @param string $key |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
public function __get( $key ) { |
|
126
|
|
|
return isset($this->config[$key]) ? $this->config[$key] : null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Dynamic property access. |
|
131
|
|
|
* @param string $key |
|
132
|
|
|
* @return boolean |
|
133
|
|
|
*/ |
|
134
|
|
|
public function __isset( $key ) { |
|
135
|
|
|
return isset($this->config[$key]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Convert the DSN into a URI-type string. |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function toString() { |
|
143
|
|
|
|
|
144
|
|
|
$str = $this->config['type']. '://'; |
|
145
|
|
|
|
|
146
|
|
View Code Duplication |
if( $this->config['user'] ) { |
|
|
|
|
|
|
147
|
|
|
$str .= $this->config['user']; |
|
148
|
|
|
if( $this->config['pass'] ) |
|
149
|
|
|
$str .= ':'. $this->config['pass']; |
|
150
|
|
|
$str .= '@'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
if( $this->config['host'] ) { |
|
|
|
|
|
|
154
|
|
|
$str .= $this->config['host']; |
|
155
|
|
|
if( $this->config['port'] ) |
|
156
|
|
|
$str .= ':'. $this->config['port']; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$str .= '/'. $this->config['db']; |
|
160
|
|
|
|
|
161
|
|
|
if( $this->config['options'] ) { |
|
162
|
|
|
$str .= '?'; |
|
163
|
|
|
foreach( $this->config['options'] as $k => $v ) { |
|
164
|
|
|
$str .= "$k=>$v&"; |
|
165
|
|
|
} |
|
166
|
|
|
$str = substr($str, 0, -1); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $str; |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Return a connection string for use by PDO. |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getConnectionString() { |
|
178
|
|
|
return $this->config['pdo']; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Ensure the dsn configuration is valid. |
|
183
|
|
|
* @param array $config |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function configure( array $config ) { |
|
187
|
|
|
|
|
188
|
|
|
if( !$config['db'] ) |
|
189
|
|
|
throw new ConfigurationException('No database schema specified'); |
|
190
|
|
|
|
|
191
|
|
|
switch( $config['type'] ) { |
|
192
|
|
|
|
|
193
|
|
|
case static::TYPE_MYSQL: |
|
194
|
|
|
$this->config = $this->configureMySQL($config); |
|
195
|
|
|
break; |
|
196
|
|
|
|
|
197
|
|
|
case static::TYPE_PGSQL: |
|
198
|
|
|
$this->config = $this->configureMySQL($config); |
|
199
|
|
|
break; |
|
200
|
|
|
|
|
201
|
|
|
case static::TYPE_SQLITE: |
|
202
|
|
|
$this->config = $this->configureMySQL($config); |
|
203
|
|
|
break; |
|
204
|
|
|
|
|
205
|
|
|
default: |
|
206
|
|
|
throw new ConfigurationException('Invalid database type: '. $config['type']); |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Configure a MySQL DSN. |
|
214
|
|
|
* @param array $config |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
View Code Duplication |
protected function configureMySQL( array $config ) { |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
if( !$config['port'] ) |
|
220
|
|
|
$config['port'] = 3306; |
|
221
|
|
|
|
|
222
|
|
|
// construct a MySQL PDO connection string |
|
223
|
|
|
$config['pdo'] = sprintf( |
|
224
|
|
|
"mysql:host=%s;port=%s;dbname=%s", |
|
225
|
|
|
$config['host'], |
|
226
|
|
|
$config['port'], |
|
227
|
|
|
$config['db'] |
|
228
|
|
|
); |
|
229
|
|
|
|
|
230
|
|
|
return $config; |
|
231
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Configure a PostgreSQL DSN. |
|
236
|
|
|
* @param array $config |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
|
View Code Duplication |
protected function configurePgSQL( array $config ) { |
|
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
if( !$config['port'] ) |
|
242
|
|
|
$config['port'] = 5432; |
|
243
|
|
|
|
|
244
|
|
|
// construct a PgSQL PDO connection string |
|
245
|
|
|
$config['pdo'] = sprintf( |
|
246
|
|
|
"pgsql:host=%s;port=%s;dbname=%s", |
|
247
|
|
|
$config['host'], |
|
248
|
|
|
$config['port'], |
|
249
|
|
|
$config['db'] |
|
250
|
|
|
); |
|
251
|
|
|
|
|
252
|
|
|
return $config; |
|
253
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Configure a SQLite DSN. |
|
258
|
|
|
* @param array $config |
|
259
|
|
|
* @return array |
|
260
|
|
|
*/ |
|
261
|
|
|
protected function configureSQLite( array $config ) { |
|
262
|
|
|
|
|
263
|
|
|
// these should always be null as they're invalid for SQLite connections |
|
264
|
|
|
$config['host'] = 'localhost'; |
|
265
|
|
|
$config['port'] = null; |
|
266
|
|
|
$config['user'] = null; |
|
267
|
|
|
$config['pass'] = null; |
|
268
|
|
|
|
|
269
|
|
|
// construct a SQLite PDO connection string |
|
270
|
|
|
$config['pdo'] = sprintf( |
|
271
|
|
|
'sqlite::%s', |
|
272
|
|
|
$config['db'] |
|
273
|
|
|
); |
|
274
|
|
|
|
|
275
|
|
|
return $config; |
|
276
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
// EOF |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.