Skip to content

Commit 2004b3e

Browse files
authored
Merge pull request phpredis#2010 from phpredis/ctor-options
Redis::__construct options
2 parents d183694 + 6c8cd5c commit 2004b3e

File tree

6 files changed

+135
-37
lines changed

6 files changed

+135
-37
lines changed

README.markdown

+26
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@ _**Description**_: Creates a Redis client
158158
$redis = new Redis();
159159
~~~
160160

161+
Starting from version 6.0.0 it's possible to specify configuration options.
162+
This allows to connect to the server without explicitly invoking `connect` command.
163+
164+
##### *Example*
165+
166+
~~~php
167+
$redis = new Redis([
168+
'host' => '127.0.0.1',
169+
'port' => 6379,
170+
'connectTimeout' => 2.5,
171+
'auth' => ['phpredis', 'phpredis'],
172+
'ssl' => ['verify_peer' => false],
173+
]);
174+
~~~
175+
176+
##### *Parameters*
177+
178+
*host*: string. can be a host, or the path to a unix domain socket.
179+
*port*: int
180+
*connectTimeout*: float, value in seconds (default is 0 meaning unlimited)
181+
*retryInterval*: int, value in milliseconds (optional)
182+
*readTimeout*: float, value in seconds (default is 0 meaning unlimited)
183+
*persistent*: mixed, if value is string then it used as persistend id, else value casts to boolean
184+
*auth*: mixed, authentication information
185+
*ssl*: array, SSL context options
186+
161187
### Class RedisException
162188
-----
163189
phpredis throws a [RedisException](#class-redisexception) object if it can't reach the Redis server. That can happen in case of connectivity issues,

redis.c

+70-3
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,79 @@ PHP_MINFO_FUNCTION(redis)
601601
DISPLAY_INI_ENTRIES();
602602
}
603603

604-
/* {{{ proto Redis Redis::__construct()
604+
/* {{{ proto Redis Redis::__construct(array $options = null)
605605
Public constructor */
606606
PHP_METHOD(Redis, __construct)
607607
{
608-
if (zend_parse_parameters_none() == FAILURE) {
609-
RETURN_FALSE;
608+
redis_object *redis;
609+
zend_string *zkey;
610+
zval *val, *opts = NULL;
611+
612+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a", &opts) == FAILURE) {
613+
RETURN_THROWS();
614+
}
615+
616+
if (opts != NULL) {
617+
redis = PHPREDIS_ZVAL_GET_OBJECT(redis_object, getThis());
618+
redis->sock = redis_sock_create("127.0.0.1", 0, 6379, 0, 0, 0, NULL, 0);
619+
620+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(opts), zkey, val) {
621+
ZVAL_DEREF(val);
622+
if (zend_string_equals_literal_ci(zkey, "host")) {
623+
if (Z_TYPE_P(val) != IS_STRING) {
624+
REDIS_VALUE_EXCEPTION("Invalid host");
625+
RETURN_THROWS();
626+
}
627+
zend_string_release(redis->sock->host);
628+
redis->sock->host = zval_get_string(val);
629+
} else if (zend_string_equals_literal_ci(zkey, "port")) {
630+
if (Z_TYPE_P(val) != IS_LONG) {
631+
REDIS_VALUE_EXCEPTION("Invalid port");
632+
RETURN_THROWS();
633+
}
634+
redis->sock->port = zval_get_long(val);
635+
} else if (zend_string_equals_literal_ci(zkey, "connectTimeout")) {
636+
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
637+
REDIS_VALUE_EXCEPTION("Invalid connect timeout");
638+
RETURN_THROWS();
639+
}
640+
redis->sock->timeout = zval_get_double(val);
641+
} else if (zend_string_equals_literal_ci(zkey, "readTimeout")) {
642+
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
643+
REDIS_VALUE_EXCEPTION("Invalid read timeout");
644+
RETURN_THROWS();
645+
}
646+
redis->sock->read_timeout = zval_get_double(val);
647+
} else if (zend_string_equals_literal_ci(zkey, "persistent")) {
648+
if (Z_TYPE_P(val) == IS_STRING) {
649+
if (redis->sock->persistent_id) zend_string_release(redis->sock->persistent_id);
650+
redis->sock->persistent_id = zval_get_string(val);
651+
redis->sock->persistent = 1;
652+
} else {
653+
redis->sock->persistent = zval_is_true(val);
654+
}
655+
} else if (zend_string_equals_literal_ci(zkey, "retryInterval")) {
656+
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_DOUBLE) {
657+
REDIS_VALUE_EXCEPTION("Invalid retry interval");
658+
RETURN_THROWS();
659+
}
660+
redis->sock->retry_interval = zval_get_long(val);
661+
} else if (zend_string_equals_literal_ci(zkey, "ssl")) {
662+
if (Z_TYPE_P(val) != IS_ARRAY) {
663+
REDIS_VALUE_EXCEPTION("Invalid SSL context options");
664+
RETURN_THROWS();
665+
}
666+
redis_sock_set_stream_context(redis->sock, val);
667+
} else if (zend_string_equals_literal_ci(zkey, "auth")) {
668+
if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_ARRAY) {
669+
REDIS_VALUE_EXCEPTION("Invalid auth credentials");
670+
RETURN_THROWS();
671+
}
672+
redis_sock_set_auth_zval(redis->sock, val);
673+
} else {
674+
php_error_docref(NULL, E_WARNING, "Skip unknown option '%s'", ZSTR_VAL(zkey));
675+
}
676+
} ZEND_HASH_FOREACH_END();
610677
}
611678
}
612679
/* }}} */

