|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CouchDB; |
|
4
|
|
|
|
|
5
|
|
|
use CouchDB\Encoder\JSONEncoder; |
|
6
|
|
|
use CouchDB\Events\EventArgs; |
|
7
|
|
|
use CouchDB\Exception\Exception; |
|
8
|
|
|
use CouchDB\Exception\InvalidDatabasenameException; |
|
9
|
|
|
use Doctrine\Common\EventManager; |
|
10
|
|
|
use GuzzleHttp\ClientInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Markus Bachmann <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Connection |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ClientInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EventManager |
|
24
|
|
|
*/ |
|
25
|
|
|
private $eventManager; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ClientInterface $client |
|
29
|
|
|
* @param EventManager $dispatcher |
|
30
|
|
|
*/ |
|
31
|
34 |
|
public function __construct(ClientInterface $client, EventManager $dispatcher = null) |
|
32
|
|
|
{ |
|
33
|
34 |
|
$this->client = $client; |
|
34
|
34 |
|
$this->eventManager = $dispatcher ?: new EventManager(); |
|
35
|
34 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return the event dispatcher. |
|
39
|
|
|
* |
|
40
|
|
|
* @return EventManager |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function getEventManager() |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->eventManager; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the couchdb version. |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
1 |
View Code Duplication |
public function version() |
|
53
|
|
|
{ |
|
54
|
1 |
|
$json = (string) $this->client->request('GET', '/')->getBody(); |
|
55
|
1 |
|
$value = JSONEncoder::decode($json); |
|
56
|
|
|
|
|
57
|
1 |
|
return $value['version']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Show all databases. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
1 |
View Code Duplication |
public function listDatabases() |
|
66
|
|
|
{ |
|
67
|
1 |
|
$json = (string) $this->client->request('GET', '/_all_dbs')->getBody(); |
|
68
|
1 |
|
$databases = JSONEncoder::decode($json); |
|
69
|
|
|
|
|
70
|
1 |
|
return $databases; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Drop a database. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function dropDatabase($name) |
|
81
|
|
|
{ |
|
82
|
2 |
|
if ($this->eventManager->hasListeners(Events::PRE_DROP_DATABASE)) { |
|
83
|
|
|
// @codeCoverageIgnoreStart |
|
84
|
|
|
$this->eventManager->dispatchEvent(Events::PRE_DROP_DATABASE, new EventArgs($this, $name)); |
|
85
|
|
|
// @codeCoverageIgnoreEnd |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
$response = $this->client->request('DELETE', sprintf('/%s/', urlencode($name))); |
|
89
|
|
|
|
|
90
|
2 |
|
if (404 === $response->getStatusCode()) { |
|
91
|
1 |
|
throw new Exception(sprintf('The database "%s" does not exist', $name)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$json = (string) $response->getBody(); |
|
95
|
1 |
|
$status = JSONEncoder::decode($json); |
|
96
|
|
|
|
|
97
|
1 |
|
if ($this->eventManager->hasListeners(Events::POST_DROP_DATABASE)) { |
|
98
|
|
|
// @codeCoverageIgnoreStart |
|
99
|
|
|
$this->eventManager->dispatchEvent(Events::POST_DROP_DATABASE, new EventArgs($this, $name)); |
|
100
|
|
|
// @codeCoverageIgnoreEnd |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return isset($status['ok']) && $status['ok'] === true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Select a database. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $name |
|
110
|
|
|
* |
|
111
|
|
|
* @throws Exception If the database doesn't exists. |
|
112
|
|
|
* |
|
113
|
|
|
* @return Database |
|
114
|
|
|
*/ |
|
115
|
2 |
View Code Duplication |
public function selectDatabase($name) |
|
116
|
|
|
{ |
|
117
|
2 |
|
$response = $this->client->request('GET', sprintf('/%s/', $name)); |
|
118
|
|
|
|
|
119
|
2 |
|
if (404 === $response->getStatusCode()) { |
|
120
|
1 |
|
throw new Exception(sprintf('The database "%s" does not exist', $name)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return $this->wrapDatabase($name); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Check if a database exists. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $name The database name |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function hasDatabase($name) |
|
134
|
|
|
{ |
|
135
|
2 |
|
$response = $this->client->request( |
|
136
|
2 |
|
'GET', |
|
137
|
2 |
|
sprintf('/%s/', urlencode($name)) |
|
138
|
2 |
|
); |
|
139
|
|
|
|
|
140
|
2 |
|
return 404 !== $response->getStatusCode(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Creates a new database. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $name The database name |
|
147
|
|
|
* |
|
148
|
|
|
* @throws Exception If the database could not be created. |
|
149
|
|
|
* |
|
150
|
|
|
* @return Database |
|
151
|
|
|
*/ |
|
152
|
4 |
|
public function createDatabase($name) |
|
153
|
|
|
{ |
|
154
|
4 |
|
if (preg_match('@[^a-z0-9\_\$\(\)+\-]@', $name)) { |
|
155
|
1 |
|
throw new InvalidDatabasenameException(sprintf( |
|
156
|
1 |
|
'The database name %s is invalid. The database name must match the following pattern (a-z0-9_$()+-)', |
|
157
|
|
|
$name |
|
158
|
1 |
|
)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
if ($this->eventManager->hasListeners(Events::PRE_CREATE_DATABASE)) { |
|
162
|
|
|
// @codeCoverageIgnoreStart |
|
163
|
|
|
$this->eventManager->dispatchEvent(Events::PRE_CREATE_DATABASE, new EventArgs($this, $name)); |
|
164
|
|
|
// @codeCoverageIgnoreEnd |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
3 |
|
$response = $this->client->request('PUT', sprintf('/%s', $name)); |
|
168
|
|
|
|
|
169
|
3 |
|
if (412 === $response->getStatusCode()) { |
|
170
|
1 |
|
throw new Exception(sprintf('The database "%s" already exist', $name)); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
2 |
|
$json = (string) $response->getBody(); |
|
174
|
2 |
|
$value = JSONEncoder::decode($json); |
|
175
|
|
|
|
|
176
|
2 |
|
if (isset($value['error'])) { |
|
177
|
1 |
|
throw new Exception(sprintf('[%s] Failed to create database "%s". (%s)', $value['error'], $name, $value['reason'])); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
$database = $this->wrapDatabase($name); |
|
181
|
|
|
|
|
182
|
1 |
|
if ($this->eventManager->hasListeners(Events::POST_CREATE_DATABASE)) { |
|
183
|
|
|
// @codeCoverageIgnoreStart |
|
184
|
|
|
$this->eventManager->dispatchEvent(Events::POST_CREATE_DATABASE, new EventArgs($database)); |
|
185
|
|
|
// @codeCoverageIgnoreEnd |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
return $database; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Gets the database. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $name The database name |
|
195
|
|
|
* |
|
196
|
|
|
* @return Database |
|
197
|
|
|
* |
|
198
|
|
|
* @deprecated This method will be removed in version 2. |
|
199
|
|
|
* |
|
200
|
|
|
* @codeCoverageIgnore |
|
201
|
|
|
*/ |
|
202
|
|
|
public function __get($name) |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->selectDatabase($name); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Drop a database. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $name The databas ename |
|
211
|
|
|
* |
|
212
|
|
|
* @return bool |
|
213
|
|
|
* |
|
214
|
|
|
* @deprecated This method will be removed in version 2. |
|
215
|
|
|
* |
|
216
|
|
|
* @codeCoverageIgnore |
|
217
|
|
|
*/ |
|
218
|
|
|
public function __unset($name) |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->dropDatabase($name); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Check if the database exist. |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $name The database name |
|
227
|
|
|
* |
|
228
|
|
|
* @return bool |
|
229
|
|
|
* |
|
230
|
|
|
* @deprecated This method will be removed in version 2. |
|
231
|
|
|
* |
|
232
|
|
|
* @codeCoverageIgnore |
|
233
|
|
|
*/ |
|
234
|
|
|
public function __isset($name) |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->hasDatabase($name); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Wraps the database to a object. |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $name The database name |
|
243
|
|
|
* |
|
244
|
|
|
* @return Database |
|
245
|
|
|
*/ |
|
246
|
2 |
|
private function wrapDatabase($name) |
|
247
|
|
|
{ |
|
248
|
2 |
|
return new Database($name, $this, $this->client); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|