Skip to content

Commit 1166803

Browse files
committed
Use public API and add test
1 parent 0a53e9f commit 1166803

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

msgpack.c

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
9999
#endif
100100

101101
#if defined(HAVE_APCU_SUPPORT)
102-
apc_register_serializer("msgpack",
103-
APC_SERIALIZER_NAME(msgpack),
104-
APC_UNSERIALIZER_NAME(msgpack),
105-
NULL TSRMLS_CC);
102+
apc_register_serializer("msgpack",
103+
APC_SERIALIZER_NAME(msgpack),
104+
APC_UNSERIALIZER_NAME(msgpack),
105+
NULL);
106106
#endif
107107

108108
msgpack_init_class();
@@ -128,7 +128,9 @@ static ZEND_MINFO_FUNCTION(msgpack) /* {{{ */ {
128128
php_info_print_table_row(2, "Session Support", "enabled" );
129129
#endif
130130
#if defined(HAVE_APCU_SUPPORT)
131-
php_info_print_table_row(2, "APCu Serializer Support", "enabled" );
131+
php_info_print_table_row(2, "MessagePack APCu Serializer ABI", APC_SERIALIZER_ABI);
132+
#else
133+
php_info_print_table_row(2, "MessagePack APCu Serializer ABI", "no");
132134
#endif
133135
php_info_print_table_row(2, "extension Version", PHP_MSGPACK_VERSION);
134136
php_info_print_table_row(2, "header Version", MSGPACK_VERSION);
@@ -321,49 +323,25 @@ static ZEND_FUNCTION(msgpack_unserialize) /* {{{ */ {
321323
/* }}} */
322324

323325
#if defined(HAVE_APCU_SUPPORT)
324-
/* {{{ apc_serialize function */
325-
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) {
326-
(void)config;
327-
328-
smart_str res = {0};
329-
msgpack_serialize_data_t var_hash;
330-
331-
msgpack_serialize_var_init(&var_hash);
332-
msgpack_serialize_zval(&res, (zval *) value, var_hash);
333-
msgpack_serialize_var_destroy(&var_hash);
334-
335-
smart_str_0(&res);
336-
337-
*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
338-
*buf_len = ZSTR_LEN(res.s);
339-
340-
return 1;
326+
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) /* {{{ */ {
327+
smart_str res = {0};
328+
php_msgpack_serialize(&res, (zval *) value);
329+
330+
if (res.s) {
331+
smart_str_0(&res);
332+
*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
333+
*buf_len = ZSTR_LEN(res.s);
334+
return 1;
335+
}
336+
return 0;
341337
}
342338
/* }}} */
343-
/* {{{ apc_unserialize function */
344-
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) {
345-
(void)config;
346-
347-
int ret;
348-
msgpack_unpack_t mp;
349-
msgpack_unserialize_data_t var_hash;
350-
size_t off = 0;
351339

352-
template_init(&mp);
353-
354-
msgpack_unserialize_var_init(&var_hash);
355-
356-
mp.user.retval = value;
357-
mp.user.var_hash = &var_hash;
358-
359-
ret = template_execute(&mp, (char *) buf, buf_len, &off);
360-
if (Z_TYPE_P(mp.user.retval) == IS_REFERENCE) {
361-
ZVAL_DEREF(mp.user.retval);
362-
}
363-
364-
msgpack_unserialize_var_destroy(&var_hash, 0);
365-
366-
return ret == MSGPACK_UNPACK_EXTRA_BYTES || ret == MSGPACK_UNPACK_SUCCESS;
340+
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) /* {{{ */ {
341+
if (buf_len > 0 && php_msgpack_unserialize(value, buf, buf_len) == SUCCESS) {
342+
return 1;
343+
}
344+
return 0;
367345
}
368346
/* }}} */
369347
#endif

tests/029.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ msgpack
4444

4545
MessagePack Support => enabled
4646
Session Support => enabled
47-
APCu Serializer Support => enabled
47+
MessagePack APCu Serializer ABI => %s
4848
extension Version => %s
4949
header Version => %s
5050

tests/apcu.phpt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
APCu serialization
3+
--INI--
4+
apc.enabled=1
5+
apc.enable_cli=1
6+
apc.serializer=msgpack
7+
--SKIPIF--
8+
<?php
9+
if (!extension_loaded("msgpack")) print "skip";
10+
if (!extension_loaded("apcu")) {
11+
echo "skip needs APCu enabled";
12+
}
13+
?>
14+
--FILE--
15+
<?php
16+
echo ini_get('apc.serializer'), "\n";
17+
18+
apcu_store('foo', 100);
19+
var_dump(apcu_fetch('foo'));
20+
21+
$foo = 'hello world';
22+
23+
apcu_store('foo', $foo);
24+
var_dump(apcu_fetch('foo'));
25+
26+
apcu_store('foo\x00bar', $foo);
27+
var_dump(apcu_fetch('foo\x00bar'));
28+
29+
apcu_store('foo', ['foo' => $foo]);
30+
var_dump(apcu_fetch('foo'));
31+
32+
class Foo {
33+
public $int = 10;
34+
protected $array = [];
35+
private $string = 'foo';
36+
}
37+
38+
$a = new Foo;
39+
apcu_store('foo', $a);
40+
unset($a);
41+
var_dump(apcu_fetch('foo'));
42+
43+
?>
44+
===DONE===
45+
--EXPECT--
46+
msgpack
47+
int(100)
48+
string(11) "hello world"
49+
string(11) "hello world"
50+
array(1) {
51+
["foo"]=>
52+
string(11) "hello world"
53+
}
54+
object(Foo)#1 (3) {
55+
["int"]=>
56+
int(10)
57+
["array":protected]=>
58+
array(0) {
59+
}
60+
["string":"Foo":private]=>
61+
string(3) "foo"
62+
}
63+
===DONE===

0 commit comments

Comments
 (0)