Skip to content

Commit bd10948

Browse files
authored
PHPC-1849: Handle deprecation of Serializable in PHP 8.1 (#1246)
* Add __serialize and __unserialize methods to Serializable classes These methods will be used on PHP 7.4+ when available. Implementing them is required to suppress a deprecation notice on PHP 8.1+. This commit also renames is_debug parameter for get_properties_hash utility functions (and related macros) to more accurately reflect its purpose of allocating a new HashTable instead of using an internal props table. * Explicitly handle serialize output in php_phongo_cursorid_get_properties_hash * Explicitly handle serialize output in php_phongo_writeconcern_get_properties_hash * Note maxStalenessSeconds handling in php_phongo_readpreference_get_properties_hash * CursorId should always serialize id as string
1 parent 7a51641 commit bd10948

File tree

123 files changed

+3258
-379
lines changed

Some content is hidden

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

123 files changed

+3258
-379
lines changed

php_phongo.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,26 @@ bool php_phongo_manager_unregister(php_phongo_manager_t* manager);
182182
} while (0)
183183
#endif
184184

185-
#define PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, size) \
186-
do { \
187-
if (is_debug) { \
188-
ALLOC_HASHTABLE(props); \
189-
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
190-
} else if ((intern)->properties) { \
191-
(props) = (intern)->properties; \
192-
} else { \
193-
ALLOC_HASHTABLE(props); \
194-
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
195-
(intern)->properties = (props); \
196-
} \
185+
#define PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_temp, intern, props, size) \
186+
do { \
187+
if (is_temp) { \
188+
ALLOC_HASHTABLE(props); \
189+
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
190+
} else if ((intern)->properties) { \
191+
(props) = (intern)->properties; \
192+
} else { \
193+
ALLOC_HASHTABLE(props); \
194+
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
195+
(intern)->properties = (props); \
196+
} \
197197
} while (0)
198198

199-
#define PHONGO_GET_PROPERTY_HASH_FREE_PROPS(is_debug, props) \
200-
do { \
201-
if (is_debug) { \
202-
zend_hash_destroy((props)); \
203-
FREE_HASHTABLE(props); \
204-
} \
199+
#define PHONGO_GET_PROPERTY_HASH_FREE_PROPS(is_temp, props) \
200+
do { \
201+
if (is_temp) { \
202+
zend_hash_destroy((props)); \
203+
FREE_HASHTABLE(props); \
204+
} \
205205
} while (0)
206206

207207
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME(zv) (Z_TYPE(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE(zv)->name) : zend_get_type_by_const(Z_TYPE(zv)))

src/BSON/Binary.c

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ static bool php_phongo_binary_init_from_hash(php_phongo_binary_t* intern, HashTa
6969
return false;
7070
} /* }}} */
7171

72+
static HashTable* php_phongo_binary_get_properties_hash(phongo_compat_object_handler_type* object, bool is_temp) /* {{{ */
73+
{
74+
php_phongo_binary_t* intern;
75+
HashTable* props;
76+
77+
intern = Z_OBJ_BINARY(PHONGO_COMPAT_GET_OBJ(object));
78+
79+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_temp, intern, props, 2);
80+
81+
if (!intern->data) {
82+
return props;
83+
}
84+
85+
{
86+
zval data, type;
87+
88+
ZVAL_STRINGL(&data, intern->data, intern->data_len);
89+
zend_hash_str_update(props, "data", sizeof("data") - 1, &data);
90+
91+
ZVAL_LONG(&type, intern->type);
92+
zend_hash_str_update(props, "type", sizeof("type") - 1, &type);
93+
}
94+
95+
return props;
96+
} /* }}} */
97+
7298
/* {{{ proto void MongoDB\BSON\Binary::__construct(string $data, int $type)
7399
Construct a new BSON binary type */
74100
static PHP_METHOD(Binary, __construct)
@@ -270,6 +296,28 @@ static PHP_METHOD(Binary, unserialize)
270296
zval_ptr_dtor(&props);
271297
} /* }}} */
272298

