|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Bridge; |
|
10
|
|
|
|
|
11
|
|
|
use MongoDB\Driver\Manager; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* MongoDB |
|
15
|
|
|
* |
|
16
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
17
|
|
|
*/ |
|
18
|
|
|
class MongoDB |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
const PROFILING_OFF = 0; |
|
22
|
|
|
const PROFILING_SLOW = 1; |
|
23
|
|
|
const PROFILING_ON = 2; |
|
24
|
|
|
const NAMESPACES_COLLECTION = 'system.namespaces'; |
|
25
|
|
|
const INDEX_COLLECTION = 'system.indexes'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
public $w = 1; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
public $w_timeout = 10000; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $name; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var MongoClient |
|
44
|
|
|
*/ |
|
45
|
|
|
private $client; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @var Manager |
|
50
|
|
|
*/ |
|
51
|
|
|
private $manager; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $collections = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private $readPreference; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Creates a new database |
|
65
|
|
|
* |
|
66
|
|
|
* @param MongoClient $client - Database connection. |
|
67
|
|
|
* @param string $name - Database name. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(MongoClient $client, $name) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->name = $name; |
|
72
|
|
|
$this->client = $client; |
|
73
|
|
|
$this->manager = new Manager(); |
|
74
|
|
|
$this->readPreference = $client->getReadPreference(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the read preference for this database |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getReadPreference() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->readPreference; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return MongoClient |
|
89
|
|
|
*/ |
|
90
|
|
|
public function _getClient() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->client; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function _getFullCollectionName($collectionName) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->name . '.' . $collectionName; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Gets a collection |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $name - The collection name. |
|
104
|
|
|
* |
|
105
|
|
|
* @return MongoCollection - Returns a new collection object. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function selectCollection($name) |
|
108
|
|
|
{ |
|
109
|
|
|
if (!isset($this->collections[$name])) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->collections[$name] = new MongoCollection($this, $name); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->collections[$name]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Execute a database command |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $command - The query to send. |
|
|
|
|
|
|
121
|
|
|
* @param array $options - This parameter is an associative array of |
|
122
|
|
|
* the form array("optionname" => boolean, ...). |
|
123
|
|
|
* |
|
124
|
|
|
* @return array - Returns database response. |
|
125
|
|
|
*/ |
|
126
|
|
|
public function command(array $cmd, array $options = []) |
|
127
|
|
|
{ |
|
128
|
|
|
$timeout = MongoCursor::$timeout; |
|
129
|
|
|
if (!empty($options['timeout'])) |
|
130
|
|
|
{ |
|
131
|
|
|
$timeout = $options['timeout']; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$protocol = empty($options['protocol']) ? $this->client->_getWriteProtocol() : $options['protocol']; |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$response = $protocol->opQuery( |
|
137
|
|
|
"{$this->name}.\$cmd", $cmd, 0, -1, 0, $timeout |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
return $response['result'][0]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Fetches toolkit for dealing with files stored in this database |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $prefix - The prefix for the files and chunks |
|
147
|
|
|
* collections. |
|
148
|
|
|
* |
|
149
|
|
|
* @return MongoGridFS - Returns a new gridfs object for this database. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getGridFS($prefix = 'fs') |
|
152
|
|
|
{ |
|
153
|
|
|
return new MongoGridFS($this, $prefix); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.