Skip to content

Commit 12aae66

Browse files
committed
Merge branch 'pr93' of github.com:andypost/msgpack-php
2 parents 94e1e40 + 1166803 commit 12aae66

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

config.m4

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ Make sure that the comment is aligned:
55
[ --with-msgpack Include msgpack support])
66

77
if test "$PHP_MSGPACK" != "no"; then
8+
AC_MSG_CHECKING([for APC/APCU includes])
9+
if test -f "$phpincludedir/ext/apcu/apc_serializer.h"; then
10+
apc_inc_path="$phpincludedir"
11+
AC_MSG_RESULT([APCU in $apc_inc_path])
12+
AC_DEFINE(HAVE_APCU_SUPPORT,1,[Whether to enable apcu support])
13+
else
14+
AC_MSG_RESULT([not found])
15+
fi
16+
817
PHP_NEW_EXTENSION(msgpack, msgpack.c msgpack_pack.c msgpack_unpack.c msgpack_class.c msgpack_convert.c, $ext_shared)
918

1019
ifdef([PHP_INSTALL_HEADERS],

msgpack.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "ext/session/php_session.h" /* for php_session_register_serializer */
1414
#endif
1515

16+
#if defined(HAVE_APCU_SUPPORT)
17+
#include "ext/apcu/apc_serializer.h"
18+
#endif /* HAVE_APCU_SUPPORT */
19+
1620
#include "php_msgpack.h"
1721
#include "msgpack_pack.h"
1822
#include "msgpack_unpack.h"
@@ -54,6 +58,12 @@ PHP_INI_END()
5458
PS_SERIALIZER_FUNCS(msgpack);
5559
#endif
5660

61+
#if defined(HAVE_APCU_SUPPORT)
62+
/** Apc serializer function prototypes */
63+
static int APC_SERIALIZER_NAME(msgpack) (APC_SERIALIZER_ARGS);
64+
static int APC_UNSERIALIZER_NAME(msgpack) (APC_UNSERIALIZER_ARGS);
65+
#endif
66+
5767
static zend_function_entry msgpack_functions[] = {
5868
ZEND_FE(msgpack_serialize, arginfo_msgpack_serialize)
5969
ZEND_FE(msgpack_unserialize, arginfo_msgpack_unserialize)
@@ -88,6 +98,13 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
8898
php_session_register_serializer("msgpack", PS_SERIALIZER_ENCODE_NAME(msgpack), PS_SERIALIZER_DECODE_NAME(msgpack));
8999
#endif
90100

101+
#if defined(HAVE_APCU_SUPPORT)
102+
apc_register_serializer("msgpack",
103+
APC_SERIALIZER_NAME(msgpack),
104+
APC_UNSERIALIZER_NAME(msgpack),
105+
NULL);
106+
#endif
107+
91108
msgpack_init_class();
92109

93110
REGISTER_LONG_CONSTANT("MESSAGEPACK_OPT_PHPONLY",
@@ -109,6 +126,11 @@ static ZEND_MINFO_FUNCTION(msgpack) /* {{{ */ {
109126
php_info_print_table_row(2, "MessagePack Support", "enabled");
110127
#if HAVE_PHP_SESSION
111128
php_info_print_table_row(2, "Session Support", "enabled" );
129+
#endif
130+
#if defined(HAVE_APCU_SUPPORT)
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");
112134
#endif
113135
php_info_print_table_row(2, "extension Version", PHP_MSGPACK_VERSION);
114136
php_info_print_table_row(2, "header Version", MSGPACK_VERSION);
@@ -300,6 +322,30 @@ static ZEND_FUNCTION(msgpack_unserialize) /* {{{ */ {
300322
}
301323
/* }}} */
302324

325+
#if defined(HAVE_APCU_SUPPORT)
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;
337+
}
338+
/* }}} */
339+
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;
345+
}
346+
/* }}} */
347+
#endif
348+
303349
/*
304350
* Local variables:
305351
* tab-width: 4

tests/029.phpt

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

4545
MessagePack Support => enabled
4646
Session Support => enabled
47+
MessagePack APCu Serializer ABI => %s
4748
extension Version => %s
4849
header Version => %s
4950

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)