|
29 | 29 | /* External libs */
|
30 | 30 | #include <bson.h>
|
31 | 31 | #include <mongoc.h>
|
| 32 | +#include <mongoc-client-private.h> |
| 33 | +#include <mongoc-host-list-private.h> |
32 | 34 |
|
33 | 35 | /* PHP Core stuff */
|
34 | 36 | #include <php.h>
|
35 | 37 | #include <php_ini.h>
|
36 | 38 | #include <ext/standard/info.h>
|
37 | 39 | #include <ext/standard/file.h>
|
38 | 40 | #include <Zend/zend_interfaces.h>
|
| 41 | +#include <Zend/zend_hash.h> |
39 | 42 | #include <ext/spl/spl_iterators.h>
|
40 | 43 | /* Our Compatability header */
|
41 | 44 | #include "php_compat_53.h"
|
|
48 | 51 |
|
49 | 52 | PHONGO_API zend_class_entry *php_phongo_manager_ce;
|
50 | 53 |
|
| 54 | +zend_object_handlers php_phongo_handler_manager; |
| 55 | + |
51 | 56 | /* {{{ proto MongoDB\Driver\Manager Manager::__construct(string $uri[, array $options = array()[, array $driverOptions = array()]])
|
52 | 57 | Constructs a new Manager */
|
53 | 58 | PHP_METHOD(Manager, __construct)
|
@@ -411,32 +416,158 @@ static void php_phongo_manager_free_object(void *object TSRMLS_DC) /* {{{ */
|
411 | 416 | zend_object_value php_phongo_manager_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
|
412 | 417 | {
|
413 | 418 | zend_object_value retval;
|
414 |
| - php_phongo_manager_t *intern; |
| 419 | + php_phongo_manager_t *intern = NULL; |
415 | 420 |
|
416 |
| - intern = (php_phongo_manager_t *)emalloc(sizeof(php_phongo_manager_t)); |
417 |
| - memset(intern, 0, sizeof(php_phongo_manager_t)); |
| 421 | + intern = (php_phongo_manager_t *)ecalloc(1, sizeof *intern); |
418 | 422 |
|
419 | 423 | zend_object_std_init(&intern->std, class_type TSRMLS_CC);
|
420 | 424 | object_properties_init(&intern->std, class_type);
|
421 | 425 |
|
422 | 426 | retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_manager_free_object, NULL TSRMLS_CC);
|
423 |
| - retval.handlers = phongo_get_std_object_handlers(); |
| 427 | + retval.handlers = &php_phongo_handler_manager; |
424 | 428 |
|
425 | 429 | return retval;
|
426 | 430 | } /* }}} */
|
| 431 | + |
| 432 | +static const char *phongo_cluster_mode_tostring(mongoc_cluster_mode_t mode) |
| 433 | +{ |
| 434 | + switch (mode) { |
| 435 | + case MONGOC_CLUSTER_DIRECT: |
| 436 | + return "direct"; |
| 437 | + case MONGOC_CLUSTER_REPLICA_SET: |
| 438 | + return "replicaset"; |
| 439 | + case MONGOC_CLUSTER_SHARDED_CLUSTER: |
| 440 | + return "sharded"; |
| 441 | + break; |
| 442 | + } |
| 443 | + |
| 444 | + return "broken"; |
| 445 | +} |
| 446 | + |
| 447 | +static const char *phongo_cluster_state_tostring(mongoc_cluster_state_t state) |
| 448 | +{ |
| 449 | + switch (state) { |
| 450 | + case MONGOC_CLUSTER_STATE_BORN: |
| 451 | + return "born"; |
| 452 | + case MONGOC_CLUSTER_STATE_HEALTHY: |
| 453 | + return "healthy"; |
| 454 | + case MONGOC_CLUSTER_STATE_DEAD: |
| 455 | + return "dead"; |
| 456 | + case MONGOC_CLUSTER_STATE_UNHEALTHY: |
| 457 | + return "unhealthy"; |
| 458 | + } |
| 459 | + |
| 460 | + return "broken"; |
| 461 | +} |
| 462 | + |
| 463 | +static void add_next_index_node(zval *array, mongoc_cluster_node_t *node) |
| 464 | +{ |
| 465 | + php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER; |
| 466 | + |
| 467 | + MAKE_STD_ZVAL(state.zchild); |
| 468 | + zval *data = NULL; |
| 469 | + |
| 470 | + MAKE_STD_ZVAL(data); |
| 471 | + array_init(data); |
| 472 | + add_assoc_long_ex(data, ZEND_STRS("ping_avg_msec"), node->ping_avg_msec); |
| 473 | + add_assoc_long_ex(data, ZEND_STRS("stamp"), node->stamp); |
| 474 | + add_assoc_bool_ex(data, ZEND_STRS("primary"), node->primary); |
| 475 | + add_assoc_bool_ex(data, ZEND_STRS("needs_auth"), node->needs_auth); |
| 476 | + add_assoc_bool_ex(data, ZEND_STRS("isdbgrid"), node->needs_auth); |
| 477 | + add_assoc_long_ex(data, ZEND_STRS("min_wire_version"), node->min_wire_version); |
| 478 | + add_assoc_long_ex(data, ZEND_STRS("max_wire_version"), node->max_wire_version); |
| 479 | + add_assoc_long_ex(data, ZEND_STRS("max_write_batch_size"), node->max_write_batch_size); |
| 480 | + add_assoc_string_ex(data, ZEND_STRS("replSet"), node->replSet, 0); |
| 481 | + add_assoc_long_ex(data, ZEND_STRS("last_read_msec"), node->last_read_msec); |
| 482 | + bson_to_zval(bson_get_data(&node->tags), node->tags.len, &state); |
| 483 | + add_assoc_zval_ex(data, ZEND_STRS("tags"), state.zchild); |
| 484 | + add_assoc_string_ex(data, ZEND_STRS("host_and_port"), node->host.host_and_port, 0); |
| 485 | + /* TODO: Should this contain the actual stream too? we have the mongoc_stream_t... */ |
| 486 | + |
| 487 | + add_next_index_zval(array, data); |
| 488 | +} |
| 489 | + |
| 490 | +HashTable *php_phongo_manager_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */ |
| 491 | +{ |
| 492 | + zval *retval = NULL; |
| 493 | + php_phongo_manager_t *intern; |
| 494 | + |
| 495 | + *is_temp = 0; |
| 496 | + intern = (php_phongo_manager_t *)zend_object_store_get_object(object TSRMLS_CC); |
| 497 | + |
| 498 | + |
| 499 | + MAKE_STD_ZVAL(retval); |
| 500 | + array_init(retval); |
| 501 | + add_assoc_string_ex(retval, ZEND_STRS("foobar"), (char *)"some other cool stuff", 0); |
| 502 | + |
| 503 | + add_assoc_long_ex(retval, ZEND_STRS("request_id"), intern->client->request_id); |
| 504 | + add_assoc_string_ex(retval, ZEND_STRS("uri"), (char *)mongoc_uri_get_string(intern->client->uri), 0); |
| 505 | + |
| 506 | + { |
| 507 | + zval *cluster = NULL; |
| 508 | + MAKE_STD_ZVAL(cluster); |
| 509 | + array_init(cluster); |
| 510 | + add_assoc_string_ex(cluster, ZEND_STRS("mode"), (char *)phongo_cluster_mode_tostring(intern->client->cluster.mode), 0); |
| 511 | + add_assoc_string_ex(cluster, ZEND_STRS("state"), (char *)phongo_cluster_state_tostring(intern->client->cluster.state), 0); |
| 512 | + add_assoc_long_ex(cluster, ZEND_STRS("request_id"), intern->client->cluster.request_id); |
| 513 | + add_assoc_long_ex(cluster, ZEND_STRS("sockettimeoutms"), intern->client->cluster.sockettimeoutms); |
| 514 | + add_assoc_long_ex(cluster, ZEND_STRS("last_reconnect"), intern->client->cluster.last_reconnect); |
| 515 | + add_assoc_string_ex(cluster, ZEND_STRS("uri"), (char *)mongoc_uri_get_string(intern->client->cluster.uri), 0); |
| 516 | + add_assoc_long_ex(cluster, ZEND_STRS("requires_auth"), intern->client->cluster.requires_auth); |
| 517 | + { |
| 518 | + zval *nodes = NULL; |
| 519 | + unsigned int i; |
| 520 | + |
| 521 | + MAKE_STD_ZVAL(nodes); |
| 522 | + array_init(nodes); |
| 523 | + for (i = 0; i < intern->client->cluster.nodes_len; i++) { |
| 524 | + add_next_index_node(nodes, &intern->client->cluster.nodes[i]); |
| 525 | + } |
| 526 | + add_assoc_zval_ex(cluster, ZEND_STRS("nodes"), nodes); |
| 527 | + } |
| 528 | + add_assoc_long_ex(cluster, ZEND_STRS("max_bson_size"), intern->client->cluster.max_bson_size); |
| 529 | + add_assoc_long_ex(cluster, ZEND_STRS("max_msg_size"), intern->client->cluster.max_msg_size); |
| 530 | + add_assoc_long_ex(cluster, ZEND_STRS("sec_latency_ms"), intern->client->cluster.sec_latency_ms); |
| 531 | + { |
| 532 | + mongoc_host_list_t host; |
| 533 | + zval *peers = NULL; |
| 534 | + mongoc_list_t *list; |
| 535 | + mongoc_list_t *liter; |
| 536 | + |
| 537 | + MAKE_STD_ZVAL(peers); |
| 538 | + array_init(peers); |
| 539 | + |
| 540 | + list = intern->client->cluster.peers; |
| 541 | + for (liter = list; liter ; liter = liter->next) { |
| 542 | + |
| 543 | + if (_mongoc_host_list_from_string(&host, liter->data)) { |
| 544 | + add_next_index_string(peers, host.host_and_port, 1); |
| 545 | + } |
| 546 | + } |
| 547 | + add_assoc_zval_ex(cluster, ZEND_STRS("peers"), peers); |
| 548 | + } |
| 549 | + add_assoc_string_ex(cluster, ZEND_STRS("replSet"), intern->client->cluster.replSet, 0); |
| 550 | + |
| 551 | + add_assoc_zval_ex(retval, ZEND_STRS("cluster"), cluster); |
| 552 | + } |
| 553 | + |
| 554 | + return Z_ARRVAL_P(retval); |
| 555 | +} /* }}} */ |
427 | 556 | /* }}} */
|
428 | 557 |
|
429 | 558 | /* {{{ PHP_MINIT_FUNCTION */
|
430 | 559 | PHP_MINIT_FUNCTION(Manager)
|
431 | 560 | {
|
432 |
| - (void)type; /* We don't care if we are loaded via dl() or extension= */ |
| 561 | + (void)type;(void)module_number; |
433 | 562 | zend_class_entry ce;
|
434 | 563 |
|
435 | 564 | INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "Manager", php_phongo_manager_me);
|
436 |
| - ce.create_object = php_phongo_manager_create_object; |
437 | 565 | php_phongo_manager_ce = zend_register_internal_class(&ce TSRMLS_CC);
|
| 566 | + php_phongo_manager_ce->create_object = php_phongo_manager_create_object; |
438 | 567 | php_phongo_manager_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
|
439 | 568 |
|
| 569 | + memcpy(&php_phongo_handler_manager, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 570 | + php_phongo_handler_manager.get_debug_info = php_phongo_manager_get_debug_info; |
440 | 571 |
|
441 | 572 | return SUCCESS;
|
442 | 573 | }
|
|
0 commit comments