@@ -159,13 +159,17 @@ public static function createConnection($dsn, array $options = [])
159
159
throw new InvalidArgumentException (sprintf ('Invalid Redis DSN: "%s". ' , $ dsn ));
160
160
}
161
161
162
- if (isset ($ params ['redis_sentinel ' ]) && !class_exists (\Predis \Client::class)) {
163
- throw new CacheException (sprintf ('Redis Sentinel support requires the "predis/predis" package: "%s". ' , $ dsn ));
162
+ $ params += $ query + $ options + self ::$ defaultConnectionOptions ;
163
+
164
+ if (isset ($ params ['redis_sentinel ' ]) && (!class_exists (\Predis \Client::class) || !class_exists (\RedisSentinel::class))) {
165
+ throw new CacheException (sprintf ('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher: "%s". ' , $ dsn ));
164
166
}
165
167
166
- $ params += $ query + $ options + self ::$ defaultConnectionOptions ;
168
+ if ($ params ['redis_cluster ' ] && isset ($ params ['redis_sentinel ' ])) {
169
+ throw new InvalidArgumentException (sprintf ('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s". ' , $ dsn ));
170
+ }
167
171
168
- if (null === $ params ['class ' ] && ! isset ( $ params [ ' redis_sentinel ' ]) && \extension_loaded ('redis ' )) {
172
+ if (null === $ params ['class ' ] && \extension_loaded ('redis ' )) {
169
173
$ class = $ params ['redis_cluster ' ] ? \RedisCluster::class : (1 < \count ($ hosts ) ? \RedisArray::class : \Redis::class);
170
174
} else {
171
175
$ class = null === $ params ['class ' ] ? \Predis \Client::class : $ params ['class ' ];
@@ -176,8 +180,19 @@ public static function createConnection($dsn, array $options = [])
176
180
$ redis = new $ class ();
177
181
178
182
$ initializer = static function ($ redis ) use ($ connect , $ params , $ dsn , $ auth , $ hosts ) {
183
+ $ host = $ hosts [0 ]['host ' ] ?? $ hosts [0 ]['path ' ];
184
+ $ port = $ hosts [0 ]['port ' ] ?? null ;
185
+
186
+ if (isset ($ params ['redis_sentinel ' ])) {
187
+ $ sentinel = new \RedisSentinel ($ host , $ port , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ]);
188
+
189
+ if (![$ host , $ port ] = $ sentinel ->getMasterAddrByName ($ params ['redis_sentinel ' ])) {
190
+ throw new InvalidArgumentException (sprintf ('Failed to retrieve master information from master name "%s" and address "%s:%d". ' , $ params ['redis_sentinel ' ], $ host , $ port ));
191
+ }
192
+ }
193
+
179
194
try {
180
- @$ redis ->{$ connect }($ hosts [ 0 ][ ' host ' ] ?? $ hosts [ 0 ][ ' path ' ] , $ hosts [ 0 ][ ' port ' ] ?? null , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ]);
195
+ @$ redis ->{$ connect }($ host , $ port , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ]);
181
196
182
197
set_error_handler (function ($ type , $ msg ) use (&$ error ) { $ error = $ msg ; });
183
198
$ isConnected = $ redis ->isConnected ();
@@ -254,9 +269,6 @@ public static function createConnection($dsn, array $options = [])
254
269
} elseif (is_a ($ class , \Predis \ClientInterface::class, true )) {
255
270
if ($ params ['redis_cluster ' ]) {
256
271
$ params ['cluster ' ] = 'redis ' ;
257
- if (isset ($ params ['redis_sentinel ' ])) {
258
- throw new InvalidArgumentException (sprintf ('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s". ' , $ dsn ));
259
- }
260
272
} elseif (isset ($ params ['redis_sentinel ' ])) {
261
273
$ params ['replication ' ] = 'sentinel ' ;
262
274
$ params ['service ' ] = $ params ['redis_sentinel ' ];
0 commit comments