Skip to content

Commit 7c99f91

Browse files
pboyd04derickr
authored andcommitted
PHP7 upgrades
1 parent 78eecb4 commit 7c99f91

File tree

1 file changed

+102
-12
lines changed

1 file changed

+102
-12
lines changed

src/bson.c

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,13 @@ bool php_phongo_bson_visit_binary(const bson_iter_t *iter ARG_UNUSED, const char
215215
TSRMLS_FETCH();
216216

217217
if (v_subtype == 0x80 && strcmp(key, PHONGO_ODM_FIELD_NAME) == 0) {
218+
#if PHP_VERSION_ID >= 70000
219+
zend_string *zs_classname = zend_string_init((char *)v_binary, v_binary_len, 0);
220+
zend_class_entry *found_ce = zend_fetch_class(zs_classname, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
221+
zend_string_free(zs_classname);
222+
#else
218223
zend_class_entry *found_ce = zend_fetch_class((char *)v_binary, v_binary_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
224+
#endif
219225

220226
if (found_ce && PHONGO_IS_CLASS_INSTANTIATABLE(found_ce) && instanceof_function(found_ce, php_phongo_persistable_ce TSRMLS_CC)) {
221227
((php_phongo_bson_state *)data)->odm = found_ce;
@@ -487,9 +493,17 @@ bool php_phongo_bson_visit_document(const bson_iter_t *iter ARG_UNUSED, const ch
487493

488494
MAKE_STD_ZVAL(obj);
489495
object_init_ex(obj, state.odm ? state.odm : state.map.document);
496+
#if PHP_VERSION_ID >= 70000
497+
zend_call_method_with_1_params(obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state.zchild);
498+
#else
490499
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state.zchild);
500+
#endif
491501
add_assoc_zval(retval, key, obj);
502+
#if PHP_VERSION_ID >= 70000
503+
zval_ptr_dtor(state.zchild);
504+
#else
492505
zval_ptr_dtor(&state.zchild);
506+
#endif
493507
break;
494508
}
495509

@@ -528,9 +542,17 @@ bool php_phongo_bson_visit_array(const bson_iter_t *iter ARG_UNUSED, const char
528542

529543
MAKE_STD_ZVAL(obj);
530544
object_init_ex(obj, state.map.array);
545+
#if PHP_VERSION_ID >= 70000
546+
zend_call_method_with_1_params(obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state.zchild);
547+
#else
531548
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state.zchild);
549+
#endif
532550
add_assoc_zval(retval, key, obj);
551+
#if PHP_VERSION_ID >= 70000
552+
zval_ptr_dtor(state.zchild);
553+
#else
533554
zval_ptr_dtor(&state.zchild);
555+
#endif
534556
break;
535557
}
536558

@@ -571,10 +593,17 @@ int php_phongo_is_array_or_document(zval **val TSRMLS_DC) /* {{{ */
571593
unsigned long idx = 0;
572594
int hash_type = 0;
573595
HashPosition pos;
596+
#if PHP_VERSION_ID >= 70000
597+
zend_string *zs_key;
598+
#endif
574599

575600
zend_hash_internal_pointer_reset_ex(ht_data, &pos);
576601
for (;; zend_hash_move_forward_ex(ht_data, &pos)) {
602+
#if PHP_VERSION_ID >= 70000
603+
hash_type = zend_hash_get_current_key_ex(ht_data, &zs_key, &index, &pos);
604+
#else
577605
hash_type = zend_hash_get_current_key_ex(ht_data, &key, &key_len, &index, 0, &pos);
606+
#endif
578607
if (hash_type == HASH_KEY_NON_EXISTENT) {
579608
break;
580609
}
@@ -604,17 +633,25 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
604633
zval *obj_data = NULL;
605634
bson_t child;
606635
HashTable *tmp_ht;
607-
636+
#if PHP_VERSION_ID >= 70000
637+
zend_call_method_with_0_params(object, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, obj_data);
638+
#else
608639
zend_call_method_with_0_params(&object, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &obj_data);
640+
#endif
609641

610642
if (!obj_data) {
611643
/* zend_call_method() failed */
612644
return;
613645
}
614646

615647
if (Z_TYPE_P(obj_data) != IS_ARRAY && !(Z_TYPE_P(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(obj_data), zend_standard_class_def TSRMLS_CC))) {
616-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s::%s() to return an array or stdClass, %s given", Z_OBJCE_P(object)->name, BSON_SERIALIZE_FUNC_NAME, (Z_TYPE_P(obj_data) == IS_OBJECT ? Z_OBJCE_P(obj_data)->name : zend_get_type_by_const(Z_TYPE_P(obj_data))));
648+
//TODO-PB
649+
//phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s::%s() to return an array or stdClass, %s given", Z_OBJCE_P(object)->name, BSON_SERIALIZE_FUNC_NAME, (Z_TYPE_P(obj_data) == IS_OBJECT ? Z_OBJCE_P(obj_data)->name : zend_get_type_by_const(Z_TYPE_P(obj_data))));
650+
#if PHP_VERSION_ID >= 70000
651+
zval_ptr_dtor(obj_data);
652+
#else
617653
zval_ptr_dtor(&obj_data);
654+
#endif
618655

619656
return;
620657
}
@@ -631,7 +668,8 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
631668
bson_append_document_begin(bson, key, key_len, &child);
632669
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce TSRMLS_CC)) {
633670
if (flags & PHONGO_BSON_ADD_CHILD_ODS) {
634-
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
671+
//TODO-PB
672+
//bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
635673
}
636674
}
637675
zval_to_bson(obj_data, flags, &child, NULL TSRMLS_CC);
@@ -725,10 +763,19 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char
725763
case IS_NULL:
726764
bson_append_null(bson, key, key_len);
727765
break;
728-
766+
#if PHP_VERSION_ID >= 70000
767+
case IS_TRUE:
768+
bson_append_bool(bson, key, key_len, true);
769+
break;
770+
771+
case IS_FALSE:
772+
bson_append_bool(bson, key, key_len, false);
773+
break;
774+
#else
729775
case IS_BOOL:
730776
bson_append_bool(bson, key, key_len, Z_BVAL_P(entry));
731777
break;
778+
#endif
732779

733780
case IS_LONG:
734781
BSON_APPEND_INT(bson, key, key_len, Z_LVAL_P(entry));
@@ -799,15 +846,20 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
799846
switch(Z_TYPE_P(data)) {
800847
case IS_OBJECT:
801848
if (instanceof_function(Z_OBJCE_P(data), php_phongo_serializable_ce TSRMLS_CC)) {
849+
#if PHP_VERSION_ID >= 70000
850+
zend_call_method_with_0_params(data, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, obj_data);
851+
#else
802852
zend_call_method_with_0_params(&data, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &obj_data);
853+
#endif
803854

804855
if (!obj_data) {
805856
/* zend_call_method() failed */
806857
break;
807858
}
808859

809860
if (Z_TYPE_P(obj_data) != IS_ARRAY && !(Z_TYPE_P(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(obj_data), zend_standard_class_def TSRMLS_CC))) {
810-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s::%s() to return an array or stdClass, %s given", Z_OBJCE_P(data)->name, BSON_SERIALIZE_FUNC_NAME, (Z_TYPE_P(obj_data) == IS_OBJECT ? Z_OBJCE_P(obj_data)->name : zend_get_type_by_const(Z_TYPE_P(obj_data))));
861+
//TODO-PB
862+
//phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s::%s() to return an array or stdClass, %s given", Z_OBJCE_P(data)->name, BSON_SERIALIZE_FUNC_NAME, (Z_TYPE_P(obj_data) == IS_OBJECT ? Z_OBJCE_P(obj_data)->name : zend_get_type_by_const(Z_TYPE_P(obj_data))));
811863

812864
break;
813865
}
@@ -816,8 +868,9 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
816868

817869
if (instanceof_function(Z_OBJCE_P(data), php_phongo_persistable_ce TSRMLS_CC)) {
818870
if (flags & PHONGO_BSON_ADD_ODS) {
819-
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name));
820-
zend_hash_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME));
871+
//TODO-PB
872+
//bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name));
873+
//zend_hash_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME));
821874
}
822875
}
823876

