10
10
use React \Socket \ConnectionInterface ;
11
11
use React \Socket \Connector ;
12
12
use React \Socket \ConnectorInterface ;
13
+ use function React \Promise \reject ;
14
+ use function React \Promise \Timer \timeout ;
13
15
14
16
class Factory
15
17
{
@@ -30,7 +32,7 @@ class Factory
30
32
public function __construct (LoopInterface $ loop = null , ConnectorInterface $ connector = null , ProtocolFactory $ protocol = null )
31
33
{
32
34
$ this ->loop = $ loop ?: Loop::get ();
33
- $ this ->connector = $ connector ?: new Connector (array () , $ this ->loop );
35
+ $ this ->connector = $ connector ?: new Connector ([] , $ this ->loop );
34
36
$ this ->protocol = $ protocol ?: new ProtocolFactory ();
35
37
}
36
38
@@ -54,18 +56,18 @@ public function createClient($uri)
54
56
$ parts = parse_url ($ uri );
55
57
}
56
58
57
- $ uri = preg_replace (array ( '/(:)[^:\/]*(@)/ ' , '/([?&]password=).*?($|&)/ ' ) , '$1***$2 ' , $ uri );
58
- if ($ parts === false || !isset ($ parts ['scheme ' ], $ parts ['host ' ]) || !in_array ($ parts ['scheme ' ], array ( 'redis ' , 'rediss ' , 'redis+unix ' ) )) {
59
- return \ React \ Promise \ reject (new \InvalidArgumentException (
59
+ $ uri = preg_replace ([ '/(:)[^:\/]*(@)/ ' , '/([?&]password=).*?($|&)/ ' ] , '$1***$2 ' , $ uri );
60
+ if ($ parts === false || !isset ($ parts ['scheme ' ], $ parts ['host ' ]) || !in_array ($ parts ['scheme ' ], [ 'redis ' , 'rediss ' , 'redis+unix ' ] )) {
61
+ return reject (new \InvalidArgumentException (
60
62
'Invalid Redis URI given (EINVAL) ' ,
61
63
defined ('SOCKET_EINVAL ' ) ? SOCKET_EINVAL : 22
62
64
));
63
65
}
64
66
65
- $ args = array () ;
66
- parse_str (isset ( $ parts ['query ' ]) ? $ parts [ ' query ' ] : '' , $ args );
67
+ $ args = [] ;
68
+ parse_str ($ parts ['query ' ] ?? '' , $ args );
67
69
68
- $ authority = $ parts ['host ' ] . ': ' . (isset ( $ parts ['port ' ]) ? $ parts [ ' port ' ] : 6379 );
70
+ $ authority = $ parts ['host ' ] . ': ' . ($ parts ['port ' ] ?? 6379 );
69
71
if ($ parts ['scheme ' ] === 'rediss ' ) {
70
72
$ authority = 'tls:// ' . $ authority ;
71
73
} elseif ($ parts ['scheme ' ] === 'redis+unix ' ) {
@@ -88,9 +90,8 @@ public function createClient($uri)
88
90
$ connecting ->cancel ();
89
91
});
90
92
91
- $ protocol = $ this ->protocol ;
92
- $ promise = $ connecting ->then (function (ConnectionInterface $ stream ) use ($ protocol ) {
93
- return new StreamingClient ($ stream , $ protocol ->createResponseParser (), $ protocol ->createSerializer ());
93
+ $ promise = $ connecting ->then (function (ConnectionInterface $ stream ) {
94
+ return new StreamingClient ($ stream , $ this ->protocol ->createResponseParser (), $ this ->protocol ->createSerializer ());
94
95
}, function (\Exception $ e ) use ($ uri ) {
95
96
throw new \RuntimeException (
96
97
'Connection to ' . $ uri . ' failed: ' . $ e ->getMessage (),
@@ -100,9 +101,8 @@ public function createClient($uri)
100
101
});
101
102
102
103
// use `?password=secret` query or `user:secret@host` password form URL
103
- $ pass = isset ($ args ['password ' ]) ? $ args ['password ' ] : (isset ($ parts ['pass ' ]) ? rawurldecode ($ parts ['pass ' ]) : null );
104
104
if (isset ($ args ['password ' ]) || isset ($ parts ['pass ' ])) {
105
- $ pass = isset ( $ args ['password ' ]) ? $ args [ ' password ' ] : rawurldecode ($ parts ['pass ' ]);
105
+ $ pass = $ args ['password ' ] ?? rawurldecode ($ parts ['pass ' ]);
106
106
$ promise = $ promise ->then (function (StreamingClient $ redis ) use ($ pass , $ uri ) {
107
107
return $ redis ->auth ($ pass )->then (
108
108
function () use ($ redis ) {
@@ -130,7 +130,7 @@ function (\Exception $e) use ($redis, $uri) {
130
130
131
131
// use `?db=1` query or `/1` path (skip first slash)
132
132
if (isset ($ args ['db ' ]) || (isset ($ parts ['path ' ]) && $ parts ['path ' ] !== '/ ' )) {
133
- $ db = isset ( $ args ['db ' ]) ? $ args [ ' db ' ] : substr ($ parts ['path ' ], 1 );
133
+ $ db = $ args ['db ' ] ?? substr ($ parts ['path ' ], 1 );
134
134
$ promise = $ promise ->then (function (StreamingClient $ redis ) use ($ db , $ uri ) {
135
135
return $ redis ->select ($ db )->then (
136
136
function () use ($ redis ) {
@@ -159,15 +159,15 @@ function (\Exception $e) use ($redis, $uri) {
159
159
});
160
160
}
161
161
162
- $ promise ->then (array ( $ deferred , 'resolve ' ), array ( $ deferred , 'reject ' ) );
162
+ $ promise ->then ([ $ deferred , 'resolve ' ], [ $ deferred , 'reject ' ] );
163
163
164
164
// use timeout from explicit ?timeout=x parameter or default to PHP's default_socket_timeout (60)
165
165
$ timeout = isset ($ args ['timeout ' ]) ? (float ) $ args ['timeout ' ] : (int ) ini_get ("default_socket_timeout " );
166
166
if ($ timeout < 0 ) {
167
167
return $ deferred ->promise ();
168
168
}
169
169
170
- return \ React \ Promise \ Timer \ timeout ($ deferred ->promise (), $ timeout , $ this ->loop )->then (null , function ($ e ) use ($ uri ) {
170
+ return timeout ($ deferred ->promise (), $ timeout , $ this ->loop )->then (null , function ($ e ) use ($ uri ) {
171
171
if ($ e instanceof TimeoutException) {
172
172
throw new \RuntimeException (
173
173
'Connection to ' . $ uri . ' timed out after ' . $ e ->getTimeout () . ' seconds (ETIMEDOUT) ' ,
0 commit comments