Skip to content

Commit 031a8da

Browse files
committed
PHPC-140: var_dump()ing WriteConcernError and WriteError
1 parent 7626368 commit 031a8da

File tree

7 files changed

+101
-12
lines changed

7 files changed

+101
-12
lines changed

src/MongoDB/WriteConcernError.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
PHONGO_API zend_class_entry *php_phongo_writeconcernerror_ce;
4646

47+
zend_object_handlers php_phongo_handler_writeconcernerror;
48+
4749
/* {{{ proto integer WriteConcernError::getCode()
4850
Returns the MongoDB error code */
4951
PHP_METHOD(WriteConcernError, getCode)
@@ -156,32 +158,51 @@ static void php_phongo_writeconcernerror_free_object(void *object TSRMLS_DC) /*
156158
zend_object_value php_phongo_writeconcernerror_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
157159
{
158160
zend_object_value retval;
159-
php_phongo_writeconcernerror_t *intern;
161+
php_phongo_writeconcernerror_t *intern = NULL;
160162

161-
intern = (php_phongo_writeconcernerror_t *)emalloc(sizeof(php_phongo_writeconcernerror_t));
162-
memset(intern, 0, sizeof(php_phongo_writeconcernerror_t));
163+
intern = (php_phongo_writeconcernerror_t *)ecalloc(1, sizeof *intern);
163164

164165
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
165166
object_properties_init(&intern->std, class_type);
166167

167168
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_writeconcernerror_free_object, NULL TSRMLS_CC);
168-
retval.handlers = phongo_get_std_object_handlers();
169+
retval.handlers = &php_phongo_handler_writeconcernerror;
169170

170171
return retval;
171172
} /* }}} */
173+
174+
HashTable *php_phongo_writeconcernerror_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
175+
{
176+
php_phongo_writeconcernerror_t *intern;
177+
zval retval = zval_used_for_init;
178+
179+
180+
*is_temp = 1;
181+
intern = (php_phongo_writeconcernerror_t *)zend_object_store_get_object(object TSRMLS_CC);
182+
183+
array_init_size(&retval, 3);
184+
add_assoc_string_ex(&retval, ZEND_STRS("message"), intern->message, 1);
185+
add_assoc_long_ex(&retval, ZEND_STRS("code"), intern->code);
186+
Z_ADDREF_P(intern->info);
187+
add_assoc_zval_ex(&retval, ZEND_STRS("info"), intern->info);
188+
189+
return Z_ARRVAL(retval);
190+
} /* }}} */
172191
/* }}} */
173192

174193
/* {{{ PHP_MINIT_FUNCTION */
175194
PHP_MINIT_FUNCTION(WriteConcernError)
176195
{
177-
(void)type; /* We don't care if we are loaded via dl() or extension= */
196+
(void)type; (void)module_number;
178197
zend_class_entry ce;
179198

180199
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "WriteConcernError", php_phongo_writeconcernerror_me);
181-
ce.create_object = php_phongo_writeconcernerror_create_object;
182200
php_phongo_writeconcernerror_ce = zend_register_internal_class(&ce TSRMLS_CC);
201+
php_phongo_writeconcernerror_ce->create_object = php_phongo_writeconcernerror_create_object;
183202
php_phongo_writeconcernerror_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
184203

204+
memcpy(&php_phongo_handler_writeconcernerror, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
205+
php_phongo_handler_writeconcernerror.get_debug_info = php_phongo_writeconcernerror_get_debug_info;
185206

186207
return SUCCESS;
187208
}

src/MongoDB/WriteError.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
PHONGO_API zend_class_entry *php_phongo_writeerror_ce;
4646

47+
zend_object_handlers php_phongo_handler_writeerror;
48+
4749
/* {{{ proto integer WriteError::getCode()
4850
Returns the MongoDB error code */
4951
PHP_METHOD(WriteError, getCode)
@@ -149,32 +151,50 @@ static void php_phongo_writeerror_free_object(void *object TSRMLS_DC) /* {{{ */
149151
zend_object_value php_phongo_writeerror_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
150152
{
151153
zend_object_value retval;
152-
php_phongo_writeerror_t *intern;
154+
php_phongo_writeerror_t *intern = NULL;
153155

154-
intern = (php_phongo_writeerror_t *)emalloc(sizeof(php_phongo_writeerror_t));
155-
memset(intern, 0, sizeof(php_phongo_writeerror_t));
156+
intern = (php_phongo_writeerror_t *)ecalloc(1, sizeof *intern);
156157

157158
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
158159
object_properties_init(&intern->std, class_type);
159160

160161
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_writeerror_free_object, NULL TSRMLS_CC);
161-
retval.handlers = phongo_get_std_object_handlers();
162+
retval.handlers = &php_phongo_handler_writeerror;
162163

