2424 */
2525class RedisHandler extends BaseHandler
2626{
27- private const DEFAULT_PORT = 6379 ;
27+ private const DEFAULT_PORT = 6379 ;
28+ private const DEFAULT_PROTOCOL = 'tcp ' ;
2829
2930 /**
3031 * phpRedis instance
@@ -102,20 +103,21 @@ protected function setSavePath(): void
102103 throw SessionException::forEmptySavepath ();
103104 }
104105
105- if (preg_match ('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?# ' , $ this ->savePath , $ matches )) {
106- if (! isset ($ matches [3 ])) {
107- $ matches [3 ] = '' ; // Just to avoid undefined index notices below
106+ if (preg_match ('#(?:( tcp|tls) ://)?([^:?]+)(?:\:(\d+))?(\?.+)?# ' , $ this ->savePath , $ matches )) {
107+ if (! isset ($ matches [4 ])) {
108+ $ matches [4 ] = '' ; // Just to avoid undefined index notices below
108109 }
109110
110111 $ this ->savePath = [
111- 'host ' => $ matches [1 ],
112- 'port ' => empty ($ matches [2 ]) ? self ::DEFAULT_PORT : $ matches [2 ],
113- 'password ' => preg_match ('#auth=([^\s&]+)# ' , $ matches [3 ], $ match ) ? $ match [1 ] : null ,
114- 'database ' => preg_match ('#database=(\d+)# ' , $ matches [3 ], $ match ) ? (int ) $ match [1 ] : 0 ,
115- 'timeout ' => preg_match ('#timeout=(\d+\.\d+|\d+)# ' , $ matches [3 ], $ match ) ? (float ) $ match [1 ] : 0.0 ,
112+ 'protocol ' => ! empty ($ matches [1 ]) ? $ matches [1 ] : self ::DEFAULT_PROTOCOL ,
113+ 'host ' => $ matches [2 ],
114+ 'port ' => empty ($ matches [3 ]) ? self ::DEFAULT_PORT : $ matches [3 ],
115+ 'password ' => preg_match ('#auth=([^\s&]+)# ' , $ matches [4 ], $ match ) ? $ match [1 ] : null ,
116+ 'database ' => preg_match ('#database=(\d+)# ' , $ matches [4 ], $ match ) ? (int ) $ match [1 ] : 0 ,
117+ 'timeout ' => preg_match ('#timeout=(\d+\.\d+|\d+)# ' , $ matches [4 ], $ match ) ? (float ) $ match [1 ] : 0.0 ,
116118 ];
117119
118- preg_match ('#prefix=([^\s&]+)# ' , $ matches [3 ], $ match ) && $ this ->keyPrefix = $ match [1 ];
120+ preg_match ('#prefix=([^\s&]+)# ' , $ matches [4 ], $ match ) && $ this ->keyPrefix = $ match [1 ];
119121 } else {
120122 throw SessionException::forInvalidSavePathFormat ($ this ->savePath );
121123 }
@@ -135,7 +137,7 @@ public function open($path, $name): bool
135137
136138 $ redis = new Redis ();
137139
138- if (! $ redis ->connect ($ this ->savePath ['host ' ], ($ this ->savePath ['host ' ][0 ] === '/ ' ? 0 : $ this ->savePath ['port ' ]), $ this ->savePath ['timeout ' ])) {
140+ if (! $ redis ->connect ($ this ->savePath ['protocol ' ] . ' :// ' . $ this -> savePath [ ' host ' ], ($ this ->savePath ['host ' ][0 ] === '/ ' ? 0 : $ this ->savePath ['port ' ]), $ this ->savePath ['timeout ' ])) {
139141 $ this ->logger ->error ('Session: Unable to connect to Redis with the configured settings. ' );
140142 } elseif (isset ($ this ->savePath ['password ' ]) && ! $ redis ->auth ($ this ->savePath ['password ' ])) {
141143 $ this ->logger ->error ('Session: Unable to authenticate to Redis instance. ' );
0 commit comments