redis.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Redis {
99

10-
public function __construct();
10+
public function __construct(array $options = null);
1111

1212
public function _compress(string $value): string;
1313

redis_arginfo.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: d32b3d0f25155fa5d5f1c9626bbf3f4bcfdf75ae */
2+
* Stub hash: fb1a3f1245758210548e5ed11543fe059e6e8770 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
5+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
56
ZEND_END_ARG_INFO()
67

78
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis__compress, 0, 1, IS_STRING, 0)
89
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
910
ZEND_END_ARG_INFO()
1011

11-
#define arginfo_class_Redis___destruct arginfo_class_Redis___construct
12+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___destruct, 0, 0, 0)
13+
ZEND_END_ARG_INFO()
1214

1315
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis__pack, 0, 1, IS_STRING, 0)
1416
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
@@ -521,7 +523,7 @@ ZEND_END_ARG_INFO()
521523

522524
#define arginfo_class_Redis_rPop arginfo_class_Redis_decr
523525

524-
#define arginfo_class_Redis_randomKey arginfo_class_Redis___construct
526+
#define arginfo_class_Redis_randomKey arginfo_class_Redis___destruct
525527

526528
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_rawcommand, 0, 1, IS_MIXED, 0)
527529
ZEND_ARG_TYPE_INFO(0, command, IS_STRING, 0)
@@ -708,7 +710,7 @@ ZEND_END_ARG_INFO()
708710

709711
#define arginfo_class_Redis_unsubscribe arginfo_class_Redis_subscribe
710712

711-
#define arginfo_class_Redis_unwatch arginfo_class_Redis___construct
713+
#define arginfo_class_Redis_unwatch arginfo_class_Redis___destruct
712714

713715
#define arginfo_class_Redis_watch arginfo_class_Redis_del
714716

redis_legacy_arginfo.h

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: d32b3d0f25155fa5d5f1c9626bbf3f4bcfdf75ae */
2+
* Stub hash: fb1a3f1245758210548e5ed11543fe059e6e8770 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
5+
ZEND_ARG_INFO(0, options)
56
ZEND_END_ARG_INFO()
67

78
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis__compress, 0, 0, 1)
89
ZEND_ARG_INFO(0, value)
910
ZEND_END_ARG_INFO()
1011

11-
#define arginfo_class_Redis___destruct arginfo_class_Redis___construct
12+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___destruct, 0, 0, 0)
13+
ZEND_END_ARG_INFO()
1214

1315
#define arginfo_class_Redis__pack arginfo_class_Redis__compress
1416

@@ -38,9 +40,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_auth, 0, 0, 1)
3840
ZEND_ARG_INFO(0, credentials)
3941
ZEND_END_ARG_INFO()
4042

41-
#define arginfo_class_Redis_bgSave arginfo_class_Redis___construct
43+
#define arginfo_class_Redis_bgSave arginfo_class_Redis___destruct
4244

43-
#define arginfo_class_Redis_bgrewriteaof arginfo_class_Redis___construct
45+
#define arginfo_class_Redis_bgrewriteaof arginfo_class_Redis___destruct
4446

4547
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_bitcount, 0, 0, 1)
4648
ZEND_ARG_INFO(0, key)
@@ -80,14 +82,14 @@ ZEND_END_ARG_INFO()
8082

