Skip to content

Commit 70b995f

Browse files
committed
Merge pull request #340
2 parents 2062a30 + 10e1879 commit 70b995f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1965
-446
lines changed

php_phongo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
13821382

13831383
intern = Z_OBJECTID_OBJ_P(object);
13841384
bson_oid_to_string(oid, intern->oid);
1385+
intern->initialized = true;
13851386
} /* }}} */
13861387

13871388
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t *sd)
@@ -2042,6 +2043,7 @@ void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoc
20422043

20432044
intern = Z_UTCDATETIME_OBJ_P(object);
20442045
intern->milliseconds = msec_since_epoch;
2046+
intern->initialized = true;
20452047
} /* }}} */
20462048

20472049
void php_phongo_new_timestamp_from_increment_and_timestamp(zval *object, uint32_t increment, uint32_t timestamp TSRMLS_DC) /* {{{ */
@@ -2053,6 +2055,7 @@ void php_phongo_new_timestamp_from_increment_and_timestamp(zval *object, uint32_
20532055
intern = Z_TIMESTAMP_OBJ_P(object);
20542056
intern->increment = increment;
20552057
intern->timestamp = timestamp;
2058+
intern->initialized = true;
20562059
} /* }}} */
20572060
void php_phongo_new_javascript_from_javascript(int init, zval *object, const char *code, size_t code_len TSRMLS_DC) /* {{{ */
20582061
{
@@ -2091,6 +2094,7 @@ void php_phongo_new_decimal128(zval *object, const bson_decimal128_t *decimal TS
20912094

20922095
intern = Z_DECIMAL128_OBJ_P(object);
20932096
memcpy(&intern->decimal, decimal, sizeof(bson_decimal128_t));
2097+
intern->initialized = true;
20942098
} /* }}} */
20952099

20962100
void php_phongo_new_regex_from_regex_and_options(zval *object, const char *pattern, const char *flags TSRMLS_DC) /* {{{ */

php_phongo.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,19 @@ void php_phongo_cursor_free(php_phongo_cursor_t *cursor);
177177
zend_object_iterator* php_phongo_cursor_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC);
178178

179179
#if PHP_VERSION_ID >= 70000
180-
#define PHONGO_CE_INIT(ce) do { \
181-
ce->ce_flags |= ZEND_ACC_FINAL; \
182-
ce->serialize = zend_class_serialize_deny; \
183-
ce->unserialize = zend_class_unserialize_deny; \
180+
#define PHONGO_CE_FINAL(ce) do { \
181+
ce->ce_flags |= ZEND_ACC_FINAL; \
184182
} while(0);
185183
#else
186-
#define PHONGO_CE_INIT(ce) do { \
187-
ce->ce_flags |= ZEND_ACC_FINAL_CLASS; \
188-
ce->serialize = zend_class_serialize_deny; \
189-
ce->unserialize = zend_class_unserialize_deny; \
184+
#define PHONGO_CE_FINAL(ce) do { \
185+
ce->ce_flags |= ZEND_ACC_FINAL_CLASS; \
190186
} while(0);
191187
#endif
192188

189+
#define PHONGO_CE_DISABLE_SERIALIZATION(ce) do { \
190+
ce->serialize = zend_class_serialize_deny; \
191+
ce->unserialize = zend_class_unserialize_deny; \
192+
} while(0);
193193

194194
#ifdef PHP_DEBUG
195195
void _phongo_debug_bson(bson_t *bson);

