|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpConsole\Storage; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* MongoDB storage for postponed response data. |
|
7
|
|
|
* |
|
8
|
|
|
* @package PhpConsole |
|
9
|
|
|
* @version 3.1 |
|
10
|
|
|
* @link http://php-console.com |
|
11
|
|
|
* @author Sergey Barbushin http://linkedin.com/in/barbushin |
|
12
|
|
|
* @copyright © Sergey Barbushin, 2011-2013. All rights reserved. |
|
13
|
|
|
* @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License" |
|
14
|
|
|
* @codeCoverageIgnore |
|
15
|
|
|
*/ |
|
16
|
|
|
class MongoDB extends ExpiringKeyValue { |
|
17
|
|
|
|
|
18
|
|
|
/** @var \MongoClient */ |
|
19
|
|
|
protected $mongoClient; |
|
20
|
|
|
/** @var \MongoCollection */ |
|
21
|
|
|
protected $mongoCollection; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct($server = 'mongodb://localhost:27017', $db = 'phpconsole', $collection = 'phpconsole') { |
|
24
|
|
|
$this->mongoClient = new \MongoClient($server); |
|
25
|
|
|
if(!$this->mongoClient) { |
|
26
|
|
|
throw new \Exception('Unable to connect to MongoDB server'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->mongoCollection = $this->mongoClient->selectCollection($db, $collection); |
|
30
|
|
|
if(!$this->mongoCollection) { |
|
31
|
|
|
throw new \Exception('Unable to get collection'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (!in_array($collection, $this->mongoCollection->db->getCollectionNames())) { |
|
35
|
|
|
$this->mongoCollection->db->createCollection($collection); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->mongoCollection->ensureIndex(array( |
|
39
|
|
|
'expireAt' => 1, |
|
40
|
|
|
), array( |
|
41
|
|
|
'background' => true, |
|
42
|
|
|
'name' => 'TTL', |
|
43
|
|
|
'expireAfterSeconds' => 0, |
|
44
|
|
|
)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Save data by auto-expire key |
|
49
|
|
|
* @param $key |
|
50
|
|
|
* @param string $data |
|
51
|
|
|
* @param int $expire |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function set($key, $data, $expire) { |
|
54
|
|
|
$this->mongoCollection->update(array( |
|
55
|
|
|
'key' => $key |
|
56
|
|
|
), array( |
|
57
|
|
|
'key' => $key, |
|
58
|
|
|
'data' => $data, |
|
59
|
|
|
'expireAt' => new \MongoDate(time() + $expire) |
|
60
|
|
|
), array( |
|
61
|
|
|
'upsert' => true |
|
62
|
|
|
)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get data by key if not expired |
|
67
|
|
|
* @param $key |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function get($key) { |
|
71
|
|
|
$record = $this->mongoCollection->findOne(array('key' => $key)); |
|
72
|
|
|
if($record && is_array($record) && array_key_exists('data', $record)) { |
|
|
|
|
|
|
73
|
|
|
return $record['data']; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Remove key in store |
|
79
|
|
|
* @param $key |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function delete($key) { |
|
83
|
|
|
return $this->mongoCollection->remove(array('key' => $key)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.