@@ -81,6 +81,19 @@ ZEND_DECLARE_MODULE_GLOBALS(mongodb)
81
81
#endif
82
82
#endif
83
83
84
+ php_phongo_server_description_type_map_t
85
+ php_phongo_server_description_type_map [PHONGO_SERVER_DESCRIPTION_TYPES ] = {
86
+ { PHONGO_SERVER_UNKNOWN , "Unknown" },
87
+ { PHONGO_SERVER_STANDALONE , "Standalone" },
88
+ { PHONGO_SERVER_MONGOS , "Mongos" },
89
+ { PHONGO_SERVER_POSSIBLE_PRIMARY , "PossiblePrimary" },
90
+ { PHONGO_SERVER_RS_PRIMARY , "RSPrimary" },
91
+ { PHONGO_SERVER_RS_SECONDARY , "RSSecondary" },
92
+ { PHONGO_SERVER_RS_ARBITER , "RSArbiter" },
93
+ { PHONGO_SERVER_RS_OTHER , "RSOther" },
94
+ { PHONGO_SERVER_RS_GHOST , "RSGhost" },
95
+ };
96
+
84
97
/* {{{ phongo_std_object_handlers */
85
98
zend_object_handlers phongo_std_object_handlers ;
86
99
@@ -1275,55 +1288,71 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
1275
1288
bson_oid_to_string (oid , intern -> oid );
1276
1289
} /* }}} */
1277
1290
1278
- void php_phongo_server_to_zval ( zval * retval , const mongoc_server_description_t * sd ) /* {{{ */
1291
+ php_phongo_server_description_type_t php_phongo_server_description_type ( mongoc_server_description_t * sd )
1279
1292
{
1280
- array_init (retval );
1281
-
1282
- ADD_ASSOC_STRING (retval , "host" , (char * )sd -> host .host );
1283
- ADD_ASSOC_LONG_EX (retval , "port" , sd -> host .port );
1284
- ADD_ASSOC_LONG_EX (retval , "type" , sd -> type );
1285
- ADD_ASSOC_BOOL_EX (retval , "is_primary" , sd -> type == MONGOC_SERVER_RS_PRIMARY );
1286
- ADD_ASSOC_BOOL_EX (retval , "is_secondary" , sd -> type == MONGOC_SERVER_RS_SECONDARY );
1287
- ADD_ASSOC_BOOL_EX (retval , "is_arbiter" , sd -> type == MONGOC_SERVER_RS_ARBITER );
1288
- {
1289
- bson_iter_t iter ;
1290
- zend_bool b = bson_iter_init_find_case (& iter , & sd -> last_is_master , "hidden" ) && bson_iter_as_bool (& iter );
1293
+ const char * name = mongoc_server_description_type (sd );
1294
+ int i ;
1291
1295
1292
- ADD_ASSOC_BOOL_EX (retval , "is_hidden" , b );
1296
+ for (i = 0 ; i < PHONGO_SERVER_DESCRIPTION_TYPES ; i ++ ) {
1297
+ if (!strcmp (name , php_phongo_server_description_type_map [i ].name )) {
1298
+ return php_phongo_server_description_type_map [i ].type ;
1299
+ }
1293
1300
}
1294
- {
1295
- bson_iter_t iter ;
1296
- zend_bool b = bson_iter_init_find_case (& iter , & sd -> last_is_master , "passive" ) && bson_iter_as_bool (& iter );
1297
1301
1298
- ADD_ASSOC_BOOL_EX (retval , "is_passive" , b );
1299
- }
1300
- if (sd -> tags .len ) {
1302
+ return PHONGO_SERVER_UNKNOWN ;
1303
+ }
1304
+
1305
+ void php_phongo_server_to_zval (zval * retval , mongoc_server_description_t * sd ) /* {{{ */
1306
+ {
1307
+ mongoc_host_list_t * host = mongoc_server_description_host (sd );
1308
+ const bson_t * is_master = mongoc_server_description_ismaster (sd );
1309
+ bson_iter_t iter ;
1310
+
1311
+ array_init (retval );
1312
+
1313
+ ADD_ASSOC_STRING (retval , "host" , host -> host );
1314
+ ADD_ASSOC_LONG_EX (retval , "port" , host -> port );
1315
+ ADD_ASSOC_LONG_EX (retval , "type" , php_phongo_server_description_type (sd ));
1316
+ ADD_ASSOC_BOOL_EX (retval , "is_primary" , !strcmp (mongoc_server_description_type (sd ), php_phongo_server_description_type_map [PHONGO_SERVER_RS_PRIMARY ].name ));
1317
+ ADD_ASSOC_BOOL_EX (retval , "is_secondary" , !strcmp (mongoc_server_description_type (sd ), php_phongo_server_description_type_map [PHONGO_SERVER_RS_SECONDARY ].name ));
1318
+ ADD_ASSOC_BOOL_EX (retval , "is_arbiter" , !strcmp (mongoc_server_description_type (sd ), php_phongo_server_description_type_map [PHONGO_SERVER_RS_ARBITER ].name ));
1319
+ ADD_ASSOC_BOOL_EX (retval , "is_hidden" , bson_iter_init_find_case (& iter , is_master , "hidden" ) && bson_iter_as_bool (& iter ));
1320
+ ADD_ASSOC_BOOL_EX (retval , "is_passive" , bson_iter_init_find_case (& iter , is_master , "passive" ) && bson_iter_as_bool (& iter ));
1321
+
1322
+ if (bson_iter_init_find (& iter , is_master , "tags" ) && BSON_ITER_HOLDS_DOCUMENT (& iter )) {
1323
+ const uint8_t * bytes ;
1324
+ uint32_t len ;
1301
1325
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
1326
+
1302
1327
/* Use native arrays for debugging output */
1303
1328
state .map .root_type = PHONGO_TYPEMAP_NATIVE_ARRAY ;
1304
1329
state .map .document_type = PHONGO_TYPEMAP_NATIVE_ARRAY ;
1305
1330
1306
- phongo_bson_to_zval_ex (bson_get_data (& sd -> tags ), sd -> tags .len , & state );
1331
+ bson_iter_document (& iter , & len , & bytes );
1332
+ phongo_bson_to_zval_ex (bytes , len , & state );
1333
+
1307
1334
#if PHP_VERSION_ID >= 70000
1308
1335
ADD_ASSOC_ZVAL_EX (retval , "tags" , & state .zchild );
1309
1336
#else
1310
1337
ADD_ASSOC_ZVAL_EX (retval , "tags" , state .zchild );
1311
1338
#endif
1312
1339
}
1340
+
1313
1341
{
1314
1342
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
1315
1343
/* Use native arrays for debugging output */
1316
1344
state .map .root_type = PHONGO_TYPEMAP_NATIVE_ARRAY ;
1317
1345
state .map .document_type = PHONGO_TYPEMAP_NATIVE_ARRAY ;
1318
1346
1319
- phongo_bson_to_zval_ex (bson_get_data (& sd -> last_is_master ), sd -> last_is_master .len , & state );
1347
+ phongo_bson_to_zval_ex (bson_get_data (is_master ), is_master -> len , & state );
1348
+
1320
1349
#if PHP_VERSION_ID >= 70000
1321
1350
ADD_ASSOC_ZVAL_EX (retval , "last_is_master" , & state .zchild );
1322
1351
#else
1323
1352
ADD_ASSOC_ZVAL_EX (retval , "last_is_master" , state .zchild );
1324
1353
#endif
1325
1354
}
1326
- ADD_ASSOC_LONG_EX (retval , "round_trip_time" , sd -> round_trip_time );
1355
+ ADD_ASSOC_LONG_EX (retval , "round_trip_time" , ( phongo_long ) mongoc_server_description_round_trip_time ( sd ) );
1327
1356
1328
1357
} /* }}} */
1329
1358
0 commit comments