Skip to content

Commit ebb2386

Browse files
committed
Synchronize Redis and RedisSentinel constructors
1 parent e571a81 commit ebb2386

9 files changed

+105
-154
lines changed

library.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,80 @@ PHP_REDIS_API void redis_debug_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock
27492749
}
27502750
}
27512751

2752+
PHP_REDIS_API int
2753+
redis_sock_configure(RedisSock *redis_sock, HashTable *opts)
2754+
{
2755+
zend_string *zkey;
2756+
zval *val;
2757+
2758+
ZEND_HASH_FOREACH_STR_KEY_VAL(opts, zkey, val) {
2759+
if (zkey == NULL) {
2760+
continue;
2761+
}
2762+
ZVAL_DEREF(val);
2763+
if (zend_string_equals_literal_ci(zkey, "host")) {
2764+
if (Z_TYPE_P(val) != IS_STRING) {
2765+
REDIS_VALUE_EXCEPTION("Invalid host");
2766+
return FAILURE;
2767+
}
2768+
if (redis_sock->host) zend_string_release(redis_sock->host);
2769+
redis_sock->host = zval_get_string(val);
2770+
} else if (zend_string_equals_literal_ci(zkey, "port")) {
2771+
if (Z_TYPE_P(val) != IS_LONG) {
2772+
REDIS_VALUE_EXCEPTION("Invalid port");
2773+
return FAILURE;
2774+
}
2775+
redis_sock->port = zval_get_long(val);
2776+
} else if (zend_string_equals_literal_ci(zkey, "connectTimeout")) {
2777+
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
2778+
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
2779+
return FAILURE;
2780+
}
2781+
redis_sock->timeout = zval_get_double(val);
2782+
} else if (zend_string_equals_literal_ci(zkey, "readTimeout")) {
2783+
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
2784+
REDIS_VALUE_EXCEPTION("Invalid read timeout");
2785+
return FAILURE;
2786+
}
2787+
redis_sock->read_timeout = zval_get_double(val);
2788+
} else if (zend_string_equals_literal_ci(zkey, "persistent")) {
2789+
if (Z_TYPE_P(val) == IS_STRING) {
2790+
if (redis_sock->persistent_id) zend_string_release(redis_sock->persistent_id);
2791+
redis_sock->persistent_id = zval_get_string(val);
2792+
redis_sock->persistent = 1;
2793+
} else {
2794+
redis_sock->persistent = zval_is_true(val);
2795+
}
2796+
} else if (zend_string_equals_literal_ci(zkey, "retryInterval")) {
2797+
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
2798+
REDIS_VALUE_EXCEPTION("Invalid retry interval");
2799+
return FAILURE;
2800+
}
2801+
redis_sock->retry_interval = zval_get_long(val);
2802+
} else if (zend_string_equals_literal_ci(zkey, "ssl")) {
2803+
if (redis_sock_set_stream_context(redis_sock, val) != SUCCESS) {
2804+
REDIS_VALUE_EXCEPTION("Invalid SSL context options");
2805+
return FAILURE;
2806+
}
2807+
} else if (zend_string_equals_literal_ci(zkey, "auth")) {
2808+
if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_ARRAY) {
2809+
REDIS_VALUE_EXCEPTION("Invalid auth credentials");
2810+
return FAILURE;
2811+
}
2812+
redis_sock_set_auth_zval(redis_sock, val);
2813+
} else if (zend_string_equals_literal_ci(zkey, "backoff")) {
2814+
if (redis_sock_set_backoff(redis_sock, val) != SUCCESS) {
2815+
REDIS_VALUE_EXCEPTION("Invalid backoff options");
2816+
return FAILURE;
2817+
}
2818+
} else {
2819+
php_error_docref(NULL, E_WARNING, "Skip unknown option '%s'", ZSTR_VAL(zkey));
2820+
}
2821+
} ZEND_HASH_FOREACH_END();
2822+
2823+
return SUCCESS;
2824+
}
2825+
27522826
/**
27532827
* redis_sock_create
27542828
*/

