1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @see https://github.com/laminas/laminas-cache for the canonical source repository
* @copyright https://github.com/laminas/laminas-cache/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-cache/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Cache\Storage\Adapter;
use Laminas\Cache\Exception;
use MongoCollection;
use MongoException;
class MongoDbResourceManager
{
/**
* Registered resources
*
* @var array[]
*/
private $resources = [];
/**
* Check if a resource exists
*
* @param string $id
*
* @return bool
*/
public function hasResource($id)
{
return isset($this->resources[$id]);
}
/**
* Set a resource
*
* @param string $id
* @param array|MongoCollection $resource
*
* @return MongoDbResourceManager Provides a fluent interface
*
* @throws Exception\RuntimeException
*/
public function setResource($id, $resource)
{
if ($resource instanceof MongoCollection) {
$this->resources[$id] = [
'db' => (string) $resource->db,
'db_instance' => $resource->db,
'collection' => (string) $resource,
'collection_instance' => $resource,
];
return $this;
}
if (! is_array($resource)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or MongoCollection; received %s',
__METHOD__,
(is_object($resource) ? get_class($resource) : gettype($resource))
));
}
$this->resources[$id] = $resource;
return $this;
}
/**
* Instantiate and return the MongoCollection resource
*
* @param string $id
* @return MongoCollection
* @throws Exception\RuntimeException
*/
public function getResource($id)
{
if (! $this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}
$resource = $this->resources[$id];
if (! isset($resource['collection_instance'])) {
try {
if (! isset($resource['db_instance'])) {
if (! isset($resource['client_instance'])) {
$clientClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient';
$resource['client_instance'] = new $clientClass(
isset($resource['server']) ? $resource['server'] : null,
isset($resource['connection_options']) ? $resource['connection_options'] : [],
isset($resource['driver_options']) ? $resource['driver_options'] : []
);
}
$resource['db_instance'] = $resource['client_instance']->selectDB(
isset($resource['db']) ? $resource['db'] : ''
);
}
$collection = $resource['db_instance']->selectCollection(
isset($resource['collection']) ? $resource['collection'] : ''
);
$collection->ensureIndex(['key' => 1]);
$this->resources[$id]['collection_instance'] = $collection;
} catch (MongoException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
return $this->resources[$id]['collection_instance'];
}
public function setServer($id, $server)
{
$this->resources[$id]['server'] = (string)$server;
unset($this->resources[$id]['client_instance']);
unset($this->resources[$id]['db_instance']);
unset($this->resources[$id]['collection_instance']);
}
public function getServer($id)
{
if (! $this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}
return isset($this->resources[$id]['server']) ? $this->resources[$id]['server'] : null;
}
public function setConnectionOptions($id, array $connectionOptions)
{
$this->resources[$id]['connection_options'] = $connectionOptions;
unset($this->resources[$id]['client_instance']);
unset($this->resources[$id]['db_instance']);
unset($this->resources[$id]['collection_instance']);
}
public function getConnectionOptions($id)
{
if (! $this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}
return isset($this->resources[$id]['connection_options'])
? $this->resources[$id]['connection_options']
: [];
}
public function setDriverOptions($id, array $driverOptions)
{
$this->resources[$id]['driver_options'] = $driverOptions;
unset($this->resources[$id]['client_instance']);
unset($this->resources[$id]['db_instance']);
unset($this->resources[$id]['collection_instance']);
}
public function getDriverOptions($id)
{
if (! $this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}
return isset($this->resources[$id]['driver_options']) ? $this->resources[$id]['driver_options'] : [];
}
public function setDatabase($id, $database)
{
$this->resources[$id]['db'] = (string)$database;
unset($this->resources[$id]['db_instance']);
unset($this->resources[$id]['collection_instance']);
}
public function getDatabase($id)
{
if (! $this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}
return isset($this->resources[$id]['db']) ? $this->resources[$id]['db'] : '';
}
public function setCollection($id, $collection)
{
$this->resources[$id]['collection'] = (string)$collection;
unset($this->resources[$id]['collection_instance']);
}
public function getCollection($id)
{
if (! $this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}
return isset($this->resources[$id]['collection']) ? $this->resources[$id]['collection'] : '';
}
}