php_phongo_structs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ typedef struct {
161161

162162
typedef struct {
163163
PHONGO_ZEND_OBJECT_PRE
164+
bool initialized;
164165
bson_decimal128_t decimal;
165166
PHONGO_ZEND_OBJECT_POST
166167
} php_phongo_decimal128_t;
@@ -185,6 +186,7 @@ typedef struct {
185186

186187
typedef struct {
187188
PHONGO_ZEND_OBJECT_PRE
189+
bool initialized;
188190
char oid[25];
189191
PHONGO_ZEND_OBJECT_POST
190192
} php_phongo_objectid_t;
@@ -200,13 +202,15 @@ typedef struct {
200202

201203
typedef struct {
202204
PHONGO_ZEND_OBJECT_PRE
205+
bool initialized;
203206
uint32_t increment;
204207
uint32_t timestamp;
205208
PHONGO_ZEND_OBJECT_POST
206209
} php_phongo_timestamp_t;
207210

208211
typedef struct {
209212
PHONGO_ZEND_OBJECT_PRE
213+
bool initialized;
210214
int64_t milliseconds;
211215
PHONGO_ZEND_OBJECT_POST
212216
} php_phongo_utcdatetime_t;

src/BSON/Binary.c

Lines changed: 117 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ PHONGO_API zend_class_entry *php_phongo_binary_ce;
4646

4747
zend_object_handlers php_phongo_handler_binary;
4848

49+
/* Initialize the object from a string and return whether it was successful. */
50+
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type)
51+
{
52+
if (type < 0 || type > UINT8_MAX) {
53+
return false;
54+
}
55+
56+
intern->data = estrndup(data, data_len);
57+
intern->data_len = data_len;
58+
intern->type = (uint8_t) type;
59+
60+
return true;
61+
}
62+
63+
/* Initialize the object from a HashTable and return whether it was successful. */
64+
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props)
65+
{
66+
#if PHP_VERSION_ID >= 70000
67+
zval *data, *type;
68+
69+
if ((data = zend_hash_str_find(props, "data", sizeof("data")-1)) && Z_TYPE_P(data) == IS_STRING &&
70+
(type = zend_hash_str_find(props, "type", sizeof("type")-1)) && Z_TYPE_P(type) == IS_LONG) {
71+
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type));
72+
}
73+
#else
74+
zval **data, **type;
75+
76+
if (zend_hash_find(props, "data", sizeof("data"), (void**) &data) == SUCCESS && Z_TYPE_PP(data) == IS_STRING &&
77+
zend_hash_find(props, "type", sizeof("type"), (void**) &type) == SUCCESS && Z_TYPE_PP(type) == IS_LONG) {
78+
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type));
79+
}
80+
#endif
81+
return false;
82+
}
83+
4984
/* {{{ proto BSON\Binary Binary::__construct(string $data, int $type)
5085
Construct a new BSON Binary type */
5186
PHP_METHOD(Binary, __construct)
@@ -66,16 +101,55 @@ PHP_METHOD(Binary, __construct)
66101
}
67102
zend_restore_error_handling(&error_handling TSRMLS_CC);
68103

69-
if (type < 0 || type > UINT8_MAX) {
104+
if (!php_phongo_binary_init(intern, data, data_len, type)) {
70105
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type);
106+
}
107+
}
108+
/* }}} */
109+
110+
/* {{{ proto Binary::__set_state(array $properties)
111+
*/
112+
PHP_METHOD(Binary, __set_state)
113+
{
114+
php_phongo_binary_t *intern;
115+
HashTable *props;
116+
zval *array;
117+
118+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
119+
RETURN_FALSE;
120+
}
121+
122+
object_init_ex(return_value, php_phongo_binary_ce);
123+
124+
intern = Z_BINARY_OBJ_P(return_value);
125+
props = Z_ARRVAL_P(array);
126+
127+
if (!php_phongo_binary_init_from_hash(intern, props)) {
128+
php_error(E_ERROR, "Invalid serialization data for Binary object");
129+
}
130+
}
131+
/* }}} */
132+
133+
/* {{{ proto Binary::__wakeup()
134+
*/
135+
PHP_METHOD(Binary, __wakeup)
136+
{
137+
php_phongo_binary_t *intern;
138+
HashTable *props;
139+
140+
if (zend_parse_parameters_none() == FAILURE) {
71141
return;
72142
}
73143

74-
intern->data = estrndup(data, data_len);
75-
intern->data_len = data_len;
76-
intern->type = (uint8_t) type;
144+
intern = Z_BINARY_OBJ_P(getThis());
145+
props = zend_std_get_properties(getThis() TSRMLS_CC);
146+
147+
if (!php_phongo_binary_init_from_hash(intern, props)) {
148+
php_error(E_ERROR, "Invalid serialization data for Binary object");
149+
}
77150
}
78151
/* }}} */
152+
79153
/* {{{ proto string Binary::getData()
80154
*/
81155
PHP_METHOD(Binary, getData)
@@ -117,18 +191,19 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Binary___construct, 0, 0, 2)
117191
ZEND_ARG_INFO(0, type)
118192
ZEND_END_ARG_INFO();
119193