library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ PHP_REDIS_API void redis_parse_info_response(char *response, zval *z_ret);
7575
PHP_REDIS_API void redis_parse_client_list_response(char *response, zval *z_ret);
7676
PHP_REDIS_API int redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
7777
PHP_REDIS_API RedisSock* redis_sock_create(char *host, int host_len, int port, double timeout, double read_timeout, int persistent, char *persistent_id, long retry_interval);
78+
PHP_REDIS_API int redis_sock_configure(RedisSock *redis_sock, HashTable *opts);
7879
PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock);
7980
PHP_REDIS_API int redis_sock_server_open(RedisSock *redis_sock);
8081
PHP_REDIS_API int redis_sock_auth(RedisSock *redis_sock);

redis.c

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -455,82 +455,18 @@ PHP_MINFO_FUNCTION(redis)
455455
Public constructor */
456456
PHP_METHOD(Redis, __construct)
457457
{
458+
HashTable *opts = NULL;
458459
redis_object *redis;
459-
zend_string *zkey;
460-
zval *val, *opts = NULL;
461460

462-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a", &opts) == FAILURE) {
463-
RETURN_THROWS();
464-
}
461+
ZEND_PARSE_PARAMETERS_START(0, 1)
462+
Z_PARAM_OPTIONAL
463+
Z_PARAM_ARRAY_HT_OR_NULL(opts)
464+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_THROWS());
465465

466-
if (opts != NULL) {
467-
redis = PHPREDIS_ZVAL_GET_OBJECT(redis_object, getThis());
468-
redis->sock = redis_sock_create("127.0.0.1", sizeof("127.0.0.1") - 1, 6379, 0, 0, 0, NULL, 0);
469-
470-
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(opts), zkey, val) {
471-
if (zkey == NULL) {
472-
continue;
473-
}
474-
ZVAL_DEREF(val);
475-
if (zend_string_equals_literal_ci(zkey, "host")) {
476-
if (Z_TYPE_P(val) != IS_STRING) {
477-
REDIS_VALUE_EXCEPTION("Invalid host");
478-
RETURN_THROWS();
479-
}
480-
zend_string_release(redis->sock->host);
481-
redis->sock->host = zval_get_string(val);
482-
} else if (zend_string_equals_literal_ci(zkey, "port")) {
483-
if (Z_TYPE_P(val) != IS_LONG) {
484-
REDIS_VALUE_EXCEPTION("Invalid port");
485-
RETURN_THROWS();
486-
}
487-
redis->sock->port = zval_get_long(val);
488-
} else if (zend_string_equals_literal_ci(zkey, "connectTimeout")) {
489-
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
490-
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
491-
RETURN_THROWS();
492-
}
493-
redis->sock->timeout = zval_get_double(val);
494-
} else if (zend_string_equals_literal_ci(zkey, "readTimeout")) {
495-
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
496-
REDIS_VALUE_EXCEPTION("Invalid read timeout");
497-
RETURN_THROWS();
498-
}
499-
redis->sock->read_timeout = zval_get_double(val);
500-
} else if (zend_string_equals_literal_ci(zkey, "persistent")) {
501-
if (Z_TYPE_P(val) == IS_STRING) {
502-
if (redis->sock->persistent_id) zend_string_release(redis->sock->persistent_id);
503-
redis->sock->persistent_id = zval_get_string(val);
504-
redis->sock->persistent = 1;
505-
} else {
506-
redis->sock->persistent = zval_is_true(val);
507-
}
508-
} else if (zend_string_equals_literal_ci(zkey, "retryInterval")) {
509-
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
510-
REDIS_VALUE_EXCEPTION("Invalid retry interval");
511-
RETURN_THROWS();
512-
}
513-
redis->sock->retry_interval = zval_get_long(val);
514-
} else if (zend_string_equals_literal_ci(zkey, "ssl")) {
515-
if (redis_sock_set_stream_context(redis->sock, val) != SUCCESS) {
516-
REDIS_VALUE_EXCEPTION("Invalid SSL context options");
517-
RETURN_THROWS();
518-
}
519-
} else if (zend_string_equals_literal_ci(zkey, "auth")) {
520-
if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_ARRAY) {
521-
REDIS_VALUE_EXCEPTION("Invalid auth credentials");
522-
RETURN_THROWS();
523-
}
524-
redis_sock_set_auth_zval(redis->sock, val);
525-
} else if (zend_string_equals_literal_ci(zkey, "backoff")) {
526-
if (redis_sock_set_backoff(redis->sock, val) != SUCCESS) {
527-
REDIS_VALUE_EXCEPTION("Invalid backoff options");
528-
RETURN_THROWS();
529-
}
530-
} else {
531-
php_error_docref(NULL, E_WARNING, "Skip unknown option '%s'", ZSTR_VAL(zkey));
532-
}
533-
} ZEND_HASH_FOREACH_END();
466+
redis = PHPREDIS_ZVAL_GET_OBJECT(redis_object, getThis());
467+
redis->sock = redis_sock_create(ZEND_STRL("127.0.0.1"), 6379, 0, 0, 0, NULL, 0);
468+
if (opts != NULL && redis_sock_configure(redis->sock, opts) != SUCCESS) {
469+
RETURN_THROWS();
534470
}
535471
}
536472
/* }}} */