8183
#define arginfo_class_Redis_bzPopMin arginfo_class_Redis_blPop
8284

83-
#define arginfo_class_Redis_clearLastError arginfo_class_Redis___construct
85+
#define arginfo_class_Redis_clearLastError arginfo_class_Redis___destruct
8486

8587
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_client, 0, 0, 1)
8688
ZEND_ARG_INFO(0, opt)
8789
ZEND_ARG_INFO(0, arg)
8890
ZEND_END_ARG_INFO()
8991

90-
#define arginfo_class_Redis_close arginfo_class_Redis___construct
92+
#define arginfo_class_Redis_close arginfo_class_Redis___destruct
9193

9294
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_command, 0, 0, 2)
9395
ZEND_ARG_INFO(0, opt)
@@ -116,7 +118,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_copy, 0, 0, 2)
116118
ZEND_ARG_INFO(0, options)
117119
ZEND_END_ARG_INFO()
118120

119-
#define arginfo_class_Redis_dbSize arginfo_class_Redis___construct
121+
#define arginfo_class_Redis_dbSize arginfo_class_Redis___destruct
120122

121123
#define arginfo_class_Redis_debug arginfo_class_Redis__prefix
122124

@@ -131,7 +133,7 @@ ZEND_END_ARG_INFO()
131133

132134
#define arginfo_class_Redis_delete arginfo_class_Redis_del
133135

134-
#define arginfo_class_Redis_discard arginfo_class_Redis___construct
136+
#define arginfo_class_Redis_discard arginfo_class_Redis___destruct
135137

136138
#define arginfo_class_Redis_dump arginfo_class_Redis__prefix
137139

@@ -151,7 +153,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_evalsha, 0, 0, 1)
151153
ZEND_ARG_INFO(0, num_keys)
152154
ZEND_END_ARG_INFO()
153155

154-
#define arginfo_class_Redis_exec arginfo_class_Redis___construct
156+
#define arginfo_class_Redis_exec arginfo_class_Redis___destruct
155157

156158
#define arginfo_class_Redis_exists arginfo_class_Redis__prefix
157159

@@ -217,40 +219,40 @@ ZEND_END_ARG_INFO()
217219

218220
#define arginfo_class_Redis_get arginfo_class_Redis__prefix
219221

220-
#define arginfo_class_Redis_getAuth arginfo_class_Redis___construct
222+
#define arginfo_class_Redis_getAuth arginfo_class_Redis___destruct
221223

222224
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_getBit, 0, 0, 2)
223225
ZEND_ARG_INFO(0, key)
224226
ZEND_ARG_INFO(0, idx)
225227
ZEND_END_ARG_INFO()
226228

227-
#define arginfo_class_Redis_getDBNum arginfo_class_Redis___construct
229+
#define arginfo_class_Redis_getDBNum arginfo_class_Redis___destruct
228230

229-
#define arginfo_class_Redis_getHost arginfo_class_Redis___construct
231+
#define arginfo_class_Redis_getHost arginfo_class_Redis___destruct
230232

231-
#define arginfo_class_Redis_getLastError arginfo_class_Redis___construct
233+
#define arginfo_class_Redis_getLastError arginfo_class_Redis___destruct
232234

233-
#define arginfo_class_Redis_getMode arginfo_class_Redis___construct
235+
#define arginfo_class_Redis_getMode arginfo_class_Redis___destruct
234236

235237
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_getOption, 0, 0, 1)
236238
ZEND_ARG_INFO(0, option)
237239
ZEND_END_ARG_INFO()
238240

239-
#define arginfo_class_Redis_getPersistentID arginfo_class_Redis___construct
241+
#define arginfo_class_Redis_getPersistentID arginfo_class_Redis___destruct
240242

241-
#define arginfo_class_Redis_getPort arginfo_class_Redis___construct
243+
#define arginfo_class_Redis_getPort arginfo_class_Redis___destruct
242244

243245
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_getRange, 0, 0, 3)
244246
ZEND_ARG_INFO(0, key)
245247
ZEND_ARG_INFO(0, start)
246248
ZEND_ARG_INFO(0, end)
247249
ZEND_END_ARG_INFO()
248250

249-
#define arginfo_class_Redis_getReadTimeout arginfo_class_Redis___construct
251+
#define arginfo_class_Redis_getReadTimeout arginfo_class_Redis___destruct
250252

251253
#define arginfo_class_Redis_getset arginfo_class_Redis_append
252254

