Skip to content

Commit bee6b53

Browse files
committed
expect an array as STATS value and reply foreach key/value
1 parent 941ae83 commit bee6b53

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

php_memcached_server.c

+25-11
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ protocol_binary_response_status s_stat_handler (const void *cookie, const void *
506506
{
507507
zval params[3];
508508
protocol_binary_response_status retval = PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND;
509-
zval zcookie, zkey, zbody;
509+
zval zcookie, zkey, zstats;
510510

511511
if (!MEMC_HAS_CB(MEMC_SERVER_ON_STAT)) {
512512
return retval;
@@ -519,24 +519,38 @@ protocol_binary_response_status s_stat_handler (const void *cookie, const void *
519519
} else {
520520
ZVAL_NULL(&zkey);
521521
}
522-
ZVAL_NULL(&zbody);
523-
ZVAL_MAKE_REF(&zbody);
522+
ZVAL_NULL(&zstats);
523+
ZVAL_MAKE_REF(&zstats);
524524

525525
ZVAL_COPY(&params[0], &zcookie);
526526
ZVAL_COPY(&params[1], &zkey);
527-
ZVAL_COPY(&params[2], &zbody);
527+
ZVAL_COPY(&params[2], &zstats);
528528

529529
retval = s_invoke_php_callback (&MEMC_GET_CB(MEMC_SERVER_ON_STAT), params, 3);
530530

531531
if (retval == PROTOCOL_BINARY_RESPONSE_SUCCESS) {
532-
if (Z_TYPE(zbody) == IS_NULL) {
532+
if (Z_ISNULL(zstats)) {
533533
retval = response_handler(cookie, NULL, 0, NULL, 0);
534-
}
535-
else {
536-
if (Z_TYPE(zbody) != IS_STRING) {
537-
convert_to_string(&zbody);
534+
} else {
535+
zval *zarray = &zstats;
536+
zend_string *key;
537+
zval *val;
538+
539+
ZVAL_DEREF(zarray);
540+
if (Z_TYPE_P(zarray) != IS_ARRAY) {
541+
convert_to_array(zarray);
542+
}
543+
544+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zarray), key, val)
545+
{
546+
zend_string *val_str = zval_get_string(val);
547+
retval = response_handler(cookie, key->val, key->len, val_str->val, val_str->len);
548+
if (retval != PROTOCOL_BINARY_RESPONSE_SUCCESS) {
549+
break;
550+
}
551+
zend_string_release(val_str);
538552
}
539-
retval = response_handler(cookie, key, key_len, Z_STRVAL(zbody), (uint32_t) Z_STRLEN(zbody));
553+
ZEND_HASH_FOREACH_END();
540554
}
541555
}
542556

@@ -545,7 +559,7 @@ protocol_binary_response_status s_stat_handler (const void *cookie, const void *
545559
zval_ptr_dtor(&params[2]);
546560
zval_ptr_dtor (&zcookie);
547561
zval_ptr_dtor (&zkey);
548-
zval_ptr_dtor (&zbody);
562+
zval_ptr_dtor (&zstats);
549563
return retval;
550564
}
551565

tests/memcachedserver.phpt

+10-6
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,21 @@ array(1) {
6767
client_id=[%s]: Stat key=[]
6868
array(1) {
6969
["127.0.0.1:3434"]=>
70-
array(1) {
71-
[""]=>
72-
string(15) "Stat reply for "
70+
array(2) {
71+
["key"]=>
72+
string(0) ""
73+
["foo"]=>
74+
string(3) "bar"
7375
}
7476
}
7577
client_id=[%s]: Stat key=[foobar]
7678
array(1) {
7779
["127.0.0.1:3434"]=>
78-
array(1) {
79-
["foobar"]=>
80-
string(21) "Stat reply for foobar"
80+
array(2) {
81+
["key"]=>
82+
string(6) "foobar"
83+
["foo"]=>
84+
string(3) "bar"
8185
}
8286
}
8387
client_id=[%s]: Client quit

tests/server.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ function ($client_id, $key, $value, $flags, $expiration, $cas, &$result_cas) {
7777
});
7878

7979
$server->on (Memcached::ON_STAT,
80-
function ($client_id, $key, &$value) {
80+
function ($client_id, $key, array &$values = null) {
8181
echo "client_id=[$client_id]: Stat key=[$key]" . PHP_EOL;
82-
$value = "Stat reply for $key";
82+
$values = [
83+
"key" => $key,
84+
"foo" => "bar",
85+
];
8386
return Memcached::RESPONSE_SUCCESS;
8487
});
8588

0 commit comments

Comments
 (0)