redis_sentinel.c

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -41,61 +41,20 @@ PHP_MINIT_FUNCTION(redis_sentinel)
4141

4242
PHP_METHOD(RedisSentinel, __construct)
4343
{
44-
int persistent = 0;
45-
char *persistent_id = NULL;
46-
double timeout = 0.0, read_timeout = 0.0;
47-
zend_long port = 26379, retry_interval = 0;
48-
redis_sentinel_object *obj;
49-
zend_string *host;
50-
zval *auth = NULL, *context = NULL, *zv = NULL;
51-
52-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|ldz!ldza",
53-
&host, &port, &timeout, &zv,
54-
&retry_interval, &read_timeout,
55-
&auth, &context) == FAILURE) {
56-
RETURN_FALSE;
57-
}
58-
59-
if (port < 0 || port > UINT16_MAX) {
60-
REDIS_VALUE_EXCEPTION("Invalid port");
61-
RETURN_THROWS();
62-
}
63-
64-
if (timeout > INT_MAX) {
65-
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
66-
RETURN_THROWS();
67-
}
44+
HashTable *opts = NULL;
45+
redis_sentinel_object *sentinel;
6846

69-
if (read_timeout > INT_MAX) {
70-
REDIS_VALUE_EXCEPTION("Invalid read timeout");
71-
RETURN_THROWS();
72-
}
47+
ZEND_PARSE_PARAMETERS_START(0, 1)
48+
Z_PARAM_OPTIONAL
49+
Z_PARAM_ARRAY_HT_OR_NULL(opts)
50+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_THROWS());
7351

74-
if (retry_interval < 0L || retry_interval > INT_MAX) {
75-
REDIS_VALUE_EXCEPTION("Invalid retry interval");
52+
sentinel = PHPREDIS_ZVAL_GET_OBJECT(redis_sentinel_object, getThis());
53+
sentinel->sock = redis_sock_create(ZEND_STRL("127.0.0.1"), 26379, 0, 0, 0, NULL, 0);
54+
if (opts != NULL && redis_sock_configure(sentinel->sock, opts) != SUCCESS) {
7655
RETURN_THROWS();
7756
}
78-
79-
if (zv) {
80-
ZVAL_DEREF(zv);
81-
if (Z_TYPE_P(zv) == IS_STRING) {
82-
persistent_id = Z_STRVAL_P(zv);
83-
persistent = 1; /* even empty */
84-
} else {
85-
persistent = zval_is_true(zv);
86-
}
87-
}
88-
89-
obj = PHPREDIS_ZVAL_GET_OBJECT(redis_sentinel_object, getThis());
90-
obj->sock = redis_sock_create(ZSTR_VAL(host), ZSTR_LEN(host), port,
91-
timeout, read_timeout, persistent, persistent_id, retry_interval);
92-
if (auth) {
93-
redis_sock_set_auth_zval(obj->sock, auth);
94-
}
95-
if (context) {
96-
redis_sock_set_stream_context(obj->sock, context);
97-
}
98-
obj->sock->sentinel = 1;
57+
sentinel->sock->sentinel = 1;
9958
}
10059