@@ -855,24 +908,39 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
855908
uint64_t index = 0;
856909
char numbuf[32];
857910
char *key = NULL;
911+
#if PHP_VERSION_ID >= 70000
912+
zend_string *zs_key;
913+
zval *entry;
914+
#else
858915
zval **entry;
916+
#endif
859917
int hash_type = HASH_KEY_NON_EXISTENT;
860-
861-
hash_type = zend_hash_get_current_key_ex(ht_data, &key, &key_len, &index, 0, &pos);
918+
#if PHP_VERSION_ID >= 70000
919+
#else
920+
hash_type = zend_hash_get_current_key_ex(ht_data, &zs_key, &index, 0, &pos);
921+
#endif
862922

863923
if (hash_type == HASH_KEY_NON_EXISTENT) {
864924
break;
865925
}
866926

927+
#if PHP_VERSION_ID >= 70000
928+
if ((entry = zend_hash_get_current_data_ex(ht_data, &pos)) != NULL) {
929+
#else
867930
if (zend_hash_get_current_data_ex(ht_data, (void **) &entry, &pos) == FAILURE) {
931+
#endif
868932
break;
869933
}
870934

871935
if (hash_type == HASH_KEY_IS_STRING) {
872936
if (ht_data_from_properties) {
873937
const char *class_name;
874-
938+
#if PHP_VERSION_ID >= 70000
939+
zend_unmangle_property_name(zs_key, &class_name, (const char **)&key);
940+
zend_string_free(zs_key);
941+
#else
875942
zend_unmangle_property_name(key, key_len-1, &class_name, (const char **)&key);
943+
#endif
876944
key_len = strlen(key);
877945

878946
/* Ignore non-public properties */
@@ -892,8 +960,11 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
892960
} else {
893961
key_len = bson_uint32_to_string(index, (const char **)&key, numbuf, sizeof(numbuf));
894962
}
895-
963+
#if PHP_VERSION_ID >= 70000
964+
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, Z_TYPE_P(entry), entry TSRMLS_CC);
965+
#else
896966
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, Z_TYPE_PP(entry), *entry TSRMLS_CC);
967+
#endif
897968
}
898969