120-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary_getData, 0, 0, 0)
194+
ZEND_BEGIN_ARG_INFO_EX(ai_Binary___set_state, 0, 0, 1)
195+
ZEND_ARG_ARRAY_INFO(0, properties, 0)
121196
ZEND_END_ARG_INFO();
122197

123-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary_getType, 0, 0, 0)
198+
ZEND_BEGIN_ARG_INFO_EX(ai_Binary_void, 0, 0, 0)
124199
ZEND_END_ARG_INFO();
125200

126-
127201
static zend_function_entry php_phongo_binary_me[] = {
128202
PHP_ME(Binary, __construct, ai_Binary___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
129-
PHP_ME(Binary, getData, ai_Binary_getData, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
130-
PHP_ME(Binary, getType, ai_Binary_getType, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
131-
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
203+
PHP_ME(Binary, __set_state, ai_Binary___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
204+
PHP_ME(Binary, getData, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
205+
PHP_ME(Binary, getType, ai_Binary_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
206+
PHP_ME(Binary, __wakeup, ai_Binary_void, ZEND_ACC_PUBLIC)
132207
PHP_FE_END
133208
};
134209

@@ -174,23 +249,43 @@ phongo_create_object_retval php_phongo_binary_create_object(zend_class_entry *cl
174249
#endif
175250
} /* }}} */
176251

177-
HashTable *php_phongo_binary_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
252+
HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{{ */
178253
{
179254
php_phongo_binary_t *intern;
255+
HashTable *props;
256+
257+
intern = Z_BINARY_OBJ_P(object);
258+
props = zend_std_get_properties(object TSRMLS_CC);
259+
260+
if (!intern->data) {
261+
return props;
262+
}
263+
180264
#if PHP_VERSION_ID >= 70000
181-
zval retval;
265+
{
266+
zval data, type;
267+
268+
ZVAL_STRINGL(&data, intern->data, intern->data_len);
269+
zend_hash_str_update(props, "data", sizeof("data")-1, &data);
270+
271+
ZVAL_LONG(&type, intern->type);
272+
zend_hash_str_update(props, "type", sizeof("type")-1, &type);
273+
}
182274
#else
183-
zval retval = zval_used_for_init;
184-
#endif
275+
{
276+
zval *data, *type;
185277

186-
intern = Z_BINARY_OBJ_P(object);
187-
*is_temp = 1;
188-
array_init_size(&retval, 2);
278+
MAKE_STD_ZVAL(data);
279+
ZVAL_STRINGL(data, intern->data, intern->data_len, 1);
280+
zend_hash_update(props, "data", sizeof("data"), &data, sizeof(data), NULL);
189281

190-
ADD_ASSOC_STRINGL(&retval, "data", intern->data, intern->data_len);
191-
ADD_ASSOC_LONG_EX(&retval, "type", intern->type);
282+
MAKE_STD_ZVAL(type);
283+
ZVAL_LONG(type, intern->type);
284+
zend_hash_update(props, "type", sizeof("type"), &type, sizeof(type), NULL);
285+
}
286+
#endif
192287

193-
return Z_ARRVAL(retval);
288+
return props;
194289
} /* }}} */
195290
/* }}} */
196291

@@ -203,12 +298,12 @@ PHP_MINIT_FUNCTION(Binary)
203298
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "Binary", php_phongo_binary_me);
204299
php_phongo_binary_ce = zend_register_internal_class(&ce TSRMLS_CC);
205300
php_phongo_binary_ce->create_object = php_phongo_binary_create_object;
206-
PHONGO_CE_INIT(php_phongo_binary_ce);
301+
PHONGO_CE_FINAL(php_phongo_binary_ce);
207302

208303
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_phongo_type_ce);
209304

210305
memcpy(&php_phongo_handler_binary, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
211-
php_phongo_handler_binary.get_debug_info = php_phongo_binary_get_debug_info;
306+
php_phongo_handler_binary.get_properties = php_phongo_binary_get_properties;
212307
#if PHP_VERSION_ID >= 70000
213308
php_phongo_handler_binary.free_obj = php_phongo_binary_free_object;
214309
php_phongo_handler_binary.offset = XtOffsetOf(php_phongo_binary_t, std);

0 commit comments

Comments
 (0)