Skip to content

Commit 94e0649

Browse files
Merge branch 'hotfix/debug_object'
2 parents 6d14bf5 + 96374a9 commit 94e0649

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

library.c

+61
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,67 @@ PHP_REDIS_API void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *
10891089
}
10901090
}
10911091

1092+
/* Response for DEBUG object which is a formatted single line reply */
1093+
PHP_REDIS_API void redis_debug_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
1094+
zval *z_tab, void *ctx)
1095+
{
1096+
char *resp, *p, *p2, *p3, *p4;
1097+
int is_numeric, resp_len;
1098+
zval *z_result;
1099+
1100+
/* Add or return false if we can't read from the socket */
1101+
if((resp = redis_sock_read(redis_sock, &resp_len TSRMLS_CC))==NULL) {
1102+
IF_MULTI_OR_PIPELINE() {
1103+
add_next_index_bool(z_tab, 0);
1104+
return;
1105+
}
1106+
RETURN_FALSE;
1107+
}
1108+
1109+
MAKE_STD_ZVAL(z_result);
1110+
array_init(z_result);
1111+
1112+
/* Skip the '+' */
1113+
p = resp + 1;
1114+
1115+
/* <info>:<value> <info2:value2> ... */
1116+
while((p2 = strchr(p, ':'))!=NULL) {
1117+
/* Null terminate at the ':' */
1118+
*p2++ = '\0';
1119+
1120+
/* Null terminate at the space if we have one */
1121+
if((p3 = strchr(p2, ' '))!=NULL) {
1122+
*p3++ = '\0';
1123+
} else {
1124+
p3 = resp + resp_len;
1125+
}
1126+
1127+
is_numeric = 1;
1128+
for(p4=p2; *p4; ++p4) {
1129+
if(*p4 < '0' || *p4 > '9') {
1130+
is_numeric = 0;
1131+
break;
1132+
}
1133+
}
1134+
1135+
/* Add our value */
1136+
if(is_numeric) {
1137+
add_assoc_long(z_result, p, atol(p2));
1138+
} else {
1139+
add_assoc_string(z_result, p, p2, 1);
1140+
}
1141+
1142+
p = p3;
1143+
}
1144+
1145+
efree(resp);
1146+
1147+
IF_MULTI_OR_PIPELINE() {
1148+
add_next_index_zval(z_tab, z_result);
1149+
} else {
1150+
RETVAL_ZVAL(z_result, 0, 1);
1151+
}
1152+
}
10921153

10931154
/**
10941155
* redis_sock_create

library.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ PHP_REDIS_API void redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSoc
2121
PHP_REDIS_API void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2222
PHP_REDIS_API void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2323
PHP_REDIS_API void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
24+
PHP_REDIS_API void redis_debug_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2425
PHP_REDIS_API void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2526
PHP_REDIS_API void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2627
PHP_REDIS_API RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id, long retry_interval, zend_bool lazy_connect);

php_redis.h

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ PHP_METHOD(Redis, bitpos);
134134
PHP_METHOD(Redis, eval);
135135
PHP_METHOD(Redis, evalsha);
136136
PHP_METHOD(Redis, script);
137+
PHP_METHOD(Redis, debug);
137138
PHP_METHOD(Redis, dump);
138139
PHP_METHOD(Redis, restore);
139140
PHP_METHOD(Redis, migrate);

redis.c

+32
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static zend_function_entry redis_functions[] = {
240240
PHP_ME(Redis, evalsha, NULL, ZEND_ACC_PUBLIC)
241241
PHP_ME(Redis, script, NULL, ZEND_ACC_PUBLIC)
242242

243+
PHP_ME(Redis, debug, NULL, ZEND_ACC_PUBLIC)
243244
PHP_ME(Redis, dump, NULL, ZEND_ACC_PUBLIC)
244245
PHP_ME(Redis, restore, NULL, ZEND_ACC_PUBLIC)
245246
PHP_ME(Redis, migrate, NULL, ZEND_ACC_PUBLIC)
@@ -6564,6 +6565,37 @@ PHP_METHOD(Redis, dump) {
65646565
REDIS_PROCESS_RESPONSE(redis_ping_response);
65656566
}
65666567

6568+
/* {{{ proto Redis::DEBUG(string key) */
6569+
PHP_METHOD(Redis, debug) {
6570+
zval *object;
6571+
RedisSock *redis_sock;
6572+
char *cmd, *key;
6573+
int cmd_len, key_len, key_free;
6574+
6575+
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
6576+
&object, redis_ce, &key, &key_len)==FAILURE)
6577+
{
6578+
RETURN_FALSE;
6579+
}
6580+
6581+
/* Grab our socket */
6582+
if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0)<0) {
6583+
RETURN_FALSE;
6584+
}
6585+
6586+
/* Prefix key, format command */
6587+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
6588+
cmd_len = redis_cmd_format_static(&cmd, "DEBUG", "ss", "OBJECT", sizeof("OBJECT")-1, key, key_len);
6589+
if(key_free) efree(key);
6590+
6591+
/* Kick it off */
6592+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
6593+
IF_ATOMIC() {
6594+
redis_debug_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
6595+
}
6596+
REDIS_PROCESS_RESPONSE(redis_debug_response);
6597+
}
6598+
65676599
/*
65686600
* {{{ proto Redis::restore(ttl, key, value)
65696601
*/

tests/TestRedis.php

+6
Original file line numberDiff line numberDiff line change
@@ -4237,6 +4237,12 @@ private function checkSerializer($mode) {
42374237
$this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === Redis::SERIALIZER_NONE); // get ok
42384238
}
42394239

4240+
public function testDebug() {
4241+
$this->redis->set('foo', 0);
4242+
$arr_info = $this->redis->debug('foo');
4243+
$this->assertTrue(isset($arr_info['encoding']) && $arr_info['encoding']=='int');
4244+
}
4245+
42404246
public function testDumpRestore() {
42414247

42424248
if (version_compare($this->version, "2.5.0", "lt")) {

0 commit comments

Comments
 (0)