10160
PHP_METHOD(RedisSentinel, ckquorum)

redis_sentinel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "sentinel_library.h"
55

6-
#define PHP_REDIS_SENTINEL_VERSION "0.1"
6+
#define PHP_REDIS_SENTINEL_VERSION "1.0"
77

88
extern zend_class_entry *redis_sentinel_ce;
99
extern PHP_MINIT_FUNCTION(redis_sentinel);

redis_sentinel.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class RedisSentinel {
1010

11-
public function __construct(string $host, int $port = 26379, float $timeout = 0, mixed $persistent = null, int $retry_interval = 0, float $read_timeout = 0, #[\SensitiveParameter] mixed $auth = null, array $context = null);
11+
public function __construct(array $options = null);
1212

1313
/** @return bool|RedisSentinel */
1414
public function ckquorum(string $master);

redis_sentinel_arginfo.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 847c735dfbbb643366344acfe6e2c5e8b76d0520 */
3-
4-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel___construct, 0, 0, 1)
5-
ZEND_ARG_TYPE_INFO(0, host, IS_STRING, 0)
6-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, port, IS_LONG, 0, "26379")
7-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 0, "0")
8-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, persistent, IS_MIXED, 0, "null")
9-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, retry_interval, IS_LONG, 0, "0")
10-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, read_timeout, IS_DOUBLE, 0, "0")
11-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, auth, IS_MIXED, 0, "null")
12-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, context, IS_ARRAY, 0, "null")
2+
* Stub hash: f1f746cc848b1debcdf88eae015732720ba206c8 */
3+
4+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel___construct, 0, 0, 0)
5+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
136
ZEND_END_ARG_INFO()
147

158
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel_ckquorum, 0, 0, 1)
@@ -77,11 +70,6 @@ static zend_class_entry *register_class_RedisSentinel(void)
7770

7871
INIT_CLASS_ENTRY(ce, "RedisSentinel", class_RedisSentinel_methods);
7972
class_entry = zend_register_internal_class_ex(&ce, NULL);
80-
#if (PHP_VERSION_ID >= 80200)
81-
82-
83-
zend_add_parameter_attribute(zend_hash_str_find_ptr(&class_entry->function_table, "__construct", sizeof("__construct") - 1), 6, ZSTR_KNOWN(ZEND_STR_SENSITIVEPARAMETER), 0);
84-
#endif
8573

8674
return class_entry;
8775
}

redis_sentinel_legacy_arginfo.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 847c735dfbbb643366344acfe6e2c5e8b76d0520 */
3-
4-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel___construct, 0, 0, 1)
5-
ZEND_ARG_INFO(0, host)
6-
ZEND_ARG_INFO(0, port)
7-
ZEND_ARG_INFO(0, timeout)
8-
ZEND_ARG_INFO(0, persistent)
9-
ZEND_ARG_INFO(0, retry_interval)
10-
ZEND_ARG_INFO(0, read_timeout)
11-
ZEND_ARG_INFO(0, auth)
12-
ZEND_ARG_INFO(0, context)
2+
* Stub hash: f1f746cc848b1debcdf88eae015732720ba206c8 */
3+
4+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel___construct, 0, 0, 0)
5+
ZEND_ARG_INFO(0, options)
136
ZEND_END_ARG_INFO()
147

158
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel_ckquorum, 0, 0, 1)

tests/RedisSentinelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Redis_Sentinel_Test extends TestSuite
3030

3131
protected function newInstance()
3232
{
33-
return new RedisSentinel($this->getHost());
33+
return new RedisSentinel(['host' => $this->getHost()]);
3434
}
3535

3636
public function setUp()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy