Skip to content

Commit edac508

Browse files
remicolletyatsukhnenko
authored andcommitted
use stub/arginfo for RedisSentinel
1 parent ed532e9 commit edac508

7 files changed

Lines changed: 147 additions & 50 deletions

File tree

library.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,38 @@
2323
#define redis_sock_write_sstr(redis_sock, sstr) \
2424
redis_sock_write(redis_sock, (sstr)->c, (sstr)->len)
2525

26+
#if PHP_VERSION_ID < 70200
27+
/* drop return type hinting in PHP 7.0 and 7.1*/
28+
#undef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX
29+
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
30+
ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, required_num_args)
31+
#endif
32+
2633
#if PHP_VERSION_ID < 80000
2734
#define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(ZSTR_VAL((zstr)), ZSTR_LEN((zstr)))
35+
36+
/* use RedisException when ValueError not available */
37+
#define REDIS_VALUE_EXCEPTION(m) REDIS_THROW_EXCEPTION(m, 0)
38+
#define RETURN_THROWS() RETURN_FALSE
39+
40+
/* default value only managed in 8+ */
41+
#define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \
42+
ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
43+
44+
/* union type only managed in 8+ */
45+
#define ZEND_ARG_TYPE_MASK(pass_by_ref, name, type_mask, default_value) ZEND_ARG_INFO(pass_by_ref, name)
46+
47+
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type) \
48+
ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, required_num_args)
49+
50+
#define IS_MIXED 0
2851
#else
2952
#define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(zstr)
53+
54+
#define REDIS_VALUE_EXCEPTION(m) zend_value_error(m)
3055
#endif
3156

57+
3258
void redis_register_persistent_resource(zend_string *id, void *ptr, int le_id);
3359

3460
PHP_REDIS_API int redis_extract_auth_info(zval *ztest, zend_string **user, zend_string **pass);

redis.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ extern int le_cluster_slot_cache;
6464

6565
extern zend_function_entry redis_array_functions[];
6666
extern zend_function_entry redis_cluster_functions[];
67-
extern zend_function_entry redis_sentinel_functions[];
6867

6968
int le_redis_pconnect;
7069

@@ -873,7 +872,7 @@ PHP_MINIT_FUNCTION(redis)
873872
redis_cluster_ce->create_object = create_cluster_context;
874873

875874
/* RedisSentinel class */
876-
INIT_CLASS_ENTRY(redis_sentinel_class_entry, "RedisSentinel", redis_sentinel_functions);
875+
INIT_CLASS_ENTRY(redis_sentinel_class_entry, "RedisSentinel", redis_sentinel_get_methods());
877876
redis_sentinel_ce = zend_register_internal_class(&redis_sentinel_class_entry);
878877
redis_sentinel_ce->create_object = create_sentinel_object;
879878

@@ -1080,17 +1079,17 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
10801079
}
10811080

10821081
if (timeout < 0L || timeout > INT_MAX) {
1083-
REDIS_THROW_EXCEPTION("Invalid connect timeout", 0);
1082+
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
10841083
return FAILURE;
10851084
}
10861085

10871086
if (read_timeout < 0L || read_timeout > INT_MAX) {
1088-
REDIS_THROW_EXCEPTION("Invalid read timeout", 0);
1087+
REDIS_VALUE_EXCEPTION("Invalid read timeout");
10891088
return FAILURE;
10901089
}
10911090

10921091
if (retry_interval < 0L || retry_interval > INT_MAX) {
1093-
REDIS_THROW_EXCEPTION("Invalid retry interval", 0);
1092+
REDIS_VALUE_EXCEPTION("Invalid retry interval");
10941093
return FAILURE;
10951094
}
10961095

redis_array.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern zend_class_entry *redis_ce;
3838
zend_class_entry *redis_array_ce;
3939

