Skip to content

Commit 57871d1

Browse files
committed
PHPC-340: BSON\UTCDateTime debug handler
The milliseconds will be dumped as a string value on 32-bit platforms.
1 parent 8f39e13 commit 57871d1

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

src/BSON/UTCDateTime.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
PHONGO_API zend_class_entry *php_phongo_utcdatetime_ce;
4646

47+
zend_object_handlers php_phongo_handler_utcdatetime;
48+
4749
/* {{{ proto BSON\UTCDateTime UTCDateTime::__construct(integer $milliseconds)
4850
Construct a new UTCDateTime */
4951
PHP_METHOD(UTCDateTime, __construct)
@@ -161,10 +163,35 @@ zend_object_value php_phongo_utcdatetime_create_object(zend_class_entry *class_t
161163
object_properties_init(&intern->std, class_type);
162164

163165
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_utcdatetime_free_object, NULL TSRMLS_CC);
164-
retval.handlers = phongo_get_std_object_handlers();
166+
retval.handlers = &php_phongo_handler_utcdatetime;
165167

166168
return retval;
167169
} /* }}} */
170+
171+
HashTable *php_phongo_utcdatetime_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
172+
{
173+
php_phongo_utcdatetime_t *intern;
174+
zval retval = zval_used_for_init;
175+
176+
*is_temp = 1;
177+
intern = (php_phongo_utcdatetime_t *)zend_object_store_get_object(object TSRMLS_CC);
178+
179+
array_init(&retval);
180+
181+
#if SIZEOF_LONG == 4
182+
{
183+
char *tmp;
184+
int tmp_len;
185+
186+
tmp_len = spprintf(&tmp, 0, "%" PRId64, intern->milliseconds);
187+
add_assoc_stringl_ex(&retval, ZEND_STRS("milliseconds"), tmp, tmp_len, 0);
188+
}
189+
#else
190+
add_assoc_long_ex(&retval, ZEND_STRS("milliseconds"), intern->milliseconds);
191+
#endif
192+
193+
return Z_ARRVAL(retval);
194+
} /* }}} */
168195
/* }}} */
169196

170197
/* {{{ PHP_MINIT_FUNCTION */
@@ -179,6 +206,8 @@ PHP_MINIT_FUNCTION(UTCDateTime)
179206

180207
zend_class_implements(php_phongo_utcdatetime_ce TSRMLS_CC, 1, php_phongo_type_ce);
181208

209+
memcpy(&php_phongo_handler_utcdatetime, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
210+
php_phongo_handler_utcdatetime.get_debug_info = php_phongo_utcdatetime_get_debug_info;
182211

183212
return SUCCESS;
184213
}

tests/bson/bson-utcdatetime-002.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
BSON BSON\UTCDateTime debug handler (32-bit)
3+
--SKIPIF--
4+
<?php if (4 !== PHP_INT_SIZE) { die('skip Only for 32-bit platform'); } ?>
5+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$classname = BSON_NAMESPACE . '\UTCDateTime';
11+
$utcdatetime = new $classname('1416445411987');
12+
13+
var_dump($utcdatetime);
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECTF--
19+
object(%SBSON\UTCDateTime)#%d (%d) {
20+
["milliseconds"]=>
21+
string(13) "1416445411987"
22+
}
23+
===DONE===

tests/bson/bson-utcdatetime-003.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
BSON BSON\UTCDateTime debug handler (64-bit)
3+
--SKIPIF--
4+
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
5+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$classname = BSON_NAMESPACE . '\UTCDateTime';
11+
$utcdatetime = new $classname(1416445411987);
12+
13+
var_dump($utcdatetime);
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECTF--
19+
object(%SBSON\UTCDateTime)#%d (%d) {
20+
["milliseconds"]=>
21+
int(1416445411987)
22+
}
23+
===DONE===

tests/manager/manager-var-dump-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ object(MongoDB\Driver\Manager)#%d (%d) {
9292
int(1000)
9393
["localTime"]=>
9494
object(%s\UTCDateTime)#%d (%d) {
95+
["milliseconds"]=>
96+
%r(int\(\d+\)|string\(\d+\) "\d+")%r
9597
}
9698
["maxWireVersion"]=>
9799
int(%d)

tests/server/server-debug.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ object(MongoDB\Driver\Server)#%d (%d) {
4444
int(1000)
4545
["localTime"]=>
4646
object(%s\UTCDateTime)#%d (%d) {
47+
["milliseconds"]=>
48+
%r(int\(\d+\)|string\(\d+\) "\d+")%r
4749
}
4850
["maxWireVersion"]=>
4951
int(%d)

0 commit comments

Comments
 (0)