Skip to content

Commit 390f768

Browse files
andypostm6w6
authored andcommitted
Use public API and add test
1 parent ed1edfe commit 390f768

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

msgpack.c

+23-45
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
103103
#endif
104104

105105
#if defined(HAVE_APCU_SUPPORT)
106-
apc_register_serializer("msgpack",
107-
APC_SERIALIZER_NAME(msgpack),
108-
APC_UNSERIALIZER_NAME(msgpack),
109-
NULL TSRMLS_CC);
106+
apc_register_serializer("msgpack",
107+
APC_SERIALIZER_NAME(msgpack),
108+
APC_UNSERIALIZER_NAME(msgpack),
109+
NULL);
110110
#endif
111111

112112
msgpack_init_class();
@@ -134,7 +134,9 @@ static ZEND_MINFO_FUNCTION(msgpack) /* {{{ */ {
134134
php_info_print_table_row(2, "Session Support", "enabled" );
135135
#endif
136136
#if defined(HAVE_APCU_SUPPORT)
137-
php_info_print_table_row(2, "APCu Serializer Support", "enabled" );
137+
php_info_print_table_row(2, "MessagePack APCu Serializer ABI", APC_SERIALIZER_ABI);
138+
#else
139+
php_info_print_table_row(2, "MessagePack APCu Serializer ABI", "no");
138140
#endif
139141
php_info_print_table_row(2, "extension Version", PHP_MSGPACK_VERSION);
140142
php_info_print_table_row(2, "header Version", MSGPACK_VERSION);
@@ -327,49 +329,25 @@ static ZEND_FUNCTION(msgpack_unserialize) /* {{{ */ {
327329
/* }}} */
328330

329331
#if defined(HAVE_APCU_SUPPORT)
330-
/* {{{ apc_serialize function */
331-
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) {
332-
(void)config;
333-
334-
smart_str res = {0};
335-
msgpack_serialize_data_t var_hash;
336-
337-
msgpack_serialize_var_init(&var_hash);
338-
msgpack_serialize_zval(&res, (zval *) value, var_hash);
339-
msgpack_serialize_var_destroy(&var_hash);
340-
341-
smart_str_0(&res);
342-
343-
*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
344-
*buf_len = ZSTR_LEN(res.s);
345-
346-
return 1;
332+
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) /* {{{ */ {
333+
smart_str res = {0};
334+
php_msgpack_serialize(&res, (zval *) value);
335+
336+
if (res.s) {
337+
smart_str_0(&res);
338+
*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
339+
*buf_len = ZSTR_LEN(res.s);
340+
return 1;
341+
}
342+
return 0;
347343
}
348344
/* }}} */
349-
/* {{{ apc_unserialize function */
350-
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) {
351-
(void)config;
352-
353-
int ret;
354-
msgpack_unpack_t mp;
355-
msgpack_unserialize_data_t var_hash;
356-
size_t off = 0;
357345

358-
template_init(&mp);
359-
360-
msgpack_unserialize_var_init(&var_hash);
361-
362-
mp.user.retval = value;
363-
mp.user.var_hash = &var_hash;
364-
365-
ret = template_execute(&mp, (char *) buf, buf_len, &off);
366-
if (Z_TYPE_P(mp.user.retval) == IS_REFERENCE) {
367-
ZVAL_DEREF(mp.user.retval);
368-
}
369-
370-
msgpack_unserialize_var_destroy(&var_hash, 0);
371-
372-
return ret == MSGPACK_UNPACK_EXTRA_BYTES || ret == MSGPACK_UNPACK_SUCCESS;
346+
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) /* {{{ */ {
347+
if (buf_len > 0 && php_msgpack_unserialize(value, buf, buf_len) == SUCCESS) {
348+
return 1;
349+
}
350+
return 0;
373351
}
374352
/* }}} */
375353
#endif

tests/029.phpt

+1-1
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

+63
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)