4040
ZEND_BEGIN_ARG_INFO_EX(arginfo_ctor, 0, 0, 1)
41-
ZEND_ARG_INFO(0, name_or_hosts)
41+
ZEND_ARG_TYPE_MASK(0, name_or_hosts, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
4242
ZEND_ARG_ARRAY_INFO(0, options, 0)
4343
ZEND_END_ARG_INFO()
4444

@@ -239,7 +239,12 @@ PHP_METHOD(RedisArray, __construct)
239239
* for ages so we can't really change it until the next major version.
240240
*/
241241
if (Z_TYPE_P(z0) != IS_ARRAY && Z_TYPE_P(z0) != IS_STRING)
242+
#if PHP_VERSION_ID < 80000
242243
WRONG_PARAM_COUNT;
244+
#else
245+
zend_argument_type_error(1, "must be of type string|array, %s given", zend_zval_type_name(z0));
246+
RETURN_THROWS();
247+
#endif
243248

244249
/* If it's a string we want to load the array from ini information */
245250
if (Z_TYPE_P(z0) == IS_STRING) {

redis_sentinel.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,12 @@
2323
zend_class_entry *redis_sentinel_ce;
2424
extern zend_class_entry *redis_exception_ce;
2525

26-
ZEND_BEGIN_ARG_INFO_EX(arginfo_ctor, 0, 0, 1)
27-
ZEND_ARG_INFO(0, host)
28-
ZEND_ARG_INFO(0, port)
29-
ZEND_ARG_INFO(0, timeout)
30-
ZEND_ARG_INFO(0, persistent)
31-
ZEND_ARG_INFO(0, retry_interval)
32-
ZEND_ARG_INFO(0, read_timeout)
33-
ZEND_END_ARG_INFO()
34-
35-
zend_function_entry redis_sentinel_functions[] = {
36-
PHP_ME(RedisSentinel, __construct, arginfo_ctor, ZEND_ACC_PUBLIC)
37-
PHP_ME(RedisSentinel, ckquorum, arginfo_value, ZEND_ACC_PUBLIC)
38-
PHP_ME(RedisSentinel, failover, arginfo_value, ZEND_ACC_PUBLIC)
39-
PHP_ME(RedisSentinel, flushconfig, arginfo_void, ZEND_ACC_PUBLIC)
40-
PHP_ME(RedisSentinel, getMasterAddrByName, arginfo_value, ZEND_ACC_PUBLIC)
41-
PHP_ME(RedisSentinel, master, arginfo_value, ZEND_ACC_PUBLIC)
42-
PHP_ME(RedisSentinel, masters, arginfo_void, ZEND_ACC_PUBLIC)
43-
PHP_ME(RedisSentinel, myid, arginfo_void, ZEND_ACC_PUBLIC)
44-
PHP_ME(RedisSentinel, ping, arginfo_void, ZEND_ACC_PUBLIC)
45-
PHP_ME(RedisSentinel, reset, arginfo_value, ZEND_ACC_PUBLIC)
46-
PHP_ME(RedisSentinel, sentinels, arginfo_value, ZEND_ACC_PUBLIC)
47-
PHP_ME(RedisSentinel, slaves, arginfo_value, ZEND_ACC_PUBLIC)
48-
PHP_FE_END
49-
};
26+
#include "redis_sentinel_arginfo.h"
27+
28+
extern const zend_function_entry *redis_sentinel_get_methods(void)
29+
{
30+
return class_RedisSentinel_methods;
31+
}
5032

5133
PHP_METHOD(RedisSentinel, __construct)
5234
{
@@ -66,23 +48,23 @@ PHP_METHOD(RedisSentinel, __construct)
6648
}
6749

6850
if (port < 0 || port > UINT16_MAX) {
69-
REDIS_THROW_EXCEPTION("Invalid port", 0);
70-
RETURN_FALSE;
51+
REDIS_VALUE_EXCEPTION("Invalid port");
52+
RETURN_THROWS();
7153
}
7254

7355
if (timeout < 0L || timeout > INT_MAX) {
74-
REDIS_THROW_EXCEPTION("Invalid connect timeout", 0);
75-
RETURN_FALSE;
56+
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
57+
RETURN_THROWS();
7658
}
7759

7860
if (read_timeout < 0L || read_timeout > INT_MAX) {
79-
REDIS_THROW_EXCEPTION("Invalid read timeout", 0);
80-
RETURN_FALSE;
61+
REDIS_VALUE_EXCEPTION("Invalid read timeout");
62+
RETURN_THROWS();
8163
}
8264

8365
if (retry_interval < 0L || retry_interval > INT_MAX) {
84-
REDIS_THROW_EXCEPTION("Invalid retry interval", 0);
85-
RETURN_FALSE;
66+
REDIS_VALUE_EXCEPTION("Invalid retry interval");
67+
RETURN_THROWS();
8668
}
8769

8870
if (zv) {

redis_sentinel.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55

66
#define PHP_REDIS_SENTINEL_VERSION "0.1"
77

8-
PHP_METHOD(RedisSentinel, __construct);
9-
PHP_METHOD(RedisSentinel, ckquorum);
10-
PHP_METHOD(RedisSentinel, failover);
11-
PHP_METHOD(RedisSentinel, flushconfig);
12-
PHP_METHOD(RedisSentinel, getMasterAddrByName);
13-
PHP_METHOD(RedisSentinel, master);
14-
PHP_METHOD(RedisSentinel, masters);
15-
PHP_METHOD(RedisSentinel, myid);
16-
PHP_METHOD(RedisSentinel, ping);
17-
PHP_METHOD(RedisSentinel, reset);
18-
PHP_METHOD(RedisSentinel, sentinels);
19-
PHP_METHOD(RedisSentinel, slaves);
8+
extern const zend_function_entry *redis_sentinel_get_methods(void);
209

2110
#endif /* REDIS_SENTINEL_H */

redis_sentinel.stub.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/** @generate-function-entries */
4+
5+
class RedisSentinel {
6+
7+
public function __construct(string $host, int $port = 26379, float $timeout = 0, mixed $persistent = NULL, int $retry_interval = 0, float $read_timeout = 0);
8+
9+
public function ckquorum(string $master): bool;
10+
11+
public function failover(string $master): bool;
12+
13+
public function flushconfig(): bool;
14+
15+
public function getMasterAddrByName(string $master): array|false;
16+
17+
public function master(string $master): array|false;
18+
19+
public function masters(): array|false;
20+
21+
public function ping(): bool;
22+
23+
public function reset(string $pattern): bool;
24+
25+
public function sentinels(string $master): array|false;
26+
27+
public function slaves(string $master): array|false;
28+
}

redis_sentinel_arginfo.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* This is a generated file, edit the .stub.php file instead.
2+
* Stub hash: cfb8ad8fbaaed2ecae02a1385d26e9645364ba9d */
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_END_ARG_INFO()
12+
13+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisSentinel_ckquorum, 0, 1, _IS_BOOL, 0)
14+
ZEND_ARG_TYPE_INFO(0, master, IS_STRING, 0)
15+
ZEND_END_ARG_INFO()
16+
17+
#define arginfo_class_RedisSentinel_failover arginfo_class_RedisSentinel_ckquorum
18+
19+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisSentinel_flushconfig, 0, 0, _IS_BOOL, 0)
20+
ZEND_END_ARG_INFO()
21+
22+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_RedisSentinel_getMasterAddrByName, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
23+
ZEND_ARG_TYPE_INFO(0, master, IS_STRING, 0)
24+
ZEND_END_ARG_INFO()
25+
26+
#define arginfo_class_RedisSentinel_master arginfo_class_RedisSentinel_getMasterAddrByName
27+
28+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_RedisSentinel_masters, 0, 0, MAY_BE_ARRAY|MAY_BE_FALSE)
29+
ZEND_END_ARG_INFO()
30+
31+
#define arginfo_class_RedisSentinel_ping arginfo_class_RedisSentinel_flushconfig
32+
33+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisSentinel_reset, 0, 1, _IS_BOOL, 0)
34+
ZEND_ARG_TYPE_INFO(0, pattern, IS_STRING, 0)
35+
ZEND_END_ARG_INFO()
36+
37+
#define arginfo_class_RedisSentinel_sentinels arginfo_class_RedisSentinel_getMasterAddrByName
38+
39+
#define arginfo_class_RedisSentinel_slaves arginfo_class_RedisSentinel_getMasterAddrByName
40+
41+
42+
ZEND_METHOD(RedisSentinel, __construct);
43+
ZEND_METHOD(RedisSentinel, ckquorum);
44+
ZEND_METHOD(RedisSentinel, failover);
45+
ZEND_METHOD(RedisSentinel, flushconfig);
46+
ZEND_METHOD(RedisSentinel, getMasterAddrByName);
47+
ZEND_METHOD(RedisSentinel, master);
48+
ZEND_METHOD(RedisSentinel, masters);
49+
ZEND_METHOD(RedisSentinel, ping);
50+
ZEND_METHOD(RedisSentinel, reset);
51+
ZEND_METHOD(RedisSentinel, sentinels);
52+
ZEND_METHOD(RedisSentinel, slaves);
53+
54+
55+
static const zend_function_entry class_RedisSentinel_methods[] = {
56+
ZEND_ME(RedisSentinel, __construct, arginfo_class_RedisSentinel___construct, ZEND_ACC_PUBLIC)
57+
ZEND_ME(RedisSentinel, ckquorum, arginfo_class_RedisSentinel_ckquorum, ZEND_ACC_PUBLIC)
58+
ZEND_ME(RedisSentinel, failover, arginfo_class_RedisSentinel_failover, ZEND_ACC_PUBLIC)
59+
ZEND_ME(RedisSentinel, flushconfig, arginfo_class_RedisSentinel_flushconfig, ZEND_ACC_PUBLIC)
60+
ZEND_ME(RedisSentinel, getMasterAddrByName, arginfo_class_RedisSentinel_getMasterAddrByName, ZEND_ACC_PUBLIC)
61+
ZEND_ME(RedisSentinel, master, arginfo_class_RedisSentinel_master, ZEND_ACC_PUBLIC)
62+
ZEND_ME(RedisSentinel, masters, arginfo_class_RedisSentinel_masters, ZEND_ACC_PUBLIC)
63+
ZEND_ME(RedisSentinel, ping, arginfo_class_RedisSentinel_ping, ZEND_ACC_PUBLIC)
64+
ZEND_ME(RedisSentinel, reset, arginfo_class_RedisSentinel_reset, ZEND_ACC_PUBLIC)
65+
ZEND_ME(RedisSentinel, sentinels, arginfo_class_RedisSentinel_sentinels, ZEND_ACC_PUBLIC)
66+
ZEND_ME(RedisSentinel, slaves, arginfo_class_RedisSentinel_slaves, ZEND_ACC_PUBLIC)
67+
ZEND_FE_END
68+
};

0 commit comments

Comments
 (0)