1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\Session; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\ParameterHolder; |
13
|
|
|
use PommProject\Foundation\Exception\ConnectionException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ConnectionConfigurator |
17
|
|
|
* |
18
|
|
|
* This class is responsible of configuring the connection. |
19
|
|
|
* It has to extract information from the DSN and ensure required arguments |
20
|
|
|
* are present. |
21
|
|
|
* |
22
|
|
|
* @package Foundation |
23
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
24
|
|
|
* @author Grégoire HUBERT |
25
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
26
|
|
|
*/ |
27
|
|
|
class ConnectionConfigurator |
28
|
|
|
{ |
29
|
|
|
protected $configuration; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* __construct |
33
|
|
|
* |
34
|
|
|
* Initialize configuration. |
35
|
|
|
* |
36
|
|
|
* @param array $dsn |
37
|
|
|
*/ |
38
|
|
|
public function __construct($dsn, $persist = false) |
39
|
|
|
{ |
40
|
|
|
$this->configuration = new ParameterHolder( |
41
|
|
|
[ |
42
|
|
|
'dsn' => $dsn, |
43
|
|
|
'configuration' => $this->getDefaultConfiguration(), |
44
|
|
|
'persist' => $persist, |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
$this->parseDsn(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* addConfiguration |
52
|
|
|
* |
53
|
|
|
* Add configuration settings. If settings exist, they are overridden. |
54
|
|
|
* |
55
|
|
|
* @param array $configuration |
56
|
|
|
* @return Connection $this |
57
|
|
|
*/ |
58
|
|
|
public function addConfiguration(array $configuration) |
59
|
|
|
{ |
60
|
|
|
$this ->configuration->setParameter( |
61
|
|
|
'configuration', |
62
|
|
|
array_merge( |
63
|
|
|
$this->configuration->getParameter('configuration'), |
64
|
|
|
$configuration |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* set |
73
|
|
|
* |
74
|
|
|
* Set a new configuration setting. |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @param mixed $value |
78
|
|
|
* @return ConnectionConfigurator $this |
79
|
|
|
*/ |
80
|
|
|
public function set($name, $value) |
81
|
|
|
{ |
82
|
|
|
$configuration = $this->configuration->getParameter('configuration'); |
83
|
|
|
$configuration[$name] = $value; |
84
|
|
|
$this |
85
|
|
|
->configuration |
86
|
|
|
->setParameter( |
87
|
|
|
'configuration', |
88
|
|
|
$configuration |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* parseDsn() |
97
|
|
|
* |
98
|
|
|
* Sets the different parameters from the DSN. |
99
|
|
|
* |
100
|
|
|
* @return Connection $this |
101
|
|
|
* @throws ConnectionException |
102
|
|
|
*/ |
103
|
|
|
private function parseDsn() |
104
|
|
|
{ |
105
|
|
|
$dsn = $this->configuration->mustHave('dsn')->getParameter('dsn'); |
106
|
|
|
if (!preg_match( |
107
|
|
|
'#([a-z]+)://([^:@]+)(?::([^@]*))?(?:@([\w\.-]+|!/.+[^/]!)(?::(\w+))?)?/(.+)#', |
108
|
|
|
$dsn, |
109
|
|
|
$matches |
110
|
|
|
)) { |
111
|
|
|
throw new ConnectionException(sprintf('Could not parse DSN "%s".', $dsn)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($matches[1] == null || $matches[1] !== 'pgsql') { |
115
|
|
|
throw new ConnectionException( |
116
|
|
|
sprintf( |
117
|
|
|
"bad protocol information '%s' in dsn '%s'. Pomm does only support 'pgsql' for now.", |
118
|
|
|
$matches[1], |
119
|
|
|
$dsn |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$adapter = $matches[1]; |
125
|
|
|
|
126
|
|
|
if ($matches[2] === null) { |
127
|
|
|
throw new ConnectionException( |
128
|
|
|
sprintf( |
129
|
|
|
"No user information in dsn '%s'.", |
130
|
|
|
$dsn |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$user = $matches[2]; |
136
|
|
|
$pass = $matches[3]; |
137
|
|
|
|
138
|
|
|
if (preg_match('/!(.*)!/', $matches[4], $host_matches)) { |
139
|
|
|
$host = $host_matches[1]; |
140
|
|
|
} else { |
141
|
|
|
$host = $matches[4]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$port = $matches[5]; |
145
|
|
|
|
146
|
|
|
if ($matches[6] === null) { |
147
|
|
|
throw new ConnectionException( |
148
|
|
|
sprintf( |
149
|
|
|
"No database name in dsn '%s'.", |
150
|
|
|
$dsn |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$database = $matches[6]; |
156
|
|
|
$this->configuration |
157
|
|
|
->setParameter('adapter', $adapter) |
158
|
|
|
->setParameter('user', $user) |
159
|
|
|
->setParameter('pass', $pass) |
160
|
|
|
->setParameter('host', $host) |
161
|
|
|
->setParameter('port', $port) |
162
|
|
|
->setParameter('database', $database) |
163
|
|
|
->mustHave('user') |
164
|
|
|
->mustHave('database') |
165
|
|
|
; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* getConnectionString |
172
|
|
|
* |
173
|
|
|
* Return the connection string. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getConnectionString() |
178
|
|
|
{ |
179
|
|
|
$this->parseDsn(); |
180
|
|
|
$connect_parameters = [ |
181
|
|
|
sprintf( |
182
|
|
|
"user=%s dbname=%s", |
183
|
|
|
$this->configuration['user'], |
184
|
|
|
$this->configuration['database'] |
185
|
|
|
) |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
View Code Duplication |
if ($this->configuration['host'] !== '') { |
189
|
|
|
$connect_parameters[] = sprintf('host=%s', $this->configuration['host']); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
View Code Duplication |
if ($this->configuration['port'] !== '') { |
193
|
|
|
$connect_parameters[] = sprintf('port=%s', $this->configuration['port']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
if ($this->configuration['pass'] !== '') { |
197
|
|
|
$connect_parameters[] = sprintf('password=%s', addslashes($this->configuration['pass'])); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return join(' ', $connect_parameters); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* getPersist |
205
|
|
|
* |
206
|
|
|
* Return whether or not to persist the connection to the DB. |
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
public function getPersist() |
211
|
|
|
{ |
212
|
|
|
return $this->configuration['persist']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* getDefaultConfiguration |
217
|
|
|
* |
218
|
|
|
* Standalone, default configuration. |
219
|
|
|
* |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
protected function getDefaultConfiguration() |
223
|
|
|
{ |
224
|
|
|
return []; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* getConfiguration |
229
|
|
|
* |
230
|
|
|
* Return current configuration settings. |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
* @throws ConnectionException |
234
|
|
|
*/ |
235
|
|
|
public function getConfiguration() |
236
|
|
|
{ |
237
|
|
|
$configuration = $this->configuration->getParameter('configuration'); |
238
|
|
|
|
239
|
|
|
if (!is_array($configuration)) { |
240
|
|
|
throw new ConnectionException( |
241
|
|
|
sprintf( |
242
|
|
|
"Invalid configuration. It should be an array of settings, '%s' returned.", |
243
|
|
|
gettype($configuration) |
244
|
|
|
) |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $configuration; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|