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
|
|
|
* MongoClient |
15
|
|
|
* |
16
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
17
|
|
|
*/ |
18
|
|
|
class MongoClient |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const VERSION = 'mangan-shim'; |
22
|
|
|
const DEFAULT_HOST = 'localhost'; |
23
|
|
|
const DEFAULT_PORT = 27017; |
24
|
|
|
const DEFAULT_DATABASE = 'admin'; |
25
|
|
|
const RP_PRIMARY = 'primary'; |
26
|
|
|
const RP_PRIMARY_PREFERRED = 'primaryPreferred'; |
27
|
|
|
const RP_SECONDARY = 'secondary'; |
28
|
|
|
const RP_SECONDARY_PREFERRED = 'secondaryPreferred'; |
29
|
|
|
const RP_NEAREST = 'nearest'; |
30
|
|
|
const RP_DEFAULT_ACCEPTABLE_LATENCY_MS = 15; |
31
|
|
|
const DEFAULT_CONNECT_TIMEOUT_MS = 60000; |
32
|
|
|
const REPL_SET_CACHE_LIFETIME = 15; // in seconds |
33
|
|
|
const STATE_STARTUP = 0; |
34
|
|
|
const STATE_PRIMARY = 1; |
35
|
|
|
const STATE_SECONDARY = 2; |
36
|
|
|
const STATE_STR_PRIMARY = 'PRIMARY'; |
37
|
|
|
const STATE_STR_SECONDARY = 'SECONDARY'; |
38
|
|
|
const HEALTHY = 1; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var boolean |
42
|
|
|
*/ |
43
|
|
|
public $connected = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $boolean = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $status; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var boolean |
57
|
|
|
*/ |
58
|
|
|
public $persistent; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private $options; |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $username; |
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $password; |
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private $database; |
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
private $uri; |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var array<string> |
87
|
|
|
*/ |
88
|
|
|
private $hosts = []; |
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var array<Protocol> |
92
|
|
|
*/ |
93
|
|
|
public $protocols = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var array<Socket> |
97
|
|
|
*/ |
98
|
|
|
private $sockets = []; |
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var array |
102
|
|
|
*/ |
103
|
|
|
private $databases = []; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var string |
107
|
|
|
*/ |
108
|
|
|
private $replSet; |
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var array |
112
|
|
|
*/ |
113
|
|
|
private $replSetStatus = []; |
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var array |
117
|
|
|
*/ |
118
|
|
|
private $replSetConf = []; |
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var array |
122
|
|
|
*/ |
123
|
|
|
private $readPreference = ['type' => self::RP_PRIMARY]; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @var int |
127
|
|
|
*/ |
128
|
|
|
private $connectTimeoutMS; |
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Holds a reference to the PRIMARY connection for replSet connections once it |
132
|
|
|
* has been established |
133
|
|
|
* |
134
|
|
|
* @var Mongofill\Protocol |
135
|
|
|
*/ |
136
|
|
|
private $primaryProtocol; |
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* @var Manager |
141
|
|
|
*/ |
142
|
|
|
private $manager; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Creates a new database connection object |
146
|
|
|
* |
147
|
|
|
* @param string $server - The server name. server should have the form: |
148
|
|
|
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db |
149
|
|
|
* @param array $options - An array of options for the connection. |
150
|
|
|
* @throws MongoConnectionException |
151
|
|
|
*/ |
152
|
|
|
public function __construct($server = 'mongodb://localhost:27017', array $options = ['connect' => true]) |
153
|
|
|
{ |
154
|
|
|
$this->manager = new Manager($server, $options); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets a database |
159
|
|
|
* |
160
|
|
|
* @param string $name - The database name. |
161
|
|
|
* |
162
|
|
|
* @return MongoDB - Returns a new database object. |
163
|
|
|
*/ |
164
|
|
|
public function selectDB($name) |
165
|
|
|
{ |
166
|
|
|
if (!isset($this->databases[$name])) |
167
|
|
|
{ |
168
|
|
|
$this->databases[$name] = new MongoDB($this, $name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->databases[$name]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the read preference for this connection |
176
|
|
|
* |
177
|
|
|
* @return array - |
178
|
|
|
*/ |
179
|
|
|
public function getReadPreference() |
180
|
|
|
{ |
181
|
|
|
return $this->readPreference; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.