1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Alcaeus\MongoDbAdapter\Helper; |
17
|
|
|
use MongoDB\Model\CollectionInfo; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instances of this class are used to interact with a database. |
21
|
|
|
* @link http://www.php.net/manual/en/class.mongodb.php |
22
|
|
|
*/ |
23
|
|
|
class MongoDB |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
use Helper\ReadPreference; |
26
|
|
|
use Helper\WriteConcern; |
27
|
|
|
|
28
|
|
|
const PROFILING_OFF = 0; |
29
|
|
|
const PROFILING_SLOW = 1; |
30
|
|
|
const PROFILING_ON = 2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \MongoDB\Database |
34
|
|
|
*/ |
35
|
|
|
protected $db; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates a new database |
39
|
|
|
* |
40
|
|
|
* This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {@see Mongo::__get()} or {@see Mongo::selectDB()}. |
41
|
|
|
* @link http://www.php.net/manual/en/mongodb.construct.php |
42
|
|
|
* @param MongoClient $conn Database connection. |
43
|
|
|
* @param string $name Database name. |
44
|
|
|
* @throws Exception |
45
|
|
|
* @return MongoDB Returns the database. |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function __construct($conn, $name) |
48
|
|
|
{ |
49
|
|
|
$this->connection = $conn; |
|
|
|
|
50
|
|
|
$this->name = $name; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->setReadPreferenceFromArray($conn->getReadPreference()); |
53
|
|
|
$this->setWriteConcernFromArray($conn->getWriteConcern()); |
54
|
|
|
|
55
|
|
|
$this->createDatabaseObject(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return \MongoDB\Database |
60
|
|
|
* @internal |
61
|
|
|
*/ |
62
|
|
|
public function getDb() |
63
|
|
|
{ |
64
|
|
|
return $this->db; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The name of this database |
69
|
|
|
* @link http://www.php.net/manual/en/mongodb.--tostring.php |
70
|
|
|
* @return string Returns this database's name. |
71
|
|
|
*/ |
72
|
|
|
public function __toString() |
73
|
|
|
{ |
74
|
|
|
return $this->name; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets a collection |
79
|
|
|
* |
80
|
|
|
* @link http://www.php.net/manual/en/mongodb.get.php |
81
|
|
|
* @param string $name The name of the collection. |
82
|
|
|
* @return MongoCollection |
83
|
|
|
*/ |
84
|
|
|
public function __get($name) |
85
|
|
|
{ |
86
|
|
|
// Handle w and wtimeout properties that replicate data stored in $readPreference |
87
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
88
|
|
|
return $this->getWriteConcern()[$name]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->selectCollection($name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
* @param mixed $value |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
101
|
|
|
$this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern()); |
102
|
|
|
$this->createDatabaseObject(); |
103
|
|
|
} else { |
104
|
|
|
$this->$name = $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* (PECL mongo >= 1.3.0)<br/> |
110
|
|
|
* @link http://www.php.net/manual/en/mongodb.getcollectionnames.php |
111
|
|
|
* Get all collections from this database |
112
|
|
|
* @return array Returns the names of the all the collections in the database as an |
113
|
|
|
* {@link http://www.php.net/manual/en/language.types.array.php array}. |
114
|
|
|
*/ |
115
|
|
|
public function getCollectionNames(array $options = []) |
116
|
|
|
{ |
117
|
|
|
if (is_bool($options)) { |
118
|
|
|
$options = ['includeSystemCollections' => $options]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$collections = $this->db->listCollections($options); |
122
|
|
|
|
123
|
|
|
$getCollectionName = function (CollectionInfo $collectionInfo) { |
124
|
|
|
return $collectionInfo->getName(); |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
return array_map($getCollectionName, (array) $collections); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return MongoClient |
132
|
|
|
* @internal This method is not part of the ext-mongo API |
133
|
|
|
*/ |
134
|
|
|
public function getConnection() |
135
|
|
|
{ |
136
|
|
|
return $this->connection; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
141
|
|
|
* Fetches toolkit for dealing with files stored in this database |
142
|
|
|
* @link http://www.php.net/manual/en/mongodb.getgridfs.php |
143
|
|
|
* @param string $prefix [optional] The prefix for the files and chunks collections. |
144
|
|
|
* @return MongoGridFS Returns a new gridfs object for this database. |
145
|
|
|
*/ |
146
|
|
|
public function getGridFS($prefix = "fs") |
147
|
|
|
{ |
148
|
|
|
return new \MongoGridFS($this, $prefix, $prefix); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
153
|
|
|
* Gets this database's profiling level |
154
|
|
|
* @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php |
155
|
|
|
* @return int Returns the profiling level. |
156
|
|
|
*/ |
157
|
|
|
public function getProfilingLevel() |
158
|
|
|
{ |
159
|
|
|
return static::PROFILING_OFF; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* (PECL mongo >= 1.1.0)<br/> |
164
|
|
|
* Get slaveOkay setting for this database |
165
|
|
|
* @link http://www.php.net/manual/en/mongodb.getslaveokay.php |
166
|
|
|
* @return bool Returns the value of slaveOkay for this instance. |
167
|
|
|
*/ |
168
|
|
|
public function getSlaveOkay() |
169
|
|
|
{ |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
175
|
|
|
* Sets this database's profiling level |
176
|
|
|
* @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php |
177
|
|
|
* @param int $level Profiling level. |
178
|
|
|
* @return int Returns the previous profiling level. |
179
|
|
|
*/ |
180
|
|
|
public function setProfilingLevel($level) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
return static::PROFILING_OFF; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
187
|
|
|
* Drops this database |
188
|
|
|
* @link http://www.php.net/manual/en/mongodb.drop.php |
189
|
|
|
* @return array Returns the database response. |
190
|
|
|
*/ |
191
|
|
|
public function drop() |
192
|
|
|
{ |
193
|
|
|
return $this->db->drop(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Repairs and compacts this database |
198
|
|
|
* @link http://www.php.net/manual/en/mongodb.repair.php |
199
|
|
|
* @param bool $preserve_cloned_files [optional] <p>If cloned files should be kept if the repair fails.</p> |
200
|
|
|
* @param bool $backup_original_files [optional] <p>If original files should be backed up.</p> |
201
|
|
|
* @return array <p>Returns db response.</p> |
202
|
|
|
*/ |
203
|
|
|
public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
return []; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
210
|
|
|
* Gets a collection |
211
|
|
|
* @link http://www.php.net/manual/en/mongodb.selectcollection.php |
212
|
|
|
* @param string $name <b>The collection name.</b> |
213
|
|
|
* @throws Exception if the collection name is invalid. |
214
|
|
|
* @return MongoCollection <p> |
215
|
|
|
* Returns a new collection object. |
216
|
|
|
* </p> |
217
|
|
|
*/ |
218
|
|
|
public function selectCollection($name) |
219
|
|
|
{ |
220
|
|
|
return new MongoCollection($this, $name); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* (PECL mongo >= 1.1.0)<br/> |
225
|
|
|
* Change slaveOkay setting for this database |
226
|
|
|
* @link http://php.net/manual/en/mongodb.setslaveokay.php |
227
|
|
|
* @param bool $ok [optional] <p> |
228
|
|
|
* If reads should be sent to secondary members of a replica set for all |
229
|
|
|
* possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance. |
230
|
|
|
* </p> |
231
|
|
|
* @return bool Returns the former value of slaveOkay for this instance. |
232
|
|
|
*/ |
233
|
|
|
public function setSlaveOkay ($ok = true) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Creates a collection |
240
|
|
|
* @link http://www.php.net/manual/en/mongodb.createcollection.php |
241
|
|
|
* @param string $name The name of the collection. |
242
|
|
|
* @param array $options [optional] <p> |
243
|
|
|
* <p> |
244
|
|
|
* An array containing options for the collections. Each option is its own |
245
|
|
|
* element in the options array, with the option name listed below being |
246
|
|
|
* the key of the element. The supported options depend on the MongoDB |
247
|
|
|
* server version. At the moment, the following options are supported: |
248
|
|
|
* </p> |
249
|
|
|
* <p> |
250
|
|
|
* <b>capped</b> |
251
|
|
|
* <p> |
252
|
|
|
* If the collection should be a fixed size. |
253
|
|
|
* </p> |
254
|
|
|
* </p> |
255
|
|
|
* <p> |
256
|
|
|
* <b>size</b> |
257
|
|
|
* <p> |
258
|
|
|
* If the collection is fixed size, its size in bytes.</p></p> |
259
|
|
|
* <p><b>max</b> |
260
|
|
|
* <p>If the collection is fixed size, the maximum number of elements to store in the collection.</p></p> |
261
|
|
|
* <i>autoIndexId</i> |
262
|
|
|
* |
263
|
|
|
* <p> |
264
|
|
|
* If capped is <b>TRUE</b> you can specify <b>FALSE</b> to disable the |
265
|
|
|
* automatic index created on the <em>_id</em> field. |
266
|
|
|
* Before MongoDB 2.2, the default value for |
267
|
|
|
* <em>autoIndexId</em> was <b>FALSE</b>. |
268
|
|
|
* </p> |
269
|
|
|
* </p> |
270
|
|
|
* @return MongoCollection <p>Returns a collection object representing the new collection.</p> |
271
|
|
|
*/ |
272
|
|
|
public function createCollection($name, $options) |
273
|
|
|
{ |
274
|
|
|
$this->db->createCollection($name, $options); |
275
|
|
|
return $this->selectCollection($name); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
280
|
|
|
* @deprecated Use MongoCollection::drop() instead. |
281
|
|
|
* Drops a collection |
282
|
|
|
* @link http://www.php.net/manual/en/mongodb.dropcollection.php |
283
|
|
|
* @param MongoCollection|string $coll MongoCollection or name of collection to drop. |
284
|
|
|
* @return array Returns the database response. |
285
|
|
|
*/ |
286
|
|
|
public function dropCollection($coll) |
287
|
|
|
{ |
288
|
|
|
return $this->db->dropCollection((string) $coll); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
293
|
|
|
* Get a list of collections in this database |
294
|
|
|
* @link http://www.php.net/manual/en/mongodb.listcollections.php |
295
|
|
|
* @param bool $includeSystemCollections [optional] <p>Include system collections.</p> |
|
|
|
|
296
|
|
|
* @return array Returns a list of MongoCollections. |
297
|
|
|
*/ |
298
|
|
|
public function listCollections(array $options = []) |
299
|
|
|
{ |
300
|
|
|
return array_map([$this, 'selectCollection'], $this->getCollectionNames($options)); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
305
|
|
|
* Creates a database reference |
306
|
|
|
* @link http://www.php.net/manual/en/mongodb.createdbref.php |
307
|
|
|
* @param string $collection The collection to which the database reference will point. |
308
|
|
|
* @param mixed $document_or_id <p> |
309
|
|
|
* If an array or object is given, its <em>_id</em> field will be |
310
|
|
|
* used as the reference ID. If a {@see MongoId} or scalar |
311
|
|
|
* is given, it will be used as the reference ID. |
312
|
|
|
* </p> |
313
|
|
|
* @return array <p>Returns a database reference array.</p> |
314
|
|
|
* <p> |
315
|
|
|
* If an array without an <em>_id</em> field was provided as the |
316
|
|
|
* <em>document_or_id</em> parameter, <b>NULL</b> will be returned. |
317
|
|
|
* </p> |
318
|
|
|
*/ |
319
|
|
|
public function createDBRef($collection, $document_or_id) |
320
|
|
|
{ |
321
|
|
|
if (is_object($document_or_id)) { |
322
|
|
|
$id = isset($document_or_id->_id) ? $document_or_id->_id : null; |
323
|
|
|
// $id = $document_or_id->_id ?? null; |
|
|
|
|
324
|
|
|
} elseif (is_array($document_or_id)) { |
325
|
|
|
if (! isset($document_or_id['_id'])) { |
326
|
|
|
return null; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$id = $document_or_id['_id']; |
330
|
|
|
} else { |
331
|
|
|
$id = $document_or_id; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return [ |
335
|
|
|
'$ref' => $collection, |
336
|
|
|
'$id' => $id, |
337
|
|
|
'$db' => $this->name, |
|
|
|
|
338
|
|
|
]; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
344
|
|
|
* Fetches the document pointed to by a database reference |
345
|
|
|
* @link http://www.php.net/manual/en/mongodb.getdbref.php |
346
|
|
|
* @param array $ref A database reference. |
347
|
|
|
* @return array Returns the document pointed to by the reference. |
348
|
|
|
*/ |
349
|
|
|
public function getDBRef(array $ref) |
|
|
|
|
350
|
|
|
{ |
351
|
|
|
$this->notImplemented(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* (PECL mongo >= 0.9.3)<br/> |
356
|
|
|
* Runs JavaScript code on the database server. |
357
|
|
|
* @link http://www.php.net/manual/en/mongodb.execute.php |
358
|
|
|
* @param MongoCode|string $code Code to execute. |
359
|
|
|
* @param array $args [optional] Arguments to be passed to code. |
360
|
|
|
* @return array Returns the result of the evaluation. |
361
|
|
|
*/ |
362
|
|
|
public function execute($code, array $args = array()) |
|
|
|
|
363
|
|
|
{ |
364
|
|
|
$this->notImplemented(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Execute a database command |
369
|
|
|
* @link http://www.php.net/manual/en/mongodb.command.php |
370
|
|
|
* @param array $data The query to send. |
371
|
|
|
* @param array() $options [optional] <p> |
|
|
|
|
372
|
|
|
* This parameter is an associative array of the form |
373
|
|
|
* <em>array("optionname" => <boolean>, ...)</em>. Currently |
374
|
|
|
* supported options are: |
375
|
|
|
* </p><ul> |
376
|
|
|
* <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li> |
377
|
|
|
* </ul> |
378
|
|
|
* @return array Returns database response. |
379
|
|
|
* Every database response is always maximum one document, |
380
|
|
|
* which means that the result of a database command can never exceed 16MB. |
381
|
|
|
* The resulting document's structure depends on the command, |
382
|
|
|
* but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents. |
383
|
|
|
*/ |
384
|
|
|
public function command(array $data, $options, &$hash) |
|
|
|
|
385
|
|
|
{ |
386
|
|
|
try { |
387
|
|
|
$cursor = new \MongoCommandCursor($this->connection, $this->name, $data); |
|
|
|
|
388
|
|
|
$cursor->setReadPreference($this->getReadPreference()); |
|
|
|
|
389
|
|
|
|
390
|
|
|
return iterator_to_array($cursor)[0]; |
391
|
|
|
} catch (\MongoDB\Driver\Exception\RuntimeException $e) { |
|
|
|
|
392
|
|
|
return [ |
393
|
|
|
'ok' => 0, |
394
|
|
|
'errmsg' => $e->getMessage(), |
395
|
|
|
'code' => $e->getCode(), |
396
|
|
|
]; |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
402
|
|
|
* Check if there was an error on the most recent db operation performed |
403
|
|
|
* @link http://www.php.net/manual/en/mongodb.lasterror.php |
404
|
|
|
* @return array Returns the error, if there was one. |
405
|
|
|
*/ |
406
|
|
|
public function lastError() |
407
|
|
|
{ |
408
|
|
|
$this->notImplemented(); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
413
|
|
|
* Checks for the last error thrown during a database operation |
414
|
|
|
* @link http://www.php.net/manual/en/mongodb.preverror.php |
415
|
|
|
* @return array Returns the error and the number of operations ago it occurred. |
416
|
|
|
*/ |
417
|
|
|
public function prevError() |
418
|
|
|
{ |
419
|
|
|
$this->notImplemented(); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
424
|
|
|
* Clears any flagged errors on the database |
425
|
|
|
* @link http://www.php.net/manual/en/mongodb.reseterror.php |
426
|
|
|
* @return array Returns the database response. |
427
|
|
|
*/ |
428
|
|
|
public function resetError() |
429
|
|
|
{ |
430
|
|
|
$this->notImplemented(); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
435
|
|
|
* Creates a database error |
436
|
|
|
* @link http://www.php.net/manual/en/mongodb.forceerror.php |
437
|
|
|
* @return boolean Returns the database response. |
438
|
|
|
*/ |
439
|
|
|
public function forceError() |
440
|
|
|
{ |
441
|
|
|
$this->notImplemented(); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* (PECL mongo >= 1.0.1)<br/> |
446
|
|
|
* Log in to this database |
447
|
|
|
* @link http://www.php.net/manual/en/mongodb.authenticate.php |
448
|
|
|
* @param string $username The username. |
449
|
|
|
* @param string $password The password (in plaintext). |
450
|
|
|
* @return array <p>Returns database response. If the login was successful, it will return 1.</p> |
451
|
|
|
* <p> |
452
|
|
|
* <span style="color: #0000BB"><?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?></span> |
453
|
|
|
* </span> |
454
|
|
|
* </code></div> |
455
|
|
|
* </div> |
456
|
|
|
* </p> |
457
|
|
|
* <p> If something went wrong, it will return </p> |
458
|
|
|
* <p> |
459
|
|
|
* <div class="example-contents"> |
460
|
|
|
* <div class="phpcode"><code><span style="color: #000000"> |
461
|
|
|
* <span style="color: #0000BB"><?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok" </span><span style="color: #007700">=> </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">"errmsg" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"auth fails"</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?></span></p> |
462
|
|
|
* <p>("auth fails" could be another message, depending on database version and |
463
|
|
|
* what went wrong)</p> |
464
|
|
|
*/ |
465
|
|
|
public function authenticate($username, $password) |
|
|
|
|
466
|
|
|
{ |
467
|
|
|
$this->notImplemented(); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* {@inheritdoc} |
472
|
|
|
*/ |
473
|
|
|
public function setReadPreference($readPreference, $tags = null) |
474
|
|
|
{ |
475
|
|
|
$result = $this->setReadPreferenceFromParameters($readPreference, $tags); |
476
|
|
|
$this->createDatabaseObject(); |
477
|
|
|
|
478
|
|
|
return $result; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* {@inheritdoc} |
483
|
|
|
*/ |
484
|
|
|
public function setWriteConcern($wstring, $wtimeout = 0) |
485
|
|
|
{ |
486
|
|
|
$result = $this->setWriteConcernFromParameters($wstring, $wtimeout); |
487
|
|
|
$this->createDatabaseObject(); |
488
|
|
|
|
489
|
|
|
return $result; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
protected function notImplemented() |
493
|
|
|
{ |
494
|
|
|
throw new \Exception('Not implemented'); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* @return \MongoDB\Database |
499
|
|
|
*/ |
500
|
|
View Code Duplication |
private function createDatabaseObject() |
|
|
|
|
501
|
|
|
{ |
502
|
|
|
$options = [ |
503
|
|
|
'readPreference' => $this->readPreference, |
504
|
|
|
'writeConcern' => $this->writeConcern, |
505
|
|
|
]; |
506
|
|
|
|
507
|
|
|
if ($this->db === null) { |
508
|
|
|
$this->db = $this->connection->getClient()->selectDatabase($this->name, $options); |
|
|
|
|
509
|
|
|
} else { |
510
|
|
|
$this->db = $this->db->withOptions($options); |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.