Skip to content

Commit

Permalink
Use public API and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
andypost authored and m6w6 committed Aug 20, 2024
1 parent ed1edfe commit 390f768
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 46 deletions.
68 changes: 23 additions & 45 deletions msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
#endif

#if defined(HAVE_APCU_SUPPORT)
apc_register_serializer("msgpack",
APC_SERIALIZER_NAME(msgpack),
APC_UNSERIALIZER_NAME(msgpack),
NULL TSRMLS_CC);
apc_register_serializer("msgpack",
APC_SERIALIZER_NAME(msgpack),
APC_UNSERIALIZER_NAME(msgpack),
NULL);
#endif

msgpack_init_class();
Expand Down Expand Up @@ -134,7 +134,9 @@ static ZEND_MINFO_FUNCTION(msgpack) /* {{{ */ {
php_info_print_table_row(2, "Session Support", "enabled" );
#endif
#if defined(HAVE_APCU_SUPPORT)
php_info_print_table_row(2, "APCu Serializer Support", "enabled" );
php_info_print_table_row(2, "MessagePack APCu Serializer ABI", APC_SERIALIZER_ABI);
#else
php_info_print_table_row(2, "MessagePack APCu Serializer ABI", "no");
#endif
php_info_print_table_row(2, "extension Version", PHP_MSGPACK_VERSION);
php_info_print_table_row(2, "header Version", MSGPACK_VERSION);
Expand Down Expand Up @@ -327,49 +329,25 @@ static ZEND_FUNCTION(msgpack_unserialize) /* {{{ */ {
/* }}} */

#if defined(HAVE_APCU_SUPPORT)
/* {{{ apc_serialize function */
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) {
(void)config;

smart_str res = {0};
msgpack_serialize_data_t var_hash;

msgpack_serialize_var_init(&var_hash);
msgpack_serialize_zval(&res, (zval *) value, var_hash);
msgpack_serialize_var_destroy(&var_hash);

smart_str_0(&res);

*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
*buf_len = ZSTR_LEN(res.s);

return 1;
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) /* {{{ */ {
smart_str res = {0};
php_msgpack_serialize(&res, (zval *) value);

if (res.s) {
smart_str_0(&res);
*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
*buf_len = ZSTR_LEN(res.s);
return 1;
}
return 0;
}
/* }}} */
/* {{{ apc_unserialize function */
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) {
(void)config;

int ret;
msgpack_unpack_t mp;
msgpack_unserialize_data_t var_hash;
size_t off = 0;

template_init(&mp);

msgpack_unserialize_var_init(&var_hash);

mp.user.retval = value;
mp.user.var_hash = &var_hash;

ret = template_execute(&mp, (char *) buf, buf_len, &off);
if (Z_TYPE_P(mp.user.retval) == IS_REFERENCE) {
ZVAL_DEREF(mp.user.retval);
}

msgpack_unserialize_var_destroy(&var_hash, 0);

return ret == MSGPACK_UNPACK_EXTRA_BYTES || ret == MSGPACK_UNPACK_SUCCESS;
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) /* {{{ */ {
if (buf_len > 0 && php_msgpack_unserialize(value, buf, buf_len) == SUCCESS) {
return 1;
}
return 0;
}
/* }}} */
#endif
Expand Down
2 changes: 1 addition & 1 deletion tests/029.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ msgpack

MessagePack Support => enabled
Session Support => enabled
APCu Serializer Support => enabled
MessagePack APCu Serializer ABI => %s
extension Version => %s
header Version => %s

Expand Down
63 changes: 63 additions & 0 deletions tests/apcu.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--TEST--
APCu serialization
--INI--
apc.enabled=1
apc.enable_cli=1
apc.serializer=msgpack
--SKIPIF--
<?php
if (!extension_loaded("msgpack")) print "skip";
if (!extension_loaded("apcu")) {
echo "skip needs APCu enabled";
}
?>
--FILE--
<?php
echo ini_get('apc.serializer'), "\n";

apcu_store('foo', 100);
var_dump(apcu_fetch('foo'));

$foo = 'hello world';

apcu_store('foo', $foo);
var_dump(apcu_fetch('foo'));

apcu_store('foo\x00bar', $foo);
var_dump(apcu_fetch('foo\x00bar'));

apcu_store('foo', ['foo' => $foo]);
var_dump(apcu_fetch('foo'));

class Foo {
public $int = 10;
protected $array = [];
private $string = 'foo';
}

$a = new Foo;
apcu_store('foo', $a);
unset($a);
var_dump(apcu_fetch('foo'));

?>
===DONE===
--EXPECT--
msgpack
int(100)
string(11) "hello world"
string(11) "hello world"
array(1) {
["foo"]=>
string(11) "hello world"
}
object(Foo)#1 (3) {
["int"]=>
int(10)
["array":protected]=>
array(0) {
}
["string":"Foo":private]=>
string(3) "foo"
}
===DONE===

0 comments on commit 390f768

Please sign in to comment.