use stub/arginfo for RedisSentinel

This commit is contained in:
Remi Collet
2020-09-14 16:51:32 +02:00
committed by Pavlo Yatsukhnenko
parent ed532e9afc
commit edac508ef5
7 changed files with 146 additions and 49 deletions

View File

@@ -23,12 +23,38 @@
#define redis_sock_write_sstr(redis_sock, sstr) \
redis_sock_write(redis_sock, (sstr)->c, (sstr)->len)
#if PHP_VERSION_ID < 70200
/* drop return type hinting in PHP 7.0 and 7.1*/
#undef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, required_num_args)
#endif
#if PHP_VERSION_ID < 80000
#define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(ZSTR_VAL((zstr)), ZSTR_LEN((zstr)))
/* use RedisException when ValueError not available */
#define REDIS_VALUE_EXCEPTION(m) REDIS_THROW_EXCEPTION(m, 0)
#define RETURN_THROWS() RETURN_FALSE
/* default value only managed in 8+ */
#define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \
ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
/* union type only managed in 8+ */
#define ZEND_ARG_TYPE_MASK(pass_by_ref, name, type_mask, default_value) ZEND_ARG_INFO(pass_by_ref, name)
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type) \
ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, required_num_args)
#define IS_MIXED 0
#else
#define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(zstr)
#define REDIS_VALUE_EXCEPTION(m) zend_value_error(m)
#endif
void redis_register_persistent_resource(zend_string *id, void *ptr, int le_id);
PHP_REDIS_API int redis_extract_auth_info(zval *ztest, zend_string **user, zend_string **pass);

View File

@@ -64,7 +64,6 @@ extern int le_cluster_slot_cache;
extern zend_function_entry redis_array_functions[];
extern zend_function_entry redis_cluster_functions[];
extern zend_function_entry redis_sentinel_functions[];
int le_redis_pconnect;
@@ -873,7 +872,7 @@ PHP_MINIT_FUNCTION(redis)
redis_cluster_ce->create_object = create_cluster_context;
/* RedisSentinel class */
INIT_CLASS_ENTRY(redis_sentinel_class_entry, "RedisSentinel", redis_sentinel_functions);
INIT_CLASS_ENTRY(redis_sentinel_class_entry, "RedisSentinel", redis_sentinel_get_methods());
redis_sentinel_ce = zend_register_internal_class(&redis_sentinel_class_entry);
redis_sentinel_ce->create_object = create_sentinel_object;
@@ -1080,17 +1079,17 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
}
if (timeout < 0L || timeout > INT_MAX) {
REDIS_THROW_EXCEPTION("Invalid connect timeout", 0);
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
return FAILURE;
}
if (read_timeout < 0L || read_timeout > INT_MAX) {
REDIS_THROW_EXCEPTION("Invalid read timeout", 0);
REDIS_VALUE_EXCEPTION("Invalid read timeout");
return FAILURE;
}
if (retry_interval < 0L || retry_interval > INT_MAX) {
REDIS_THROW_EXCEPTION("Invalid retry interval", 0);
REDIS_VALUE_EXCEPTION("Invalid retry interval");
return FAILURE;
}

View File

