Skip to content

Commit e914485

Browse files
Merge branch 'hotfix/inspection_methods' into develop
Conflicts: php_redis.h redis.c
2 parents 11a1195 + ef79232 commit e914485

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed

README.markdown

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ You can send comments, patches, questions [here on github](https://github.com/ni
2626
* [Pub/sub](#pubsub)
2727
* [Transactions](#transactions)
2828
* [Scripting](#scripting)
29+
* [Introspection](#introspection)
2930

3031
-----
3132

@@ -2986,3 +2987,88 @@ serializing values, and you return something from redis in a LUA script that is
29862987
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
29872988
$redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return Array(1,2,3)
29882989
~~~
2990+
2991+
2992+
2993+
## Introspection
2994+
2995+
### IsConnected
2996+
-----
2997+
_**Description**_: A method to determine if a phpredis object thinks it's connected to a server
2998+
2999+
##### *Parameters*
3000+
None
3001+
3002+
##### *Return value*
3003+
*Boolean* Returns TRUE if phpredis thinks it's connected and FALSE if not
3004+
3005+
### GetHost
3006+
-----
3007+
_**Description**_: Retreive our host or unix socket that we're connected to
3008+
3009+
##### *Parameters*
3010+
None
3011+
3012+
##### *Return value*
3013+
*Mixed* The host or unix socket we're connected to or FALSE if we're not connected
3014+
3015+
3016+
### GetPort
3017+
-----
3018+
_**Description**_: Get the port we're connected to
3019+
3020+
##### *Parameters*
3021+
None
3022+
3023+
##### *Return value*
3024+
*Mixed* Returns the port we're connected to or FALSE if we're not connected
3025+
3026+
### getDBNum
3027+
-----
3028+
_**Description**_: Get the database number phpredis is pointed to
3029+
3030+
##### *Parameters*
3031+
None
3032+
3033+
##### *Return value*
3034+
*Mixed* Returns the database number (LONG) phpredis thinks it's pointing to or FALSE if we're not connected
3035+
3036+
### GetTimeout
3037+
-----
3038+
_**Description**_: Get the (write) timeout in use for phpreids
3039+
3040+
##### *Parameters*
3041+
None
3042+
3043+
##### *Return value*
3044+
*Mixed* The timeout (DOUBLE) specified in our connect call or FALSE if we're not connected
3045+
3046+
### GetReadTimeout
3047+
_**Description**_: Get the read timeout specified to phpredis or FALSE if we're not connected
3048+
3049+
##### *Parameters*
3050+
None
3051+
3052+
##### *Return value*
3053+
*Mixed* Returns the read timeout (which can be set using setOption and Redis::OPT_READ_TIMOUT) or FALSE if we're not connected
3054+
3055+
### GetPersistentID
3056+
-----
3057+
_**Description**_: Gets the persistent ID that phpredis is using
3058+
3059+
##### *Parameters*
3060+
None
3061+
3062+
##### *Return value*
3063+
*Mixed* Returns the persistent id phpredis is using (which will only be set if connected with pconnect), NULL if we're not
3064+
using a persistent ID, and FALSE if we're not connected
3065+
3066+
### GetAuth
3067+
-----
3068+
_**Description**_: Get the password used to authenticate the phpredis connection
3069+
3070+
### *Parameters*
3071+
None
3072+
3073+
### *Return value*
3074+
*Mixed* Returns the password used to authenticate a phpredis session or NULL if none was used, and FALSE if we're not connected

common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ typedef struct {
157157
php_stream *stream;
158158
char *host;
159159
short port;
160+
char *auth;
160161
double timeout;
161162
double read_timeout;
162163
long retry_interval;

library.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,12 @@ PHPAPI void redis_free_socket(RedisSock *redis_sock)
14131413
if(redis_sock->err) {
14141414
efree(redis_sock->err);
14151415
}
1416+
if(redis_sock->auth) {
1417+
efree(redis_sock->auth);
1418+
}
1419+
if(redis_sock->persistent_id) {
1420+
efree(redis_sock->persistent_id);
1421+
}
14161422
efree(redis_sock->host);
14171423
efree(redis_sock);
14181424
}

php_redis.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ PHP_METHOD(Redis, config);
183183

184184
PHP_METHOD(Redis, client);
185185

186+
PHP_METHOD(Redis, getHost);
187+
PHP_METHOD(Redis, getPort);
188+
PHP_METHOD(Redis, getDBNum);
189+
PHP_METHOD(Redis, getTimeout);
190+
PHP_METHOD(Redis, getReadTimeout);
191+
PHP_METHOD(Redis, isConnected);
192+
PHP_METHOD(Redis, getPersistentID);
193+
PHP_METHOD(Redis, getAuth);
194+
186195
#ifdef PHP_WIN32
187196
#define PHP_REDIS_API __declspec(dllexport)
188197
#else

redis.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ static zend_function_entry redis_functions[] = {
236236
/* config */
237237
PHP_ME(Redis, config, NULL, ZEND_ACC_PUBLIC)
238238

239+
/* introspection */
240+
PHP_ME(Redis, getHost, NULL, ZEND_ACC_PUBLIC)
241+
PHP_ME(Redis, getPort, NULL, ZEND_ACC_PUBLIC)
242+
PHP_ME(Redis, getDBNum, NULL, ZEND_ACC_PUBLIC)
243+
PHP_ME(Redis, getTimeout, NULL, ZEND_ACC_PUBLIC)
244+
PHP_ME(Redis, getReadTimeout, NULL, ZEND_ACC_PUBLIC)
245+
PHP_ME(Redis, getPersistentID, NULL, ZEND_ACC_PUBLIC)
246+
PHP_ME(Redis, getAuth, NULL, ZEND_ACC_PUBLIC)
247+
PHP_ME(Redis, isConnected, NULL, ZEND_ACC_PUBLIC)
248+
239249
/* aliases */
240250
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
241251
PHP_MALIAS(Redis, popen, pconnect, NULL, ZEND_ACC_PUBLIC)
@@ -385,6 +395,26 @@ PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC, int no_thr
385395
return Z_LVAL_PP(socket);
386396
}
387397

398+
/**
399+
* redis_sock_get_direct
400+
* Returns our attached RedisSock pointer if we're connected
401+
*/
402+
PHPAPI RedisSock *redis_sock_get_connected(INTERNAL_FUNCTION_PARAMETERS TSRMLS_DC) {
403+
zval *object;
404+
RedisSock *redis_sock;
405+
406+
// If we can't grab our object, or get a socket, or we're not connected, return NULL
407+
if((zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, redis_ce) == FAILURE) ||
408+
(redis_sock_get(object, &redis_sock TSRMLS_CC, 1) < 0) || redis_sock->status != REDIS_SOCK_STATUS_CONNECTED)
409+
{
410+
return NULL;
411+
}
412+
413+
// Return our socket
414+
return redis_sock;
415+
}
416+
417+
388418
/**
389419
* PHP_MINIT_FUNCTION
390420
*/
@@ -3297,6 +3327,10 @@ PHP_METHOD(Redis, auth) {
32973327

32983328
cmd_len = redis_cmd_format_static(&cmd, "AUTH", "s", password, password_len);
32993329

3330+
// Free previously stored auth if we have one, and store this password
3331+
if(redis_sock->auth) efree(redis_sock->auth);
3332+
redis_sock->auth = estrndup(password, password_len);
3333+
33003334
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
33013335
IF_ATOMIC() {
33023336
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
@@ -6312,6 +6346,124 @@ PHP_METHOD(Redis, time) {
63126346
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply_raw);
63136347
}
63146348

6349+
/*
6350+
* Introspection stuff
6351+
*/
6352+
6353+
/*
6354+
* {{{ proto Redis::IsConnected
6355+
*/
6356+
PHP_METHOD(Redis, isConnected) {
6357+
RedisSock *redis_sock;
6358+
6359+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6360+
RETURN_TRUE;
6361+
} else {
6362+
RETURN_FALSE;
6363+
}
6364+
}
6365+
6366+
/*
6367+
* {{{ proto Redis::getHost()
6368+
*/
6369+
PHP_METHOD(Redis, getHost) {
6370+
RedisSock *redis_sock;
6371+
6372+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6373+
RETURN_STRING(redis_sock->host, 1);
6374+
} else {
6375+
RETURN_FALSE;
6376+
}
6377+
}
6378+
6379+
/*
6380+
* {{{ proto Redis::getPort()
6381+
*/
6382+
PHP_METHOD(Redis, getPort) {
6383+
RedisSock *redis_sock;
6384+
6385+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6386+
// Return our port
6387+
RETURN_LONG(redis_sock->port);
6388+
} else {
6389+
RETURN_FALSE;
6390+
}
6391+
}
6392+
6393+
/*
6394+
* {{{ proto Redis::getDBNum
6395+
*/
6396+
PHP_METHOD(Redis, getDBNum) {
6397+
RedisSock *redis_sock;
6398+
6399+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6400+
// Return our db number
6401+
RETURN_LONG(redis_sock->dbNumber);
6402+
} else {
6403+
RETURN_FALSE;
6404+
}
6405+
}
6406+
6407+
/*
6408+
* {{{ proto Redis::getTimeout
6409+
*/
6410+
PHP_METHOD(Redis, getTimeout) {
6411+
RedisSock *redis_sock;
6412+
6413+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6414+
RETURN_DOUBLE(redis_sock->timeout);
6415+
} else {
6416+
RETURN_FALSE;
6417+
}
6418+
}
6419+
6420+
/*
6421+
* {{{ proto Redis::getReadTimeout
6422+
*/
6423+
PHP_METHOD(Redis, getReadTimeout) {
6424+
RedisSock *redis_sock;
6425+
6426+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6427+
RETURN_DOUBLE(redis_sock->read_timeout);
6428+
} else {
6429+
RETURN_FALSE;
6430+
}
6431+
}
6432+
6433+
/*
6434+
* {{{ proto Redis::getPersistentID
6435+
*/
6436+
PHP_METHOD(Redis, getPersistentID) {
6437+
RedisSock *redis_sock;
6438+
6439+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6440+
if(redis_sock->persistent_id != NULL) {
6441+
RETURN_STRING(redis_sock->persistent_id, 1);
6442+
} else {
6443+
RETURN_NULL();
6444+
}
6445+
} else {
6446+
RETURN_FALSE;
6447+
}
6448+
}
6449+
6450+
/*
6451+
* {{{ proto Redis::getAuth
6452+
*/
6453+
PHP_METHOD(Redis, getAuth) {
6454+
RedisSock *redis_sock;
6455+
6456+
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
6457+
if(redis_sock->auth != NULL) {
6458+
RETURN_STRING(redis_sock->auth, 1);
6459+
} else {
6460+
RETURN_NULL();
6461+
}
6462+
} else {
6463+
RETURN_FALSE;
6464+
}
6465+
}
6466+
63156467
/*
63166468
* $redis->client('list');
63176469
* $redis->client('kill', <ip:port>);

tests/TestRedis.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4327,6 +4327,13 @@ public function testReadTimeoutOption() {
43274327
$this->assertEquals(12.3, $this->redis->getOption(Redis::OPT_READ_TIMEOUT));
43284328
}
43294329

4330+
public function testIntrospection() {
4331+
// Simple introspection tests
4332+
$this->assertTrue($this->redis->getHost() === self::HOST);
4333+
$this->assertTrue($this->redis->getPort() === self::PORT);
4334+
$this->assertTrue($this->redis->getAuth() === self::AUTH);
4335+
}
4336+
43304337
}
43314338

43324339
exit(TestSuite::run("Redis_Test"));

0 commit comments

Comments
 (0)