Skip to content

Commit 8ec96a1

Browse files
committed
RedisArray constructor
1 parent 17a9c9e commit 8ec96a1

5 files changed

Lines changed: 100 additions & 1 deletion

File tree

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ if test "$PHP_REDIS" != "no"; then
5555
dnl
5656
dnl PHP_SUBST(REDIS_SHARED_LIBADD)
5757

58-
PHP_NEW_EXTENSION(redis, redis.c library.c redis_session.c igbinary/igbinary.c igbinary/hash_si.c igbinary/hash_function.c, $ext_shared)
58+
PHP_NEW_EXTENSION(redis, redis.c library.c redis_session.c redis_array.c igbinary/igbinary.c igbinary/hash_si.c igbinary/hash_function.c, $ext_shared)
5959
fi

redis.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
#include "common.h"
2727
#include "ext/standard/info.h"
28+
#include "php_ini.h"
2829
#include "php_redis.h"
30+
#include "redis_array.h"
2931
#include <zend_exceptions.h>
3032

3133
#ifdef PHP_SESSION
@@ -47,6 +49,14 @@ zend_class_entry *redis_ce;
4749
zend_class_entry *redis_exception_ce;
4850
zend_class_entry *spl_ce_RuntimeException = NULL;
4951

52+
extern zend_function_entry redis_array_functions[];
53+
54+
PHP_INI_BEGIN()
55+
/* redis arrays */
56+
PHP_INI_ENTRY("redis.arrays.names", "", PHP_INI_ALL, NULL)
57+
PHP_INI_ENTRY("redis.arrays.hosts", "", PHP_INI_ALL, NULL)
58+
PHP_INI_ENTRY("redis.arrays.functions", "", PHP_INI_ALL, NULL)
59+
PHP_INI_END()
5060

5161
ZEND_DECLARE_MODULE_GLOBALS(redis)
5262

@@ -297,11 +307,20 @@ PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC)
297307
PHP_MINIT_FUNCTION(redis)
298308
{
299309
zend_class_entry redis_class_entry;
310+
zend_class_entry redis_array_class_entry;
300311
zend_class_entry redis_exception_class_entry;
312+
313+
REGISTER_INI_ENTRIES();
301314

315+
/* Redis class */
302316
INIT_CLASS_ENTRY(redis_class_entry, "Redis", redis_functions);
303317
redis_ce = zend_register_internal_class(&redis_class_entry TSRMLS_CC);
304318

319+
/* RedisArray class */
320+
INIT_CLASS_ENTRY(redis_array_class_entry, "RedisArray", redis_array_functions);
321+
redis_array_ce = zend_register_internal_class(&redis_array_class_entry TSRMLS_CC);
322+
323+
/* RedisException class */
305324
INIT_CLASS_ENTRY(redis_exception_class_entry, "RedisException", NULL);
306325
redis_exception_ce = zend_register_internal_class_ex(
307326
&redis_exception_class_entry,

redis_array.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifdef HAVE_CONFIG_H
2+
#include "config.h"
3+
#endif
4+
5+
#include "common.h"
6+
#include "ext/standard/info.h"
7+
#include "php_ini.h"
8+
#include "php_redis.h"
9+
#include "redis_array.h"
10+
#include <zend_exceptions.h>
11+
12+
#include "library.h"
13+
#include "redis_array.h"
14+
15+
zend_function_entry redis_array_functions[] = {
16+
PHP_ME(RedisArray, __construct, NULL, ZEND_ACC_PUBLIC)
17+
{NULL, NULL, NULL}
18+
};
19+
20+
/* {{{ proto RedisArray RedisArray::__construct()
21+
Public constructor */
22+
PHP_METHOD(RedisArray, __construct)
23+
{
24+
zval *z0;
25+
char *fun = NULL, *name = NULL;
26+
int fun_len;
27+
28+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &z0, &fun, &fun_len) == FAILURE) {
29+
RETURN_FALSE;
30+
}
31+
32+
if(!fun || !fun_len) { /* either an array name or a list of hosts */
33+
switch(Z_TYPE_P(z0)) {
34+
case IS_STRING:
35+
name = Z_STRVAL_P(z0);
36+
printf("name=%s\n", name);
37+
break;
38+
39+
case IS_ARRAY:
40+
printf("ARRAY OF HOSTS\n");
41+
break;
42+
43+
default:
44+
WRONG_PARAM_COUNT;
45+
break;
46+
}
47+
} else {
48+
if(Z_TYPE_P(z0) != IS_ARRAY) {
49+
WRONG_PARAM_COUNT;
50+
}
51+
printf("ARRAY OF HOSTS, fun=%s\n", fun);
52+
}
53+
}

redis_array.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef REDIS_ARRAY_H
2+
#define REDIS_ARRAY_H
3+
4+
#include "common.h"
5+
6+
zend_class_entry *redis_array_ce;
7+
8+
PHP_METHOD(RedisArray, __construct);
9+
10+
#endif

tests/array-test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
// can't set anything that hasn't been declared when the extension loads.
4+
ini_set('redis.array.names', 'users,friends');
5+
ini_set('redis.array.hosts', 'users=localhost:6379,localhost:6380,localhost:6381,localhost:6382');
6+
7+
var_dump(ini_get('redis.arrays.names'));
8+
var_dump(ini_get('redis.arrays.hosts'));
9+
var_dump(ini_get('redis.arrays.functions'));
10+
11+
// different redis arrays
12+
$ra = new RedisArray('users');
13+
$ra = new RedisArray(array('localhost:6379', 'localhost:6380'));
14+
$ra = new RedisArray(array('localhost:6379', 'localhost:6380'), 'hash_key');
15+
16+
17+
?>

0 commit comments

Comments
 (0)