299+
/* {{{ proto array MongoDB\Driver\Binary::__serialize()
300+
*/
301+
static PHP_METHOD(Binary, __serialize)
302+
{
303+
PHONGO_PARSE_PARAMETERS_NONE();
304+
305+
RETURN_ARR(php_phongo_binary_get_properties_hash(PHONGO_COMPAT_OBJ_P(getThis()), true));
306+
} /* }}} */
307+
308+
/* {{{ proto void MongoDB\Driver\Binary::__unserialize(array $data)
309+
*/
310+
static PHP_METHOD(Binary, __unserialize)
311+
{
312+
zval* data;
313+
314+
PHONGO_PARSE_PARAMETERS_START(1, 1)
315+
Z_PARAM_ARRAY(data)
316+
PHONGO_PARSE_PARAMETERS_END();
317+
318+
php_phongo_binary_init_from_hash(Z_BINARY_OBJ_P(getThis()), Z_ARRVAL_P(data));
319+
} /* }}} */
320+
273321
/* {{{ MongoDB\BSON\Binary function entries */
274322
/* clang-format off */
275323
ZEND_BEGIN_ARG_INFO_EX(ai_Binary___construct, 0, 0, 2)
@@ -281,6 +329,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Binary___set_state, 0, 0, 1)
281329
ZEND_ARG_ARRAY_INFO(0, properties, 0)
282330
ZEND_END_ARG_INFO()
283331

332+
ZEND_BEGIN_ARG_INFO_EX(ai_Binary___unserialize, 0, 0, 1)
333+
ZEND_ARG_ARRAY_INFO(0, data, 0)
334+
ZEND_END_ARG_INFO()
335+
284336
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(ai_Binary_jsonSerialize, 0, 0, IS_ARRAY, 0)
285337
ZEND_END_ARG_INFO()
286338

@@ -293,8 +345,10 @@ ZEND_END_ARG_INFO()
293345

