@@ -118,6 +118,9 @@ public static function createConnection($dsn, array $options = [])
118
118
119
119
$ query = $ hosts = [];
120
120
121
+ $ tls = 'rediss ' === $ scheme ;
122
+ $ tcpScheme = $ tls ? 'tls ' : 'tcp ' ;
123
+
121
124
if (isset ($ params ['query ' ])) {
122
125
parse_str ($ params ['query ' ], $ query );
123
126
@@ -130,9 +133,9 @@ public static function createConnection($dsn, array $options = [])
130
133
parse_str ($ parameters , $ parameters );
131
134
}
132
135
if (false === $ i = strrpos ($ host , ': ' )) {
133
- $ hosts [$ host ] = ['scheme ' => ' tcp ' , 'host ' => $ host , 'port ' => 6379 ] + $ parameters ;
136
+ $ hosts [$ host ] = ['scheme ' => $ tcpScheme , 'host ' => $ host , 'port ' => 6379 ] + $ parameters ;
134
137
} elseif ($ port = (int ) substr ($ host , 1 + $ i )) {
135
- $ hosts [$ host ] = ['scheme ' => ' tcp ' , 'host ' => substr ($ host , 0 , $ i ), 'port ' => $ port ] + $ parameters ;
138
+ $ hosts [$ host ] = ['scheme ' => $ tcpScheme , 'host ' => substr ($ host , 0 , $ i ), 'port ' => $ port ] + $ parameters ;
136
139
} else {
137
140
$ hosts [$ host ] = ['scheme ' => 'unix ' , 'path ' => substr ($ host , 0 , $ i )] + $ parameters ;
138
141
}
@@ -148,7 +151,7 @@ public static function createConnection($dsn, array $options = [])
148
151
}
149
152
150
153
if (isset ($ params ['host ' ])) {
151
- array_unshift ($ hosts , ['scheme ' => ' tcp ' , 'host ' => $ params ['host ' ], 'port ' => $ params ['port ' ] ?? 6379 ]);
154
+ array_unshift ($ hosts , ['scheme ' => $ tcpScheme , 'host ' => $ params ['host ' ], 'port ' => $ params ['port ' ] ?? 6379 ]);
152
155
} else {
153
156
array_unshift ($ hosts , ['scheme ' => 'unix ' , 'path ' => $ params ['path ' ]]);
154
157
}
@@ -174,9 +177,16 @@ public static function createConnection($dsn, array $options = [])
174
177
$ connect = $ params ['persistent ' ] || $ params ['persistent_id ' ] ? 'pconnect ' : 'connect ' ;
175
178
$ redis = new $ class ();
176
179
177
- $ initializer = static function ($ redis ) use ($ connect , $ params , $ dsn , $ auth , $ hosts ) {
180
+ $ initializer = static function ($ redis ) use ($ connect , $ params , $ dsn , $ auth , $ hosts , $ tls ) {
181
+ $ host = $ hosts [0 ]['host ' ] ?? $ hosts [0 ]['path ' ];
182
+ $ port = $ hosts [0 ]['port ' ] ?? null ;
183
+
184
+ if (isset ($ hosts [0 ]['host ' ]) && $ tls ) {
185
+ $ host = 'tls:// ' .$ host ;
186
+ }
187
+
178
188
try {
179
- @$ redis ->{$ connect }($ hosts [ 0 ][ ' host ' ] ?? $ hosts [ 0 ][ ' path ' ] , $ hosts [ 0 ][ ' port ' ] ?? null , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ], $ params ['read_timeout ' ]);
189
+ @$ redis ->{$ connect }($ host , $ port , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ], $ params ['read_timeout ' ]);
180
190
181
191
set_error_handler (function ($ type , $ msg ) use (&$ error ) { $ error = $ msg ; });
182
192
$ isConnected = $ redis ->isConnected ();
@@ -210,7 +220,11 @@ public static function createConnection($dsn, array $options = [])
210
220
}
211
221
} elseif (is_a ($ class , \RedisArray::class, true )) {
212
222
foreach ($ hosts as $ i => $ host ) {
213
- $ hosts [$ i ] = 'tcp ' === $ host ['scheme ' ] ? $ host ['host ' ].': ' .$ host ['port ' ] : $ host ['path ' ];
223
+ switch ($ host ['scheme ' ]) {
224
+ case 'tcp ' : $ hosts [$ i ] = $ host ['host ' ].': ' .$ host ['port ' ]; break ;
225
+ case 'tls ' : $ hosts [$ i ] = 'tls:// ' .$ host ['host ' ].': ' .$ host ['port ' ]; break ;
226
+ default : $ hosts [$ i ] = $ host ['path ' ];
227
+ }
214
228
}
215
229
$ params ['lazy_connect ' ] = $ params ['lazy ' ] ?? true ;
216
230
$ params ['connect_timeout ' ] = $ params ['timeout ' ];
@@ -227,7 +241,11 @@ public static function createConnection($dsn, array $options = [])
227
241
} elseif (is_a ($ class , \RedisCluster::class, true )) {
228
242
$ initializer = static function () use ($ class , $ params , $ dsn , $ hosts ) {
229
243
foreach ($ hosts as $ i => $ host ) {
230
- $ hosts [$ i ] = 'tcp ' === $ host ['scheme ' ] ? $ host ['host ' ].': ' .$ host ['port ' ] : $ host ['path ' ];
244
+ switch ($ host ['scheme ' ]) {
245
+ case 'tcp ' : $ hosts [$ i ] = $ host ['host ' ].': ' .$ host ['port ' ]; break ;
246
+ case 'tls ' : $ hosts [$ i ] = 'tls:// ' .$ host ['host ' ].': ' .$ host ['port ' ]; break ;
247
+ default : $ hosts [$ i ] = $ host ['path ' ];
248
+ }
231
249
}
232
250
233
251
try {
0 commit comments