@@ -14,7 +14,7 @@ function mo_delete($id) {
14
14
$opts = array("http" =>
15
15
array(
16
16
"method" => "DELETE",
17
- "timeout" => 30 ,
17
+ "timeout" => 60 ,
18
18
"header" => "Accept: application/json\r\n" .
19
19
"Content-type: application/x-www-form-urlencoded",
20
20
"ignore_errors" => true,
@@ -32,7 +32,7 @@ function mo_post($url, $body) {
32
32
$opts = array("http" =>
33
33
array(
34
34
"method" => "POST",
35
- "timeout" => 30 ,
35
+ "timeout" => 60 ,
36
36
"header" => "Accept: application/json\r\n" .
37
37
"Content-type: application/x-www-form-urlencoded",
38
38
"content" => json_encode($body),
@@ -151,6 +151,9 @@ function phase($phase, &$output) {
151
151
if (isset ($ phase ["MOOperation " ])) {
152
152
return MOOperation ($ phase ["MOOperation " ], $ output );
153
153
}
154
+ if (isset ($ phase ["clientHosts " ])) {
155
+ return clientHosts ($ phase ["clientHosts " ], $ output );
156
+ }
154
157
if (isset ($ phase ["clientOperation " ])) {
155
158
return clientOperation ($ phase ["clientOperation " ], $ output );
156
159
}
@@ -166,11 +169,62 @@ function MOOperation($phase, &$output) {
166
169
case "post " :
167
170
return 'mo_post(" ' . $ phase ["uri " ] . '", ' . var_export ($ phase ["payload " ], true ) . '); ' ;
168
171
172
+ case "delete " :
173
+ return 'mo_delete(" ' . $ phase ["uri " ] . '"); ' ;
174
+
169
175
default :
170
- throw UnexpectedValueException ("Don't know the method $ method " );
176
+ throw new UnexpectedValueException ("Don't know the method $ method " );
171
177
}
172
178
}
173
179
180
+ function clientHosts ($ hosts , &$ output ) {
181
+ $ output = array ();
182
+
183
+ $ retval = <<< CODE
184
+ \$clientHosts = array();
185
+
186
+ CODE ;
187
+
188
+ if (!empty ($ hosts ['primary ' ])) {
189
+ $ primary = var_export ($ hosts ['primary ' ], true );
190
+
191
+ $ retval .= <<< CODE
192
+ \$found = array_filter( \$manager->getServers(), function( \$server) {
193
+ return \$server->getHost() == $ primary && \$server->getType() == MongoDB\\Driver\\SERVERTYPE_RS_PRIMARY;
194
+ });
195
+ if (count( \$found) == 1) {
196
+ \$clientHosts['primary'] = $ primary;
197
+ }
198
+
199
+ CODE ;
200
+ $ output ['primary ' ] = $ hosts ['primary ' ];
201
+ }
202
+
203
+ if (!empty ($ hosts ['secondaries ' ])) {
204
+ foreach ($ hosts ['secondaries ' ] as $ secondaryHost ) {
205
+ $ secondary = var_export ($ secondaryHost , true );
206
+
207
+ $ retval .= <<< CODE
208
+
209
+ \$found = array_filter( \$manager->getServers(), function( \$server) {
210
+ return \$server->getHost() == $ secondary && \$server->getType() == MongoDB\\Driver\\SERVERTYPE_RS_SECONDARY;
211
+ });
212
+ if (count( \$found) == 1) {
213
+ \$clientHosts['secondaries'][] = $ secondary;
214
+ }
215
+
216
+ CODE ;
217
+ $ output ['secondaries ' ][] = $ secondaryHost ;
218
+ }
219
+ }
220
+
221
+ $ retval .= <<< CODE
222
+ var_dump( \$clientHosts);
223
+ CODE ;
224
+
225
+ return $ retval ;
226
+ }
227
+
174
228
function clientOperation ($ phase , &$ output ) {
175
229
switch ($ phase ["operation " ]) {
176
230
case "insertOne " :
@@ -201,7 +255,7 @@ function clientOperation($phase, &$output) {
201
255
return $ retval ;
202
256
break ;
203
257
204
- case "findOne " :
258
+ case "find " :
205
259
$ output = $ phase ["outcome " ];
206
260
if (!$ output ["ok " ]) {
207
261
$ output ["errmsg " ] = "%s " ;
@@ -210,11 +264,7 @@ function clientOperation($phase, &$output) {
210
264
try {
211
265
\$query = new MongoDB\\Driver\\Query(array());
212
266
\$result = \$manager->executeQuery("databaseName.collectionName", \$query)->toArray();
213
- if ( \$result) {
214
- var_dump(array("ok" => 1));
215
- } else {
216
- var_dump(array("ok" => 0, "errmsg" => "Empty result"));
217
- }
267
+ var_dump(array("ok" => 1));
218
268
} catch(Exception \$e) {
219
269
var_dump(array("ok" => 0, "errmsg" => get_class( \$e) . ": " . \$e->getMessage()));
220
270
}
0 commit comments