1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MongoClient.php |
5
|
|
|
* |
6
|
|
|
* @author Dennis de Greef <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace Link0\Profiler\PersistenceHandler\MongoDbHandler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Extension of the MongoClient class to adhere to the interface we want to talk to |
12
|
|
|
* |
13
|
|
|
* @package Link0\Profiler\PersistenceHandler\MongoDbHandler |
14
|
|
|
*/ |
15
|
|
|
final class MongoClient implements MongoClientInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \MongoDB\Driver\Manager |
19
|
|
|
*/ |
20
|
|
|
private $driverManager; |
21
|
|
|
|
22
|
|
|
public function __construct($uri = 'mongodb://127.0.0.1', $uriOptions = [], $driverOptions = []) |
23
|
|
|
{ |
24
|
|
|
$this->driverManager = new \MongoDB\Driver\Manager($uri, $uriOptions, $driverOptions); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $namespace |
30
|
|
|
* @param array|object $filter |
31
|
|
|
* @param array $queryOptions |
32
|
|
|
* @return string[] |
33
|
|
|
* |
34
|
|
|
* @throws \MongoDB\Driver\Exception\Exception |
35
|
|
|
* @throws \MongoDB\Driver\Exception\AuthenticationException if authentication is needed and fails |
36
|
|
|
* @throws \MongoDB\Driver\Exception\ConnectionException if connection to the server fails for other then authentication reasons |
37
|
|
|
* @throws \MongoDB\Driver\Exception\RuntimeException on other errors (invalid command, command arguments, ...) |
38
|
|
|
*/ |
39
|
|
|
public function executeQuery($namespace, $filter, $queryOptions = array()) |
40
|
|
|
{ |
41
|
|
|
$query = new \MongoDB\Driver\Query($filter, $queryOptions); |
42
|
|
|
|
43
|
|
|
return iterator_to_array($this->driverManager->executeQuery($namespace, $query)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $namespace |
48
|
|
|
* @param array|object $document |
49
|
|
|
* @param array|null $options |
50
|
|
|
* @return bool Whether or not the write was acknowledged |
51
|
|
|
* |
52
|
|
|
* TODO: Any sort of feedback from insert was never actually processed before. |
|
|
|
|
53
|
|
|
* It would be prettier if it was somehow. |
54
|
|
|
* |
55
|
|
|
* TODO: The driver docs I have locally state a different set of possible exceptions than |
|
|
|
|
56
|
|
|
* the documentation on php.net - figuring out what is actually going to happen |
57
|
|
|
* would be preferred. |
58
|
|
|
* |
59
|
|
|
* @throws \MongoDB\Driver\Exception\Exception |
60
|
|
|
* @throws \MongoDB\Driver\Exception\AuthenticationException if authentication is needed and fails |
61
|
|
|
* @throws \MongoDB\Driver\Exception\ConnectionException if connection to the server fails for other then authentication reasons |
62
|
|
|
* @throws \MongoDB\Driver\Exception\InvalidArgumentException on argument parsing errors |
63
|
|
|
* @throws \MongoDB\Driver\Exception\BulkWriteException on any write failure (e.g. write error, failure to apply a write concern) |
64
|
|
|
* @throws \MongoDB\Driver\Exception\RuntimeException on other errors |
65
|
|
|
*/ |
66
|
|
|
public function insert($namespace, $document, $options = null) |
67
|
|
|
{ |
68
|
|
|
if ($options === null) { |
69
|
|
|
$options = array( |
70
|
|
|
'ordered' => false, |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$bulkWrite = new \MongoDB\Driver\BulkWrite($options); |
75
|
|
|
|
76
|
|
|
// NOTE: This function returns a \MongoDB\BSON\ObjectID if the document contained no |
77
|
|
|
// ID. This is currently not used anywhere, but the interface will need refactoring |
78
|
|
|
// if it is necessary in any place. |
79
|
|
|
$bulkWrite->insert($document); |
80
|
|
|
|
81
|
|
|
$writeResult = $this->driverManager->executeBulkWrite($namespace, $bulkWrite); |
82
|
|
|
|
83
|
|
|
return $writeResult->isAcknowledged(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|