@@ -38,7 +38,7 @@ extern zend_class_entry *redis_ce;
zend_class_entry *redis_array_ce;
ZEND_BEGIN_ARG_INFO_EX(arginfo_ctor, 0, 0, 1)
ZEND_ARG_INFO(0, name_or_hosts)
ZEND_ARG_TYPE_MASK(0, name_or_hosts, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
@@ -239,7 +239,12 @@ PHP_METHOD(RedisArray, __construct)
* for ages so we can't really change it until the next major version.
*/
if (Z_TYPE_P(z0) != IS_ARRAY && Z_TYPE_P(z0) != IS_STRING)
#if PHP_VERSION_ID < 80000
WRONG_PARAM_COUNT;
#else
zend_argument_type_error(1, "must be of type string|array, %s given", zend_zval_type_name(z0));
RETURN_THROWS();
#endif
/* If it's a string we want to load the array from ini information */
if (Z_TYPE_P(z0) == IS_STRING) {

View File

@@ -23,30 +23,12 @@
zend_class_entry *redis_sentinel_ce;
extern zend_class_entry *redis_exception_ce;
ZEND_BEGIN_ARG_INFO_EX(arginfo_ctor, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, persistent)
ZEND_ARG_INFO(0, retry_interval)
ZEND_ARG_INFO(0, read_timeout)
ZEND_END_ARG_INFO()
#include "redis_sentinel_arginfo.h"
zend_function_entry redis_sentinel_functions[] = {
PHP_ME(RedisSentinel, __construct, arginfo_ctor, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, ckquorum, arginfo_value, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, failover, arginfo_value, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, flushconfig, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, getMasterAddrByName, arginfo_value, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, master, arginfo_value, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, masters, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, myid, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, ping, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, reset, arginfo_value, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, sentinels, arginfo_value, ZEND_ACC_PUBLIC)
PHP_ME(RedisSentinel, slaves, arginfo_value, ZEND_ACC_PUBLIC)
PHP_FE_END
};
extern const zend_function_entry *redis_sentinel_get_methods(void)
{
return class_RedisSentinel_methods;
}
PHP_METHOD(RedisSentinel, __construct)
{
@@ -66,23 +48,23 @@ PHP_METHOD(RedisSentinel, __construct)
}
if (port < 0 || port > UINT16_MAX) {
REDIS_THROW_EXCEPTION("Invalid port", 0);
RETURN_FALSE;
REDIS_VALUE_EXCEPTION("Invalid port");
RETURN_THROWS();
}
if (timeout < 0L || timeout > INT_MAX) {
REDIS_THROW_EXCEPTION("Invalid connect timeout", 0);
RETURN_FALSE;
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
RETURN_THROWS();
}
if (read_timeout < 0L || read_timeout > INT_MAX) {
REDIS_THROW_EXCEPTION("Invalid read timeout", 0);
RETURN_FALSE;
REDIS_VALUE_EXCEPTION("Invalid read timeout");
RETURN_THROWS();
}
if (retry_interval < 0L || retry_interval > INT_MAX) {
REDIS_THROW_EXCEPTION("Invalid retry interval", 0);
RETURN_FALSE;
REDIS_VALUE_EXCEPTION("Invalid retry interval");
RETURN_THROWS();
}
if (zv) {

View File

@@ -5,17 +5,6 @@
#define PHP_REDIS_SENTINEL_VERSION "0.1"
PHP_METHOD(RedisSentinel, __construct);
PHP_METHOD(RedisSentinel, ckquorum);
PHP_METHOD(RedisSentinel, failover);
PHP_METHOD(RedisSentinel, flushconfig);
PHP_METHOD(RedisSentinel, getMasterAddrByName);
PHP_METHOD(RedisSentinel, master);
PHP_METHOD(RedisSentinel, masters);
PHP_METHOD(RedisSentinel, myid);
PHP_METHOD(RedisSentinel, ping);
PHP_METHOD(RedisSentinel, reset);
PHP_METHOD(RedisSentinel, sentinels);
PHP_METHOD(RedisSentinel, slaves);
extern const zend_function_entry *redis_sentinel_get_methods(void);
#endif /* REDIS_SENTINEL_H */

28
redis_sentinel.stub.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
/** @generate-function-entries */
class RedisSentinel {
public function __construct(string $host, int $port = 26379, float $timeout = 0, mixed $persistent = NULL, int $retry_interval = 0, float $read_timeout = 0);
public function ckquorum(string $master): bool;
public function failover(string $master): bool;
public function flushconfig(): bool;
public function getMasterAddrByName(string $master): array|false;
public function master(string $master): array|false;
public function masters(): array|false;
public function ping(): bool;
public function reset(string $pattern): bool;
public function sentinels(string $master): array|false;
public function slaves(string $master): array|false;
}

68
redis_sentinel_arginfo.h Normal file
View File

@@ -0,0 +1,68 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: cfb8ad8fbaaed2ecae02a1385d26e9645364ba9d */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisSentinel___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, host, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, port, IS_LONG, 0, "26379")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, persistent, IS_MIXED, 0, "NULL")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, retry_interval, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, read_timeout, IS_DOUBLE, 0, "0")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisSentinel_ckquorum, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, master, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_RedisSentinel_failover arginfo_class_RedisSentinel_ckquorum
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisSentinel_flushconfig, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_RedisSentinel_getMasterAddrByName, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, master, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_RedisSentinel_master arginfo_class_RedisSentinel_getMasterAddrByName
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_RedisSentinel_masters, 0, 0, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_END_ARG_INFO()
#define arginfo_class_RedisSentinel_ping arginfo_class_RedisSentinel_flushconfig
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisSentinel_reset, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, pattern, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_RedisSentinel_sentinels arginfo_class_RedisSentinel_getMasterAddrByName
#define arginfo_class_RedisSentinel_slaves arginfo_class_RedisSentinel_getMasterAddrByName
ZEND_METHOD(RedisSentinel, __construct);
ZEND_METHOD(RedisSentinel, ckquorum);
ZEND_METHOD(RedisSentinel, failover);
ZEND_METHOD(RedisSentinel, flushconfig);
ZEND_METHOD(RedisSentinel, getMasterAddrByName);
ZEND_METHOD(RedisSentinel, master);
ZEND_METHOD(RedisSentinel, masters);
ZEND_METHOD(RedisSentinel, ping);
ZEND_METHOD(RedisSentinel, reset);
ZEND_METHOD(RedisSentinel, sentinels);
ZEND_METHOD(RedisSentinel, slaves);
static const zend_function_entry class_RedisSentinel_methods[] = {
ZEND_ME(RedisSentinel, __construct, arginfo_class_RedisSentinel___construct, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, ckquorum, arginfo_class_RedisSentinel_ckquorum, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, failover, arginfo_class_RedisSentinel_failover, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, flushconfig, arginfo_class_RedisSentinel_flushconfig, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, getMasterAddrByName, arginfo_class_RedisSentinel_getMasterAddrByName, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, master, arginfo_class_RedisSentinel_master, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, masters, arginfo_class_RedisSentinel_masters, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, ping, arginfo_class_RedisSentinel_ping, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, reset, arginfo_class_RedisSentinel_reset, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, sentinels, arginfo_class_RedisSentinel_sentinels, ZEND_ACC_PUBLIC)
ZEND_ME(RedisSentinel, slaves, arginfo_class_RedisSentinel_slaves, ZEND_ACC_PUBLIC)
ZEND_FE_END
};