9
9
use React \SocketClient \Connector ;
10
10
use React \Dns \Resolver \Factory as ResolverFactory ;
11
11
use InvalidArgumentException ;
12
- use BadMethodCallException ;
13
- use Exception ;
14
12
use React \EventLoop \LoopInterface ;
15
13
use React \Promise ;
16
14
@@ -42,17 +40,21 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
42
40
*/
43
41
public function createClient ($ target = null )
44
42
{
45
- $ auth = $ this ->getAuthFromTarget ($ target );
46
- $ db = $ this ->getDatabaseFromTarget ($ target );
43
+ try {
44
+ $ parts = $ this ->parseUrl ($ target );
45
+ } catch (InvalidArgumentException $ e ) {
46
+ return Promise \reject ($ e );
47
+ }
48
+
47
49
$ protocol = $ this ->protocol ;
48
50
49
- $ promise = $ this ->connect ( $ target )->then (function (Stream $ stream ) use ($ protocol ) {
51
+ $ promise = $ this ->connector -> create ( $ parts [ ' host ' ], $ parts [ ' port ' ] )->then (function (Stream $ stream ) use ($ protocol ) {
50
52
return new StreamingClient ($ stream , $ protocol ->createResponseParser (), $ protocol ->createSerializer ());
51
53
});
52
54
53
- if ($ auth !== null ) {
54
- $ promise = $ promise ->then (function (StreamingClient $ client ) use ($ auth ) {
55
- return $ client ->auth ($ auth )->then (
55
+ if (isset ( $ parts [ ' auth ' ]) ) {
56
+ $ promise = $ promise ->then (function (StreamingClient $ client ) use ($ parts ) {
57
+ return $ client ->auth ($ parts [ ' auth ' ] )->then (
56
58
function () use ($ client ) {
57
59
return $ client ;
58
60
},
@@ -64,9 +66,9 @@ function ($error) use ($client) {
64
66
});
65
67
}
66
68
67
- if ($ db !== null ) {
68
- $ promise = $ promise ->then (function (StreamingClient $ client ) use ($ db ) {
69
- return $ client ->select ($ db )->then (
69
+ if (isset ( $ parts [ ' db ' ]) ) {
70
+ $ promise = $ promise ->then (function (StreamingClient $ client ) use ($ parts ) {
71
+ return $ client ->select ($ parts [ ' db ' ] )->then (
70
72
function () use ($ client ) {
71
73
return $ client ;
72
74
},
@@ -81,6 +83,11 @@ function ($error) use ($client) {
81
83
return $ promise ;
82
84
}
83
85
86
+ /**
87
+ * @param string|null $target
88
+ * @return array with keys host, port, auth and db
89
+ * @throws InvalidArgumentException
90
+ */
84
91
private function parseUrl ($ target )
85
92
{
86
93
if ($ target === null ) {
@@ -92,54 +99,35 @@ private function parseUrl($target)
92
99
93
100
$ parts = parse_url ($ target );
94
101
if ($ parts === false || !isset ($ parts ['host ' ]) || $ parts ['scheme ' ] !== 'tcp ' ) {
95
- throw new Exception ('Given URL can not be parsed ' );
102
+ throw new InvalidArgumentException ('Given URL can not be parsed ' );
96
103
}
97
104
98
105
if (!isset ($ parts ['port ' ])) {
99
- $ parts ['port ' ] = ' 6379 ' ;
106
+ $ parts ['port ' ] = 6379 ;
100
107
}
101
108
102
109
if ($ parts ['host ' ] === 'localhost ' ) {
103
110
$ parts ['host ' ] = '127.0.0.1 ' ;
104
111
}
105
112
106
- return $ parts ;
107
- }
108
-
109
- private function connect ($ target )
110
- {
111
- try {
112
- $ parts = $ this ->parseUrl ($ target );
113
- } catch (Exception $ e ) {
114
- return Promise \reject ($ e );
115
- }
116
-
117
- return $ this ->connector ->create ($ parts ['host ' ], $ parts ['port ' ]);
118
- }
119
-
120
- private function getAuthFromTarget ($ target )
121
- {
122
113
$ auth = null ;
123
- $ parts = parse_url ($ target );
124
114
if (isset ($ parts ['user ' ])) {
125
115
$ auth = $ parts ['user ' ];
126
116
}
127
117
if (isset ($ parts ['pass ' ])) {
128
118
$ auth .= ': ' . $ parts ['pass ' ];
129
119
}
120
+ if ($ auth !== null ) {
121
+ $ parts ['auth ' ] = $ auth ;
122
+ }
130
123
131
- return $ auth ;
132
- }
133
-
134
- private function getDatabaseFromTarget ($ target )
135
- {
136
- $ db = null ;
137
- $ path = parse_url ($ target , PHP_URL_PATH );
138
- if ($ path !== null && $ path !== '' ) {
124
+ if (isset ($ parts ['path ' ]) && $ parts ['path ' ] !== '' ) {
139
125
// skip first slash
140
- $ db = substr ($ path , 1 );
126
+ $ parts [ ' db ' ] = substr ($ parts [ ' path ' ] , 1 );
141
127
}
142
128
143
- return $ db ;
129
+ unset($ parts ['scheme ' ], $ parts ['user ' ], $ parts ['pass ' ], $ parts ['path ' ]);
130
+
131
+ return $ parts ;
144
132
}
145
133
}
0 commit comments