Skip to content

Commit 1d8362c

Browse files
committed
Merge pull request #88
2 parents bc2dded + 6df3f8a commit 1d8362c

12 files changed

+278
-8
lines changed

src/BSON/Javascript.c

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

4545
PHONGO_API zend_class_entry *php_phongo_javascript_ce;
4646

47+
zend_object_handlers php_phongo_handler_javascript;
4748

4849
/* {{{ proto BSON\Javascript Javascript::__construct(string $javascript[, array|object $document])
4950
* The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated
@@ -119,12 +120,43 @@ zend_object_value php_phongo_javascript_create_object(zend_class_entry *class_ty
119120
object_properties_init(&intern->std, class_type);
120121

121122
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_javascript_free_object, NULL TSRMLS_CC);
122-
retval.handlers = phongo_get_std_object_handlers();
123+
retval.handlers = &php_phongo_handler_javascript;
123124

124125
intern->document = NULL;
125126

126127
return retval;
127128
} /* }}} */
129+
130+
HashTable *php_phongo_javascript_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
131+
{
132+
php_phongo_javascript_t *intern;
133+
zval retval = zval_used_for_init;
134+
135+
136+
*is_temp = 1;
137+
intern = (php_phongo_javascript_t *)zend_object_store_get_object(object TSRMLS_CC);
138+
139+
array_init(&retval);
140+
141+
add_assoc_stringl_ex(&retval, ZEND_STRS("javascript"), intern->javascript, intern->javascript_len, 1);
142+
143+
if (intern->document) {
144+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
145+
146+
MAKE_STD_ZVAL(state.zchild);
147+
148+
if (bson_to_zval(bson_get_data(intern->document), intern->document->len, &state)) {
149+
Z_ADDREF_P(state.zchild);
150+
add_assoc_zval_ex(&retval, ZEND_STRS("scope"), state.zchild);
151+
} else {
152+
add_assoc_null_ex(&retval, ZEND_STRS("scope"));
153+
}
154+
155+
zval_ptr_dtor(&state.zchild);
156+
}
157+
158+
return Z_ARRVAL(retval);
159+
} /* }}} */
128160
/* }}} */
129161

130162
/* {{{ PHP_MINIT_FUNCTION */
@@ -140,6 +172,8 @@ PHP_MINIT_FUNCTION(Javascript)
140172

141173
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_phongo_type_ce);
142174

175+
memcpy(&php_phongo_handler_javascript, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
176+
php_phongo_handler_javascript.get_debug_info = php_phongo_javascript_get_debug_info;
143177

144178
return SUCCESS;
145179
}

src/BSON/Regex.c

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

4545
PHONGO_API zend_class_entry *php_phongo_regex_ce;
4646

47+
zend_object_handlers php_phongo_handler_regex;
48+
4749
/* {{{ proto BSON\Regex Regex::__construct(string $pattern, string $flags)
4850
Constructs a new regular expression. */
4951
PHP_METHOD(Regex, __construct)
@@ -185,10 +187,27 @@ zend_object_value php_phongo_regex_create_object(zend_class_entry *class_type TS
185187
object_properties_init(&intern->std, class_type);
186188

187189
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_regex_free_object, NULL TSRMLS_CC);
188-
retval.handlers = phongo_get_std_object_handlers();
190+
retval.handlers = &php_phongo_handler_regex;
189191

190192
return retval;
191193
} /* }}} */
194+
195+
HashTable *php_phongo_regex_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
196+
{
197+
php_phongo_regex_t *intern;
198+
zval retval = zval_used_for_init;
199+
200+
201+
*is_temp = 1;
202+
intern = (php_phongo_regex_t *)zend_object_store_get_object(object TSRMLS_CC);
203+
204+
array_init(&retval);
205+
206+
add_assoc_stringl_ex(&retval, ZEND_STRS("pattern"), intern->pattern, intern->pattern_len, 1);
207+
add_assoc_stringl_ex(&retval, ZEND_STRS("flags"), intern->flags, intern->flags_len, 1);
208+
209+
return Z_ARRVAL(retval);
210+
} /* }}} */
192211
/* }}} */
193212

194213
/* {{{ PHP_MINIT_FUNCTION */
@@ -203,6 +222,8 @@ PHP_MINIT_FUNCTION(Regex)
203222

204223
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_type_ce);
205224

225+
memcpy(&php_phongo_handler_regex, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
226+
php_phongo_handler_regex.get_debug_info = php_phongo_regex_get_debug_info;
206227

207228
return SUCCESS;
208229
}

src/BSON/Timestamp.c

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

4545
PHONGO_API zend_class_entry *php_phongo_timestamp_ce;
4646

47+
zend_object_handlers php_phongo_handler_timestamp;
48+
4749
/* {{{ proto BSON\Timestamp Timestamp::__construct(integer $increment, int $timestamp)
4850
Construct a new BSON Timestamp (4bytes increment, 4bytes timestamp) */
4951
PHP_METHOD(Timestamp, __construct)
@@ -129,10 +131,27 @@ zend_object_value php_phongo_timestamp_create_object(zend_class_entry *class_typ
129131
object_properties_init(&intern->std, class_type);
130132

131133
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_timestamp_free_object, NULL TSRMLS_CC);
132-
retval.handlers = phongo_get_std_object_handlers();
134+
retval.handlers = &php_phongo_handler_timestamp;
133135