253-
#define arginfo_class_Redis_getTimeout arginfo_class_Redis___construct
255+
#define arginfo_class_Redis_getTimeout arginfo_class_Redis___destruct
254256

255257
#define arginfo_class_Redis_hDel arginfo_class_Redis_geohash
256258

@@ -310,7 +312,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_info, 0, 0, 0)
310312
ZEND_ARG_INFO(0, opt)
311313
ZEND_END_ARG_INFO()
312314

313-
#define arginfo_class_Redis_isConnected arginfo_class_Redis___construct
315+
#define arginfo_class_Redis_isConnected arginfo_class_Redis___destruct
314316

315317
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_keys, 0, 0, 1)
316318
ZEND_ARG_INFO(0, pattern)
@@ -351,7 +353,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_lSet, 0, 0, 3)
351353
ZEND_ARG_INFO(0, value)
352354
ZEND_END_ARG_INFO()
353355

354-
#define arginfo_class_Redis_lastSave arginfo_class_Redis___construct
356+
#define arginfo_class_Redis_lastSave arginfo_class_Redis___destruct
355357

356358
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_lindex, 0, 0, 2)
357359
ZEND_ARG_INFO(0, key)
@@ -425,7 +427,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_ping, 0, 0, 0)
425427
ZEND_ARG_INFO(0, key)
426428
ZEND_END_ARG_INFO()
427429

428-
#define arginfo_class_Redis_pipeline arginfo_class_Redis___construct
430+
#define arginfo_class_Redis_pipeline arginfo_class_Redis___destruct
429431

430432
#define arginfo_class_Redis_popen arginfo_class_Redis_connect
431433

@@ -455,7 +457,7 @@ ZEND_END_ARG_INFO()
455457

456458
#define arginfo_class_Redis_rPop arginfo_class_Redis__prefix
457459

458-
#define arginfo_class_Redis_randomKey arginfo_class_Redis___construct
460+
#define arginfo_class_Redis_randomKey arginfo_class_Redis___destruct
459461

460462
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_rawcommand, 0, 0, 1)
461463
ZEND_ARG_INFO(0, command)
@@ -475,7 +477,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_restore, 0, 0, 3)
475477
ZEND_ARG_INFO(0, value)
476478
ZEND_END_ARG_INFO()
477479

478-
#define arginfo_class_Redis_role arginfo_class_Redis___construct
480+
#define arginfo_class_Redis_role arginfo_class_Redis___destruct
479481

480482
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_rpoplpush, 0, 0, 2)
481483
ZEND_ARG_INFO(0, src)
@@ -526,7 +528,7 @@ ZEND_END_ARG_INFO()
526528

527529
#define arginfo_class_Redis_sUnionStore arginfo_class_Redis_sDiffStore
528530

529-
#define arginfo_class_Redis_save arginfo_class_Redis___construct
531+
#define arginfo_class_Redis_save arginfo_class_Redis___destruct
530532

531533
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_scan, 0, 0, 1)
532534
ZEND_ARG_INFO(1, iterator)
@@ -614,7 +616,7 @@ ZEND_END_ARG_INFO()
614616

615617
#define arginfo_class_Redis_swapdb arginfo_class_Redis_rpoplpush
616618

617-
#define arginfo_class_Redis_time arginfo_class_Redis___construct
619+
#define arginfo_class_Redis_time arginfo_class_Redis___destruct
618620

619621
#define arginfo_class_Redis_ttl arginfo_class_Redis__prefix
620622

@@ -624,7 +626,7 @@ ZEND_END_ARG_INFO()
624626

625627
#define arginfo_class_Redis_unsubscribe arginfo_class_Redis_subscribe
626628

627-
#define arginfo_class_Redis_unwatch arginfo_class_Redis___construct
629+
#define arginfo_class_Redis_unwatch arginfo_class_Redis___destruct
628630

629631
#define arginfo_class_Redis_watch arginfo_class_Redis_del
630632

tests/RedisTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ protected function getFullHostPath()
104104
}
105105

106106
protected function newInstance() {
107-
$r = new Redis();
108-
109-
$r->connect($this->getHost(), $this->getPort());
107+
$r = new Redis([
108+
'host' => $this->getHost(),
109+
'port' => $this->getPort(),
110+
]);
110111

111112
if($this->getAuth()) {
112113
$this->assertTrue($r->auth($this->getAuth()));

0 commit comments

Comments
 (0)