294346
static zend_function_entry php_phongo_binary_me[] = {
295347
PHP_ME(Binary, __construct, ai_Binary___construct, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
348+
PHP_ME(Binary, __serialize, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
296349
PHP_ME(Binary, __set_state, ai_Binary___set_state, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
297350
PHP_ME(Binary, __toString, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
351+
PHP_ME(Binary, __unserialize, ai_Binary___unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
298352
PHP_ME(Binary, jsonSerialize, ai_Binary_jsonSerialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
299353
PHP_ME(Binary, serialize, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
300354
PHP_ME(Binary, unserialize, ai_Binary_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
@@ -377,32 +431,6 @@ static int php_phongo_binary_compare_objects(zval* o1, zval* o2) /* {{{ */
377431
return zend_binary_strcmp(intern1->data, intern1->data_len, intern2->data, intern2->data_len);
378432
} /* }}} */
379433

380-
static HashTable* php_phongo_binary_get_properties_hash(phongo_compat_object_handler_type* object, bool is_debug) /* {{{ */
381-
{
382-
php_phongo_binary_t* intern;
383-
HashTable* props;
384-
385-
intern = Z_OBJ_BINARY(PHONGO_COMPAT_GET_OBJ(object));
386-
387-
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);
388-
389-
if (!intern->data) {
390-
return props;
391-
}
392-
393-
{
394-
zval data, type;
395-
396-
ZVAL_STRINGL(&data, intern->data, intern->data_len);
397-
zend_hash_str_update(props, "data", sizeof("data") - 1, &data);
398-
399-
ZVAL_LONG(&type, intern->type);
400-
zend_hash_str_update(props, "type", sizeof("type") - 1, &type);
401-
}
402-
403-
return props;
404-
} /* }}} */
405-
406434
static HashTable* php_phongo_binary_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */
407435
{
408436
*is_temp = 1;

src/BSON/DBPointer.c

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ static bool php_phongo_dbpointer_init_from_hash(php_phongo_dbpointer_t* intern,
6868
return false;
6969
} /* }}} */
7070

71+
HashTable* php_phongo_dbpointer_get_properties_hash(phongo_compat_object_handler_type* object, bool is_temp) /* {{{ */
72+
{
73+
php_phongo_dbpointer_t* intern;
74+
HashTable* props;
75+
76+
intern = Z_OBJ_DBPOINTER(PHONGO_COMPAT_GET_OBJ(object));
77+
78+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_temp, intern, props, 2);
79+
80+
if (!intern->ref) {
81+
return props;
82+
}
83+
84+
{
85+
zval ref, id;
86+
87+
ZVAL_STRING(&ref, intern->ref);
88+
ZVAL_STRING(&id, intern->id);
89+
zend_hash_str_update(props, "ref", sizeof("ref") - 1, &ref);
90+
zend_hash_str_update(props, "id", sizeof("id") - 1, &id);
91+
}
92+
93+
return props;
94+
} /* }}} */
95+
7196
/* {{{ proto string MongoDB\BSON\DBPointer::__toString()
7297
Return the DBPointer's namespace string and ObjectId. */
7398
static PHP_METHOD(DBPointer, __toString)
@@ -187,8 +212,34 @@ static PHP_METHOD(DBPointer, unserialize)
187212
zval_ptr_dtor(&props);
188213
} /* }}} */
189214

215+
/* {{{ proto array MongoDB\Driver\DBPointer::__serialize()
216+
*/
217+
static PHP_METHOD(DBPointer, __serialize)
218+
{
219+
PHONGO_PARSE_PARAMETERS_NONE();
220+
221+
RETURN_ARR(php_phongo_dbpointer_get_properties_hash(PHONGO_COMPAT_OBJ_P(getThis()), true));
222+
} /* }}} */
223+
224+
/* {{{ proto void MongoDB\Driver\DBPointer::__unserialize(array $data)
225+
*/
226+
static PHP_METHOD(DBPointer, __unserialize)
227+
{
228+
zval* data;
229+
230+
PHONGO_PARSE_PARAMETERS_START(1, 1)
231+
Z_PARAM_ARRAY(data)
232+
PHONGO_PARSE_PARAMETERS_END();
233+
234+
php_phongo_dbpointer_init_from_hash(Z_DBPOINTER_OBJ_P(getThis()), Z_ARRVAL_P(data));
235+
} /* }}} */
236+
190237
/* {{{ MongoDB\BSON\DBPointer function entries */
191238
/* clang-format off */
239+
ZEND_BEGIN_ARG_INFO_EX(ai_DBPointer___unserialize, 0, 0, 1)
240+
ZEND_ARG_ARRAY_INFO(0, data, 0)
241+
ZEND_END_ARG_INFO()
242+
192243
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(ai_DBPointer_jsonSerialize, 0, 0, IS_ARRAY, 0)
193244
ZEND_END_ARG_INFO()
194245

@@ -201,7 +252,9 @@ ZEND_END_ARG_INFO()
201252

202253
static zend_function_entry php_phongo_dbpointer_me[] = {
203254
/* __set_state intentionally missing */
255+
PHP_ME(DBPointer, __serialize, ai_DBPointer_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
204256
PHP_ME(DBPointer, __toString, ai_DBPointer_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
257+
PHP_ME(DBPointer, __unserialize, ai_DBPointer___unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
205258
PHP_ME(DBPointer, jsonSerialize, ai_DBPointer_jsonSerialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
206259
PHP_ME(DBPointer, serialize, ai_DBPointer_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
207260
PHP_ME(DBPointer, unserialize, ai_DBPointer_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
@@ -279,31 +332,6 @@ static int php_phongo_dbpointer_compare_objects(zval* o1, zval* o2) /* {{{ */
279332
return strcmp(intern1->id, intern2->id);
280333
} /* }}} */
281334

282-
HashTable* php_phongo_dbpointer_get_properties_hash(phongo_compat_object_handler_type* object, bool is_debug) /* {{{ */
283-
{
284-
php_phongo_dbpointer_t* intern;
285-
HashTable* props;
286-
287-
intern = Z_OBJ_DBPOINTER(PHONGO_COMPAT_GET_OBJ(object));
288-
289-
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);
290-
291-
if (!intern->ref) {
292-
return props;
293-
}
294-
295-
{
296-
zval ref, id;
297-
298-
ZVAL_STRING(&ref, intern->ref);
299-
ZVAL_STRING(&id, intern->id);
300-
zend_hash_str_update(props, "ref", sizeof("ref") - 1, &ref);
301-
zend_hash_str_update(props, "id", sizeof("id") - 1, &id);
302-
}
303-
304-
return props;
305-
} /* }}} */
306-
307335
static HashTable* php_phongo_dbpointer_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */
308336
{
309337
*is_temp = 1;

src/BSON/Decimal128.c

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t* intern
5656
return false;
5757
} /* }}} */
5858

59+
static HashTable* php_phongo_decimal128_get_properties_hash(phongo_compat_object_handler_type* object, bool is_temp) /* {{{ */
60+
{
61+
php_phongo_decimal128_t* intern;
62+
HashTable* props;
63+
char outbuf[BSON_DECIMAL128_STRING] = "";
64+
65+
intern = Z_OBJ_DECIMAL128(PHONGO_COMPAT_GET_OBJ(object));
66+
67+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_temp, intern, props, 1);
68+
69+
if (!intern->initialized) {
70+
return props;
71+
}
72+
73+
bson_decimal128_to_string(&intern->decimal, outbuf);
74+
75+
{
76+
zval dec;
77+
78+
ZVAL_STRING(&dec, outbuf);
79+
zend_hash_str_update(props, "dec", sizeof("dec") - 1, &dec);
80+
}
81+
82+
return props;
83+
} /* }}} */
84+
5985
/* {{{ proto void MongoDB\BSON\Decimal128::__construct(string $value)
6086
Construct a new BSON Decimal128 type */
6187
static PHP_METHOD(Decimal128, __construct)
@@ -214,6 +240,28 @@ static PHP_METHOD(Decimal128, unserialize)
214240
zval_ptr_dtor(&props);
215241
} /* }}} */
216242

243+
/* {{{ proto array MongoDB\Driver\Decimal128::__serialize()
244+
*/
245+
static PHP_METHOD(Decimal128, __serialize)
246+
{
247+
PHONGO_PARSE_PARAMETERS_NONE();
248+
249+
RETURN_ARR(php_phongo_decimal128_get_properties_hash(PHONGO_COMPAT_OBJ_P(getThis()), true));
250+
} /* }}} */
251+
252+
/* {{{ proto void MongoDB\Driver\Decimal128::__unserialize(array $data)
253+
*/
254+
static PHP_METHOD(Decimal128, __unserialize)
255+
{
256+
zval* data;
257+
258+
PHONGO_PARSE_PARAMETERS_START(1, 1)
259+
Z_PARAM_ARRAY(data)
260+
PHONGO_PARSE_PARAMETERS_END();
261+
262+
php_phongo_decimal128_init_from_hash(Z_DECIMAL128_OBJ_P(getThis()), Z_ARRVAL_P(data));
263+
} /* }}} */
264+
217265
/* {{{ MongoDB\BSON\Decimal128 function entries */
218266
/* clang-format off */
219267
ZEND_BEGIN_ARG_INFO_EX(ai_Decimal128___construct, 0, 0, 1)
@@ -224,6 +272,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Decimal128___set_state, 0, 0, 1)
224272
ZEND_ARG_ARRAY_INFO(0, properties, 0)
225273
ZEND_END_ARG_INFO()
226274

275+
ZEND_BEGIN_ARG_INFO_EX(ai_Decimal128___unserialize, 0, 0, 1)
276+
ZEND_ARG_ARRAY_INFO(0, data, 0)
277+
ZEND_END_ARG_INFO()
278+
227279
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(ai_Decimal128_jsonSerialize, 0, 0, IS_ARRAY, 0)
228280
ZEND_END_ARG_INFO()
229281

@@ -236,8 +288,10 @@ ZEND_END_ARG_INFO()
236288

237289
static zend_function_entry php_phongo_decimal128_me[] = {
238290
PHP_ME(Decimal128, __construct, ai_Decimal128___construct, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
291+
PHP_ME(Decimal128, __serialize, ai_Decimal128_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
239292
PHP_ME(Decimal128, __set_state, ai_Decimal128___set_state, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
240293
PHP_ME(Decimal128, __toString, ai_Decimal128_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
294+
PHP_ME(Decimal128, __unserialize, ai_Decimal128___unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
241295
PHP_ME(Decimal128, jsonSerialize, ai_Decimal128_jsonSerialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
242296
PHP_ME(Decimal128, serialize, ai_Decimal128_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
243297
PHP_ME(Decimal128, unserialize, ai_Decimal128_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
@@ -294,32 +348,6 @@ static zend_object* php_phongo_decimal128_clone_object(phongo_compat_object_hand
294348
return new_object;
295349
} /* }}} */
296350

297-
static HashTable* php_phongo_decimal128_get_properties_hash(phongo_compat_object_handler_type* object, bool is_debug) /* {{{ */
298-
{
299-
php_phongo_decimal128_t* intern;
300-
HashTable* props;
301-
char outbuf[BSON_DECIMAL128_STRING] = "";
302-
303-
intern = Z_OBJ_DECIMAL128(PHONGO_COMPAT_GET_OBJ(object));
304-
305-
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 1);
306-
307-
if (!intern->initialized) {
308-
return props;
309-
}
310-
311-
bson_decimal128_to_string(&intern->decimal, outbuf);
312-
313-
{
314-
zval dec;
315-
316-
ZVAL_STRING(&dec, outbuf);
317-
zend_hash_str_update(props, "dec", sizeof("dec") - 1, &dec);
318-
}
319-
320-
return props;
321-
} /* }}} */
322-
323351
static HashTable* php_phongo_decimal128_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */
324352
{
325353
*is_temp = 1;

0 commit comments

Comments
 (0)