Added INI loader

This commit is contained in:
Nicolas Favre-Felix
2011-08-28 14:04:45 +01:00
parent 04bfb396f2
commit 5dffb96bd7
5 changed files with 126 additions and 8 deletions
+2
View File
@@ -60,7 +60,9 @@ PHP_INI_BEGIN()
/* redis arrays */
PHP_INI_ENTRY("redis.arrays.names", "", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("redis.arrays.hosts", "", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("redis.arrays.previous", "", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("redis.arrays.functions", "", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("redis.arrays.index", "", PHP_INI_ALL, NULL)
PHP_INI_END()
ZEND_DECLARE_MODULE_GLOBALS(redis)
+18 -1
View File
@@ -1,3 +1,20 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Nicolas Favre-Felix <n.favre-felix@owlient.eu> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -170,7 +187,7 @@ PHP_METHOD(RedisArray, __construct)
/* extract either name of list of hosts from z0 */
switch(Z_TYPE_P(z0)) {
case IS_STRING:
name = Z_STRVAL_P(z0);
ra = ra_load_array(Z_STRVAL_P(z0));
break;
case IS_ARRAY:
+101
View File
@@ -1,7 +1,29 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Nicolas Favre-Felix <n.favre-felix@owlient.eu> |
+----------------------------------------------------------------------+
*/
#include "redis_array_impl.h"
#include "php_redis.h"
#include "library.h"
#include "php_variables.h"
#include "SAPI.h"
#include "ext/standard/url.h"
#define PHPREDIS_INDEX_NAME "__phpredis_array_index__"
extern int le_redis_sock;
@@ -99,6 +121,85 @@ void ra_init_function_table(RedisArray *ra) {
add_assoc_bool(ra->z_pure_cmds, "SMEMBERS", 1);
}
static int
ra_find_name(const char *name) {
const char *ini_names, *p, *next;
/* php_printf("Loading redis array with name=[%s]\n", name); */
ini_names = INI_STR("redis.arrays.names");
for(p = ini_names; p;) {
next = strchr(p, ',');
if(next) {
if(strncmp(p, name, next - p) == 0) {
return 1;
}
} else {
if(strcmp(p, name) == 0) {
return 1;
}
break;
}
p = next + 1;
}
return 0;
}
/* laod array from INI settings */
RedisArray *ra_load_array(const char *name) {
zval *z_params_hosts, **z_hosts;
zval *z_params_prev, **z_prev;
zval *z_params_funs, **z_data_pp, *z_fun = NULL;
zval *z_params_index;
zend_bool b_index = 0;
HashTable *hHosts = NULL, *hPrev = NULL;
/* find entry */
if(!ra_find_name(name))
return NULL;
/* find hosts */
MAKE_STD_ZVAL(z_params_hosts);
array_init(z_params_hosts);
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.hosts")), z_params_hosts TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(z_params_hosts), name, strlen(name) + 1, (void **) &z_hosts) != FAILURE) {
hHosts = Z_ARRVAL_PP(z_hosts);
}
/* find previous hosts */
MAKE_STD_ZVAL(z_params_prev);
array_init(z_params_prev);
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.previous")), z_params_prev TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(z_params_prev), name, strlen(name) + 1, (void **) &z_prev) != FAILURE) {
hPrev = Z_ARRVAL_PP(z_prev);
}
/* find function */
MAKE_STD_ZVAL(z_params_funs);
array_init(z_params_funs);
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.functions")), z_params_funs TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(z_params_funs), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) {
MAKE_STD_ZVAL(z_fun);
*z_fun = **z_data_pp;
zval_copy_ctor(z_fun);
}
/* find index option */
MAKE_STD_ZVAL(z_params_index);
array_init(z_params_index);
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.index")), z_params_index TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(z_params_index), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) {
if(Z_TYPE_PP(z_data_pp) == IS_STRING && strncmp(Z_STRVAL_PP(z_data_pp), "1", 1) == 0) {
b_index = 1;
}
}
return ra_make_array(hHosts, z_fun, hPrev, b_index);
}
RedisArray *
ra_make_array(HashTable *hosts, zval *z_fun, HashTable *hosts_prev, zend_bool b_index) {
+1
View File
@@ -6,6 +6,7 @@
#include "redis_array.h"
RedisArray* ra_load_hosts(RedisArray *ra, HashTable *hosts);
RedisArray *ra_load_array(const char *name);
RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, HashTable *hosts_prev, zend_bool b_index);
zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos);
void ra_init_function_table(RedisArray *ra);
+4 -7
View File
@@ -1,13 +1,10 @@
<?php
// can't set anything that hasn't been declared when the extension loads.
ini_set('redis.array.names', 'users,friends');
ini_set('redis.array.hosts', 'users=localhost:6379,localhost:6380,localhost:6381,localhost:6382');
ini_set('redis.array.index', 'users=1,friends=0');
// var_dump(ini_get('redis.arrays.names'));
// var_dump(ini_get('redis.arrays.hosts'));
// var_dump(ini_get('redis.arrays.functions'));
ini_set('redis.arrays.names', 'users,friends');
ini_set('redis.arrays.hosts', 'users[]=localhost:6379&users[]=localhost:6380&users[]=localhost:6381&users[]=localhost:6382&friends[]=localhost');
ini_set('redis.arrays.functions', 'users=user_hash');
ini_set('redis.arrays.index', 'users=1,friends=0');
// different redis arrays
$ra = new RedisArray('users');