1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the vendor/project package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\KeyValueStore; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use Exception; |
16
|
|
|
use MongoDB\BSON\Binary; |
17
|
|
|
use MongoDB\Collection; |
18
|
|
|
use MongoDB\Driver\Exception\UnexpectedValueException; |
19
|
|
|
use Webmozart\KeyValueStore\Api\KeyValueStore; |
20
|
|
|
use Webmozart\KeyValueStore\Api\NoSuchKeyException; |
21
|
|
|
use Webmozart\KeyValueStore\Api\ReadException; |
22
|
|
|
use Webmozart\KeyValueStore\Api\UnserializationFailedException; |
23
|
|
|
use Webmozart\KeyValueStore\Api\UnsupportedValueException; |
24
|
|
|
use Webmozart\KeyValueStore\Api\WriteException; |
25
|
|
|
use Webmozart\KeyValueStore\Util\KeyUtil; |
26
|
|
|
use Webmozart\KeyValueStore\Util\Serializer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A key-value-store backed by MongoDB. |
30
|
|
|
* |
31
|
|
|
* @since 1.0 |
32
|
|
|
* |
33
|
|
|
* @author Bernhard Schussek <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class MongoDbStore implements KeyValueStore |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Flag: Disable serialization. |
39
|
|
|
*/ |
40
|
|
|
const NO_SERIALIZE = 1; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Flag: Support storage of binary data. |
44
|
|
|
*/ |
45
|
|
|
const SUPPORT_BINARY = 2; |
46
|
|
|
|
47
|
|
|
private static $typeMap = array( |
48
|
|
|
'root' => 'array', |
49
|
|
|
'document' => 'array', |
50
|
|
|
'array' => 'array', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Collection |
55
|
|
|
*/ |
56
|
|
|
private $collection; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Closure |
60
|
|
|
*/ |
61
|
|
|
private $serialize; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Closure |
65
|
|
|
*/ |
66
|
|
|
private $unserialize; |
67
|
|
|
|
68
|
332 |
|
public function __construct(Collection $collection, $flags = 0) |
69
|
|
|
{ |
70
|
332 |
|
$this->collection = $collection; |
71
|
|
|
|
72
|
332 |
|
if ($flags & self::NO_SERIALIZE) { |
73
|
166 |
|
if ($flags & self::SUPPORT_BINARY) { |
74
|
|
|
$this->serialize = function ($unserialized) { |
75
|
55 |
|
if (!is_string($unserialized)) { |
76
|
17 |
|
throw UnsupportedValueException::forValue($unserialized, $this); |
77
|
|
|
} |
78
|
|
|
|
79
|
38 |
|
return new Binary($unserialized, Binary::TYPE_GENERIC); |
80
|
|
|
}; |
81
|
|
|
$this->unserialize = function (Binary $serialized) { |
82
|
18 |
|
return $serialized->getData(); |
83
|
83 |
|
}; |
84
|
|
|
} else { |
85
|
|
|
$this->serialize = function ($unserialized) { |
86
|
55 |
|
if (!is_scalar($unserialized) && !is_array($unserialized) && null !== $unserialized) { |
87
|
5 |
|
throw UnsupportedValueException::forValue($unserialized, $this); |
88
|
|
|
} |
89
|
|
|
|
90
|
50 |
|
return $unserialized; |
91
|
|
|
}; |
92
|
|
|
$this->unserialize = function ($serialized) { |
93
|
28 |
|
return $serialized; |
94
|
166 |
|
}; |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
194 |
|
if ($flags & self::SUPPORT_BINARY) { |
98
|
|
|
$this->serialize = function ($unserialized) { |
99
|
55 |
|
return new Binary( |
100
|
55 |
|
Serializer::serialize($unserialized), |
101
|
53 |
|
Binary::TYPE_GENERIC |
102
|
|
|
); |
103
|
|
|
}; |
104
|
|
|
$this->unserialize = function (Binary $serialized) { |
105
|
33 |
|
return Serializer::unserialize($serialized->getData()); |
106
|
83 |
|
}; |
107
|
|
|
} else { |
108
|
|
|
$this->serialize = function ($unserialized) { |
109
|
59 |
|
return Serializer::serialize($unserialized); |
110
|
|
|
}; |
111
|
46 |
|
$this->unserialize = function ($serialized) { |
112
|
46 |
|
return Serializer::unserialize($serialized); |
113
|
|
|
}; |
114
|
|
|
} |
115
|
|
|
} |
116
|
332 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
232 |
|
public function set($key, $value) |
122
|
|
|
{ |
123
|
232 |
|
KeyUtil::validate($key); |
124
|
|
|
|
125
|
224 |
|
$serialized = $this->serialize->__invoke($value); |
126
|
|
|
|
127
|
|
|
try { |
128
|
198 |
|
$this->collection->replaceOne( |
129
|
198 |
|
array('_id' => $key), |
130
|
198 |
|
array('_id' => $key, 'value' => $serialized), |
131
|
198 |
|
array('upsert' => true) |
132
|
|
|
); |
133
|
9 |
|
} catch (UnexpectedValueException $e) { |
|
|
|
|
134
|
5 |
|
throw UnsupportedValueException::forType('binary', $this, 0, $e); |
135
|
4 |
|
} catch (Exception $e) { |
136
|
4 |
|
throw WriteException::forException($e); |
137
|
|
|
} |
138
|
189 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
28 |
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
144
|
|
|
{ |
145
|
28 |
|
KeyUtil::validate($key); |
146
|
|
|
|
147
|
|
|
try { |
148
|
20 |
|
$document = $this->collection->findOne( |
149
|
20 |
|
array('_id' => $key), |
150
|
20 |
|
array('typeMap' => self::$typeMap) |
151
|
|
|
); |
152
|
4 |
|
} catch (Exception $e) { |
153
|
4 |
|
throw ReadException::forException($e); |
154
|
|
|
} |
155
|
|
|
|
156
|
16 |
|
if (null === $document) { |
157
|
4 |
|
return $default; |
158
|
|
|
} |
159
|
|
|
|
160
|
12 |
|
return $this->unserialize->__invoke($document['value']); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
105 |
View Code Duplication |
public function getOrFail($key) |
|
|
|
|
167
|
|
|
{ |
168
|
105 |
|
KeyUtil::validate($key); |
169
|
|
|
|
170
|
|
|
try { |
171
|
97 |
|
$document = $this->collection->findOne( |
172
|
97 |
|
array('_id' => $key), |
173
|
97 |
|
array('typeMap' => self::$typeMap) |
174
|
|
|
); |
175
|
4 |
|
} catch (Exception $e) { |
176
|
4 |
|
throw ReadException::forException($e); |
177
|
|
|
} |
178
|
|
|
|
179
|
93 |
|
if (null === $document) { |
180
|
4 |
|
throw NoSuchKeyException::forKey($key); |
181
|
|
|
} |
182
|
|
|
|
183
|
89 |
|
return $this->unserialize->__invoke($document['value']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
24 |
|
public function getMultiple(array $keys, $default = null) |
190
|
|
|
{ |
191
|
24 |
|
KeyUtil::validateMultiple($keys); |
192
|
|
|
|
193
|
16 |
|
$values = array_fill_keys($keys, $default); |
194
|
|
|
|
195
|
|
|
try { |
196
|
16 |
|
$cursor = $this->collection->find( |
197
|
16 |
|
array('_id' => array('$in' => array_values($keys))), |
198
|
16 |
|
array('typeMap' => self::$typeMap) |
199
|
|
|
); |
200
|
|
|
|
201
|
12 |
|
foreach ($cursor as $document) { |
202
|
12 |
|
$values[$document['_id']] = $this->unserialize->__invoke($document['value']); |
203
|
|
|
} |
204
|
8 |
|
} catch (UnserializationFailedException $e) { |
205
|
4 |
|
throw $e; |
206
|
4 |
|
} catch (Exception $e) { |
207
|
4 |
|
throw ReadException::forException($e); |
208
|
|
|
} |
209
|
|
|
|
210
|
8 |
|
return $values; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
105 |
|
public function getMultipleOrFail(array $keys) |
217
|
|
|
{ |
218
|
105 |
|
KeyUtil::validateMultiple($keys); |
219
|
|
|
|
220
|
97 |
|
$values = array(); |
221
|
|
|
|
222
|
|
|
try { |
223
|
97 |
|
$cursor = $this->collection->find( |
224
|
97 |
|
array('_id' => array('$in' => array_values($keys))), |
225
|
97 |
|
array('typeMap' => self::$typeMap) |
226
|
|
|
); |
227
|
|
|
|
228
|
93 |
|
foreach ($cursor as $document) { |
229
|
93 |
|
$values[$document['_id']] = $this->unserialize->__invoke($document['value']); |
230
|
|
|
} |
231
|
8 |
|
} catch (UnserializationFailedException $e) { |
232
|
4 |
|
throw $e; |
233
|
4 |
|
} catch (Exception $e) { |
234
|
4 |
|
throw ReadException::forException($e); |
235
|
|
|
} |
236
|
|
|
|
237
|
89 |
|
$notFoundKeys = array_diff($keys, array_keys($values)); |
238
|
|
|
|
239
|
89 |
|
if (count($notFoundKeys) > 0) { |
240
|
4 |
|
throw NoSuchKeyException::forKeys($notFoundKeys); |
241
|
|
|
} |
242
|
|
|
|
243
|
85 |
|
return $values; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
44 |
View Code Duplication |
public function remove($key) |
|
|
|
|
250
|
|
|
{ |
251
|
44 |
|
KeyUtil::validate($key); |
252
|
|
|
|
253
|
|
|
try { |
254
|
36 |
|
$result = $this->collection->deleteOne(array('_id' => $key)); |
255
|
32 |
|
$deletedCount = $result->getDeletedCount(); |
256
|
8 |
|
} catch (Exception $e) { |
257
|
8 |
|
throw WriteException::forException($e); |
258
|
|
|
} |
259
|
|
|
|
260
|
28 |
|
return $deletedCount > 0; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritdoc} |
265
|
|
|
*/ |
266
|
101 |
View Code Duplication |
public function exists($key) |
|
|
|
|
267
|
|
|
{ |
268
|
101 |
|
KeyUtil::validate($key); |
269
|
|
|
|
270
|
|
|
try { |
271
|
93 |
|
$count = $this->collection->count(array('_id' => $key)); |
272
|
4 |
|
} catch (Exception $e) { |
273
|
4 |
|
throw ReadException::forException($e); |
274
|
|
|
} |
275
|
|
|
|
276
|
89 |
|
return $count > 0; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
332 |
|
public function clear() |
283
|
|
|
{ |
284
|
|
|
try { |
285
|
332 |
|
$this->collection->drop(); |
286
|
4 |
|
} catch (Exception $e) { |
287
|
4 |
|
throw WriteException::forException($e); |
288
|
|
|
} |
289
|
332 |
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
16 |
|
public function keys() |
295
|
|
|
{ |
296
|
|
|
try { |
297
|
16 |
|
$cursor = $this->collection->find(array(), array( |
298
|
16 |
|
'projection' => array('_id' => 1), |
299
|
|
|
)); |
300
|
|
|
|
301
|
12 |
|
$keys = array(); |
302
|
|
|
|
303
|
12 |
|
foreach ($cursor as $document) { |
304
|
12 |
|
$keys[] = $document['_id']; |
305
|
|
|
} |
306
|
4 |
|
} catch (Exception $e) { |
307
|
4 |
|
throw ReadException::forException($e); |
308
|
|
|
} |
309
|
|
|
|
310
|
12 |
|
return $keys; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.