16
16
use Symfony \Component \Messenger \Exception \InvalidArgumentException ;
17
17
use Symfony \Component \Messenger \Exception \LogicException ;
18
18
use Symfony \Component \Messenger \Exception \TransportException ;
19
+ use Symfony \Contracts \Service \ResetInterface ;
19
20
20
21
/**
21
22
* A Redis connection.
28
29
*
29
30
* @final
30
31
*/
31
- class Connection
32
+ class Connection implements ResetInterface
32
33
{
33
34
private const DEFAULT_OPTIONS = [
34
35
'host ' => '127.0.0.1 ' ,
@@ -55,7 +56,8 @@ class Connection
55
56
'ssl ' => null , // see https://php.net/context.ssl
56
57
];
57
58
58
- private \Redis |Relay |\RedisCluster |\Closure $ redis ;
59
+ private \Redis |Relay |\RedisCluster |null $ redis = null ;
60
+ private \Closure $ redisInitializer ;
59
61
private string $ stream ;
60
62
private string $ queue ;
61
63
private string $ group ;
@@ -112,9 +114,9 @@ public function __construct(array $options, \Redis|Relay|\RedisCluster|null $red
112
114
113
115
if ((\is_array ($ host ) && null === $ sentinelMaster ) || $ redis instanceof \RedisCluster) {
114
116
$ hosts = \is_string ($ host ) ? [$ host .': ' .$ port ] : $ host ; // Always ensure we have an array
115
- $ this ->redis = static fn () => self ::initializeRedisCluster ($ redis , $ hosts , $ auth , $ options );
117
+ $ this ->redisInitializer = static fn () => self ::initializeRedisCluster ($ redis , $ hosts , $ auth , $ options );
116
118
} else {
117
- $ this ->redis = static function () use ($ redis , $ sentinelMaster , $ host , $ port , $ options , $ auth ) {
119
+ $ this ->redisInitializer = static function () use ($ redis , $ sentinelMaster , $ host , $ port , $ options , $ auth ) {
118
120
if (null !== $ sentinelMaster ) {
119
121
$ sentinelClass = \extension_loaded ('redis ' ) ? \RedisSentinel::class : Sentinel::class;
120
122
$ hostIndex = 0 ;
@@ -737,10 +739,15 @@ private function rawCommand(string $command, ...$arguments): mixed
737
739
738
740
private function getRedis (): \Redis |Relay |\RedisCluster
739
741
{
740
- if ($ this ->redis instanceof \Closure ) {
741
- $ this ->redis = ($ this ->redis )();
742
+ if (! $ this ->redis ) {
743
+ $ this ->redis = ($ this ->redisInitializer )();
742
744
}
743
745
744
746
return $ this ->redis ;
745
747
}
748
+
749
+ public function reset (): void
750
+ {
751
+ $ this ->redis = null ;
752
+ }
746
753
}
0 commit comments