|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage\Driver\MongoDB; |
|
4
|
|
|
|
|
5
|
|
|
use MongoDB; |
|
6
|
|
|
|
|
7
|
|
|
final class ConnectionOptions |
|
8
|
|
|
{ |
|
9
|
|
|
private const READ_PREFERENCE = [ |
|
10
|
|
|
MongoDB\Driver\ReadPreference::RP_PRIMARY => 'primary', |
|
11
|
|
|
MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED => 'primaryPreferred', |
|
12
|
|
|
MongoDB\Driver\ReadPreference::RP_SECONDARY => 'secondary', |
|
13
|
|
|
MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED => 'secondaryPreferred', |
|
14
|
|
|
MongoDB\Driver\ReadPreference::RP_NEAREST => 'nearest', |
|
15
|
|
|
]; |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $username; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private $password; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $database; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
private $connectTimeout; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
private $socketTimeout; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $appName; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $replicaSet; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $authMechanism; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array */ |
|
41
|
|
|
private $authOptions; |
|
42
|
|
|
|
|
43
|
|
|
/** @var MongoDB\Driver\ReadConcern */ |
|
44
|
|
|
private $readConcern; |
|
45
|
|
|
|
|
46
|
|
|
/** @var MongoDB\Driver\WriteConcern */ |
|
47
|
|
|
private $writeConcern; |
|
48
|
|
|
|
|
49
|
|
|
/** @var MongoDB\Driver\ReadPreference */ |
|
50
|
|
|
private $readPreference; |
|
51
|
|
|
|
|
52
|
|
|
/** @var string */ |
|
53
|
|
|
private $sslPemFile; |
|
54
|
|
|
|
|
55
|
|
|
/** @var string */ |
|
56
|
|
|
private $sslPemPassword; |
|
57
|
|
|
|
|
58
|
|
|
/** @var resource */ |
|
59
|
|
|
private $sslContext; |
|
60
|
|
|
|
|
61
|
49 |
|
public function __construct(string $database, string $username = null, string $password = null) |
|
62
|
|
|
{ |
|
63
|
49 |
|
$this->username = $username; |
|
64
|
49 |
|
$this->password = $password; |
|
65
|
49 |
|
$this->database = $database; |
|
66
|
49 |
|
$this->readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY); |
|
67
|
49 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function setReadConcern(MongoDB\Driver\ReadConcern $concern): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->readConcern = $concern; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function setWriteConcern(MongoDB\Driver\WriteConcern $concern): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->writeConcern = $concern; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function setReadPreference(MongoDB\Driver\ReadPreference $preference): void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->readPreference = $preference; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function setAppName(string $appName): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->appName = $appName; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Specifies non default authentication mechanism |
|
91
|
|
|
* @param string $mechanism |
|
92
|
|
|
* @param array $options |
|
93
|
|
|
* @see https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst#auth-related-options |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setAuth(string $mechanism, array $options = []): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->authMechanism = $mechanism; |
|
98
|
|
|
$this->authOptions = $options; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setConnectionTimeout(int $milliseconds): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->connectTimeout = $milliseconds; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setSocketTimeout(int $milliseconds): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->socketTimeout = $milliseconds; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setReplicaSet(string $name): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->replicaSet = $name; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function useSSL(string $pemFile, string $pemPassword = null, array $context = null): void |
|
117
|
|
|
{ |
|
118
|
|
|
if ($context !== null) { |
|
119
|
|
|
$this->sslContext = stream_context_create([ |
|
120
|
|
|
'ssl' => $context |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->sslPemFile = $pemFile; |
|
125
|
|
|
$this->sslPemPassword = $pemPassword; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
49 |
|
public function getURIOptions(): array |
|
129
|
|
|
{ |
|
130
|
49 |
|
$options = []; |
|
131
|
49 |
|
if ($this->username !== null) { |
|
132
|
49 |
|
$options['username'] = $this->username; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
49 |
|
if ($this->password !== null) { |
|
136
|
49 |
|
$options['password'] = $this->password; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
49 |
|
if ($this->appName !== null) { |
|
140
|
|
|
$options['appname'] = $this->appName; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
49 |
|
if ($this->authMechanism !== null) { |
|
144
|
|
|
$options['authMechanism'] = $this->authMechanism; |
|
145
|
|
|
if ($this->authOptions !== null) { |
|
146
|
|
|
$options['authMechanismProperties'] = $this->authOptions; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
49 |
|
if ($this->connectTimeout !== null) { |
|
151
|
|
|
$options['connectTimeoutMS'] = $this->connectTimeout; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
49 |
|
if ($this->socketTimeout !== null) { |
|
155
|
|
|
$options['socketTimeoutMS'] = $this->socketTimeout; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
49 |
|
if ($this->replicaSet !== null) { |
|
159
|
|
|
$options['replicaSet'] = $this->replicaSet; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
49 |
|
if ($this->readConcern !== null) { |
|
163
|
|
|
$options['readConcernLevel'] = $this->readConcern->getLevel(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
49 |
|
if ($this->readPreference !== null) { |
|
167
|
49 |
|
if (method_exists($this->readPreference, 'getMaxStalenessSeconds')) { |
|
168
|
49 |
|
$options['maxStalenessSeconds'] = $this->readPreference->getMaxStalenessSeconds(); |
|
169
|
|
|
} |
|
170
|
49 |
|
$options['readPreference'] = self::READ_PREFERENCE[$this->readPreference->getMode()]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
49 |
|
return $options; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
49 |
|
public function getDriverOptions(): array |
|
177
|
|
|
{ |
|
178
|
49 |
|
if (!$this->sslPemFile) { |
|
179
|
49 |
|
return []; |
|
180
|
|
|
} |
|
181
|
|
|
$options = [ |
|
182
|
|
|
'pem_file' => $this->sslPemFile, |
|
183
|
|
|
]; |
|
184
|
|
|
|
|
185
|
|
|
if ($this->sslPemPassword) { |
|
186
|
|
|
$options['pem_pwd'] = $this->sslPemPassword; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if ($this->sslContext) { |
|
190
|
|
|
$options['context'] = $this->sslContext; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $options; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
49 |
|
public function getDatabase(): string |
|
197
|
|
|
{ |
|
198
|
49 |
|
return $this->database; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|