@@ -14,7 +14,7 @@ function mo_delete($id) {
1414 $opts = array("http" =>
1515 array(
1616 "method" => "DELETE",
17- "timeout" => 30 ,
17+ "timeout" => 60 ,
1818 "header" => "Accept: application/json\r\n" .
1919 "Content-type: application/x-www-form-urlencoded",
2020 "ignore_errors" => true,
@@ -32,7 +32,7 @@ function mo_post($url, $body) {
3232 $opts = array("http" =>
3333 array(
3434 "method" => "POST",
35- "timeout" => 30 ,
35+ "timeout" => 60 ,
3636 "header" => "Accept: application/json\r\n" .
3737 "Content-type: application/x-www-form-urlencoded",
3838 "content" => json_encode($body),
@@ -151,6 +151,9 @@ function phase($phase, &$output) {
151151 if (isset ($ phase ["MOOperation " ])) {
152152 return MOOperation ($ phase ["MOOperation " ], $ output );
153153 }
154+ if (isset ($ phase ["clientHosts " ])) {
155+ return clientHosts ($ phase ["clientHosts " ], $ output );
156+ }
154157 if (isset ($ phase ["clientOperation " ])) {
155158 return clientOperation ($ phase ["clientOperation " ], $ output );
156159 }
@@ -166,11 +169,62 @@ function MOOperation($phase, &$output) {
166169 case "post " :
167170 return 'mo_post(" ' . $ phase ["uri " ] . '", ' . var_export ($ phase ["payload " ], true ) . '); ' ;
168171
172+ case "delete " :
173+ return 'mo_delete(" ' . $ phase ["uri " ] . '"); ' ;
174+
169175 default :
170- throw UnexpectedValueException ("Don't know the method $ method " );
176+ throw new UnexpectedValueException ("Don't know the method $ method " );
171177 }
172178}
173179
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+
174228function clientOperation ($ phase , &$ output ) {
175229 switch ($ phase ["operation " ]) {
176230 case "insertOne " :
@@ -201,7 +255,7 @@ function clientOperation($phase, &$output) {
201255 return $ retval ;
202256 break ;
203257
204- case "findOne " :
258+ case "find " :
205259 $ output = $ phase ["outcome " ];
206260 if (!$ output ["ok " ]) {
207261 $ output ["errmsg " ] = "%s " ;
@@ -210,11 +264,7 @@ function clientOperation($phase, &$output) {
210264try {
211265 \$query = new MongoDB\\Driver\\Query(array());
212266 \$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));
218268} catch(Exception \$e) {
219269 var_dump(array("ok" => 0, "errmsg" => get_class( \$e) . ": " . \$e->getMessage()));
220270}
0 commit comments