163164
return retval;
164165
} /* }}} */
166+
167+
HashTable *php_phongo_writeerror_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
168+
{
169+
php_phongo_writeerror_t *intern;
170+
zval retval = zval_used_for_init;
171+
172+
173+
*is_temp = 1;
174+
intern = (php_phongo_writeerror_t *)zend_object_store_get_object(object TSRMLS_CC);
175+
176+
array_init_size(&retval, 3);
177+
add_assoc_string_ex(&retval, ZEND_STRS("message"), intern->message, 1);
178+
add_assoc_long_ex(&retval, ZEND_STRS("code"), intern->code);
179+
add_assoc_long_ex(&retval, ZEND_STRS("index"), intern->index);
180+
181+
return Z_ARRVAL(retval);
182+
} /* }}} */
165183
/* }}} */
166184

167185
/* {{{ PHP_MINIT_FUNCTION */
168186
PHP_MINIT_FUNCTION(WriteError)
169187
{
170-
(void)type; /* We don't care if we are loaded via dl() or extension= */
188+
(void)type; (void)module_number;
171189
zend_class_entry ce;
172190

173191
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "WriteError", php_phongo_writeerror_me);
174-
ce.create_object = php_phongo_writeerror_create_object;
175192
php_phongo_writeerror_ce = zend_register_internal_class(&ce TSRMLS_CC);
193+
php_phongo_writeerror_ce->create_object = php_phongo_writeerror_create_object;
176194
php_phongo_writeerror_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
177195

196+
memcpy(&php_phongo_handler_writeerror, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
197+
php_phongo_handler_writeerror.get_debug_info = php_phongo_writeerror_get_debug_info;
178198

179199
return SUCCESS;
180200
}

tests/replicaset/writeconcern-error.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ matchedCount: 0
2626
modifiedCount: 0
2727
upsertedCount: 0
2828
deletedCount: 0
29+
object(MongoDB\Driver\WriteConcernError)#%d (%d) {
30+
["message"]=>
31+
string(33) "waiting for replication timed out"
32+
["code"]=>
33+
int(64)
34+
["info"]=>
35+
array(1) {
36+
["wtimeout"]=>
37+
bool(true)
38+
}
39+
}
2940
writeConcernError.message: waiting for replication timed out
3041
writeConcernError.code: 64
3142
writeConcernError.info: array(1) {

tests/replicaset/writeconcernerror-0001.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ matchedCount: 1
3232
modifiedCount: 1
3333
upsertedCount: 0
3434
deletedCount: 1
35+
object(MongoDB\Driver\WriteConcernError)#%d (%d) {
36+
["message"]=>
37+
string(33) "waiting for replication timed out"
38+
["code"]=>
39+
int(64)
40+
["info"]=>
41+
array(1) {
42+
["wtimeout"]=>
43+
bool(true)
44+
}
45+
}
3546
writeConcernError.message: waiting for replication timed out
3647
writeConcernError.code: 64
3748
writeConcernError.info: array(1) {

tests/standalone/manager-executeWriteBatch-002.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ matchedCount: 0
4343
modifiedCount: 0
4444
upsertedCount: 0
4545
deletedCount: 0
46+
object(MongoDB\Driver\WriteError)#%d (%d) {
47+
["message"]=>
48+
string(%d) "%s"
49+
["code"]=>
50+
int(11000)
51+
["index"]=>
52+
int(1)
53+
}
4654
writeError[1].message: %s
4755
writeError[1].code: 11000
4856

tests/standalone/manager-executeWriteBatch-003.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,24 @@ matchedCount: 0
4343
modifiedCount: 0
4444
upsertedCount: 0
4545
deletedCount: 0
46+
object(MongoDB\Driver\WriteError)#%d (%d) {
47+
["message"]=>
48+
string(%d) "%s"
49+
["code"]=>
50+
int(11000)
51+
["index"]=>
52+
int(1)
53+
}
4654
writeError[1].message: %s
4755
writeError[1].code: 11000
56+
object(MongoDB\Driver\WriteError)#%d (%d) {
57+
["message"]=>
58+
string(%d) "%s"
59+
["code"]=>
60+
int(11000)
61+
["index"]=>
62+
int(3)
63+
}
4864
writeError[3].message: %s
4965
writeError[3].code: 11000
5066

tests/utils/tools.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function printWriteResult(MongoDB\Driver\WriteResult $result)
138138
function printWriteConcernError(MongoDB\Driver\WriteConcernError $error = null)
139139
{
140140
if ($error) {
141+
var_dump($error);
141142
printf("writeConcernError.message: %s\n", $error->getMessage());
142143
printf("writeConcernError.code: %d\n", $error->getCode());
143144
printf("writeConcernError.info: ");
@@ -147,6 +148,7 @@ function printWriteConcernError(MongoDB\Driver\WriteConcernError $error = null)
147148

148149
function printWriteError(MongoDB\Driver\WriteError $error)
149150
{
151+
var_dump($error);
150152
printf("writeError[%d].message: %s\n", $error->getIndex(), $error->getMessage());
151153
printf("writeError[%d].code: %d\n", $error->getIndex(), $error->getCode());
152154
}

0 commit comments

Comments
 (0)