134136
return retval;
135137
} /* }}} */
138+
139+
HashTable *php_phongo_timestamp_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
140+
{
141+
php_phongo_timestamp_t *intern;
142+
zval retval = zval_used_for_init;
143+
144+
145+
*is_temp = 1;
146+
intern = (php_phongo_timestamp_t *)zend_object_store_get_object(object TSRMLS_CC);
147+
148+
array_init(&retval);
149+
150+
add_assoc_long_ex(&retval, ZEND_STRS("increment"), intern->increment);
151+
add_assoc_long_ex(&retval, ZEND_STRS("timestamp"), intern->timestamp);
152+
153+
return Z_ARRVAL(retval);
154+
} /* }}} */
136155
/* }}} */
137156

138157
/* {{{ PHP_MINIT_FUNCTION */
@@ -147,6 +166,9 @@ PHP_MINIT_FUNCTION(Timestamp)
147166

148167
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, php_phongo_type_ce);
149168

169+
memcpy(&php_phongo_handler_timestamp, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
170+
php_phongo_handler_timestamp.get_debug_info = php_phongo_timestamp_get_debug_info;
171+
150172

151173
return SUCCESS;
152174
}

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-encode-003.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object(stdClass)#%d (1) {
5959
["props"]=>
6060
array(3) {
6161
["__pclass"]=>
62-
object(%sBSON\Binary)#%d (2) {
62+
object(%SBSON\Binary)#%d (2) {
6363
["data"]=>
6464
string(7) "MyClass"
6565
["type"]=>
@@ -83,7 +83,7 @@ object(stdClass)#%d (1) {
8383
["props"]=>
8484
array(4) {
8585
["__pclass"]=>
86-
object(%sBSON\Binary)#%d (2) {
86+
object(%SBSON\Binary)#%d (2) {
8787
["data"]=>
8888
string(8) "MyClass2"
8989
["type"]=>
@@ -116,7 +116,7 @@ object(stdClass)#%d (1) {
116116
["props"]=>
117117
array(3) {
118118
["__pclass"]=>
119-
object(%sBSON\Binary)#%d (2) {
119+
object(%SBSON\Binary)#%d (2) {
120120
["data"]=>
121121
string(7) "MyClass"
122122
["type"]=>
@@ -133,7 +133,7 @@ object(stdClass)#%d (1) {
133133
["props"]=>
134134
array(4) {
135135
["__pclass"]=>
136-
object(%sBSON\Binary)#%d (2) {
136+
object(%SBSON\Binary)#%d (2) {
137137
["data"]=>
138138
string(8) "MyClass2"
139139
["type"]=>

tests/bson/bson-javascript-002.phpt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
BSON BSON\Javascript debug handler
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$jsClassname = BSON_NAMESPACE . '\Javascript';
10+
$oidClassname = BSON_NAMESPACE . '\ObjectId';
11+
12+
$tests = array(
13+
array(
14+
'function foo(bar) { return bar; }',
15+
array(),
16+
),
17+
array(
18+
'function foo() { return foo; }',
19+
array('foo' => 42),
20+
),
21+
array(
22+
'function foo() { return id; }',
23+
array('id' => new $oidClassname('53e2a1c40640fd72175d4603')),
24+
),
25+
);
26+
27+
foreach ($tests as $test) {
28+
list($code, $scope) = $test;
29+
30+
$js = new $jsClassname($code, $scope);
31+
var_dump($js);
32+
}
33+
34+
?>
35+
===DONE===
36+
<?php exit(0); ?>
37+
--EXPECTF--
38+
object(%SBSON\Javascript)#%d (%d) {
39+
["javascript"]=>
40+
string(33) "function foo(bar) { return bar; }"
41+
["scope"]=>
42+
object(stdClass)#%d (%d) {
43+
}
44+
}
45+
object(%SBSON\Javascript)#%d (%d) {
46+
["javascript"]=>
47+
string(30) "function foo() { return foo; }"
48+
["scope"]=>
49+
object(stdClass)#%d (%d) {
50+
["foo"]=>
51+
int(42)
52+
}
53+
}
54+
object(%SBSON\Javascript)#%d (%d) {
55+
["javascript"]=>
56+
string(29) "function foo() { return id; }"
57+
["scope"]=>
58+
object(stdClass)#%d (%d) {
59+
["id"]=>
60+
object(MongoDB\BSON\ObjectID)#%d (%d) {
61+
["oid"]=>
62+
string(24) "53e2a1c40640fd72175d4603"
63+
}
64+
}
65+
}
66+
===DONE===

tests/bson/bson-regex-002.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
BSON BSON\Regex debug handler
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$classname = BSON_NAMESPACE . '\Regex';
10+
$regex = new $classname('regexp', 'i');
11+
12+
var_dump($regex);
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECTF--
18+
object(%SBSON\Regex)#%d (%d) {
19+
["pattern"]=>
20+
string(6) "regexp"
21+
["flags"]=>
22+
string(1) "i"
23+
}
24+
===DONE===

tests/bson/bson-timestamp-002.phpt

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

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===

0 commit comments

Comments
 (0)