899970
if (flags & PHONGO_BSON_ADD_ID) {
@@ -910,7 +981,11 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
910981
}
911982
}
912983
if (obj_data) {
984+
#if PHP_VERSION_ID >= 70000
985+
zval_ptr_dtor(obj_data);
986+
#else
913987
zval_ptr_dtor(&obj_data);
988+
#endif
914989
}
915990
}
916991

@@ -974,8 +1049,13 @@ int bson_to_zval_ex(const unsigned char *data, int data_len, php_phongo_bson_sta
9741049

9751050
MAKE_STD_ZVAL(obj);
9761051
object_init_ex(obj, state->odm ? state->odm : state->map.root);
977-
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state->zchild);
1052+
#if PHP_VERSION_ID >= 70000
1053+
zend_call_method_with_1_params(obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state->zchild);
1054+
zval_ptr_dtor(state->zchild);
1055+
#else
1056+
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state->zchild);
9781057
zval_ptr_dtor(&state->zchild);
1058+
#endif
9791059
state->zchild = obj;
9801060
break;
9811061
}
@@ -1026,7 +1106,13 @@ static void apply_classname_to_state(const char *classname, int classname_len, p
10261106
*type = PHONGO_TYPEMAP_NATIVE_OBJECT;
10271107
*type_ce = NULL;
10281108
} else {
1109+
#if PHP_VERSION_ID >= 70000
1110+
zend_string* zs_classname = zend_string_init(classname, classname_len, 0);
1111+
zend_class_entry *found_ce = zend_fetch_class(zs_classname, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
1112+
zend_string_free(zs_classname);
1113+
#else
10291114
zend_class_entry *found_ce = zend_fetch_class(classname, classname_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
1115+
#endif
10301116

10311117
if (!found_ce) {
10321118
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Class %s does not exist", classname);
@@ -1092,7 +1178,11 @@ PHP_FUNCTION(toPHP)
10921178
php_phongo_bson_typemap_to_state(typemap, &state.map TSRMLS_CC);
10931179

10941180
if (!bson_to_zval_ex((const unsigned char *)data, data_len, &state)) {
1181+
#if PHP_VERSION_ID >= 70000
1182+
zval_ptr_dtor(state.zchild);
1183+
#else
10951184
zval_ptr_dtor(&state.zchild);
1185+
#endif
10961186
RETURN_NULL();
10971187
}
10981188
RETURN_ZVAL(state.zchild, 0, 1);

0 commit comments

Comments
 (0)