File tree Expand file tree Collapse file tree 2 files changed +26
-15
lines changed Expand file tree Collapse file tree 2 files changed +26
-15
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace React \Datagram ;
4
4
5
- use React \EventLoop \LoopInterface ;
5
+ use React \Datagram \Socket ;
6
+ use React \Dns \Resolver \Factory as DnsFactory ;
6
7
use React \Dns \Resolver \Resolver ;
8
+ use React \EventLoop \LoopInterface ;
7
9
use React \Promise ;
8
- use React \Datagram \Socket ;
9
- use \Exception ;
10
10
use React \Promise \CancellablePromiseInterface ;
11
+ use \Exception ;
11
12
12
13
class Factory
13
14
{
14
15
protected $ loop ;
15
16
protected $ resolver ;
16
17
18
+ /**
19
+ *
20
+ * @param LoopInterface $loop
21
+ * @param Resolver|null $resolver Resolver instance to use. Will otherwise
22
+ * default to using Google's public DNS 8.8.8.8
23
+ */
17
24
public function __construct (LoopInterface $ loop , Resolver $ resolver = null )
18
25
{
26
+ if ($ resolver === null ) {
27
+ $ factory = new DnsFactory ();
28
+ $ resolver = $ factory ->create ('8.8.8.8 ' , $ loop );
29
+ }
30
+
19
31
$ this ->loop = $ loop ;
20
32
$ this ->resolver = $ resolver ;
21
33
}
@@ -99,10 +111,6 @@ protected function resolveHost($host)
99
111
return Promise \resolve ($ host );
100
112
}
101
113
102
- if ($ this ->resolver === null ) {
103
- return Promise \reject (new Exception ('No resolver given in order to get IP address for given hostname ' ));
104
- }
105
-
106
114
$ promise = $ this ->resolver ->resolve ($ host );
107
115
108
116
// wrap DNS lookup in order to control cancellation behavior
Original file line number Diff line number Diff line change @@ -54,6 +54,17 @@ public function testCreateClientLocalhost()
54
54
$ capturedClient ->close ();
55
55
}
56
56
57
+ public function testCreateClientLocalhostWithDefaultResolver ()
58
+ {
59
+ $ this ->resolver = null ;
60
+ $ this ->factory = new Factory ($ this ->loop );
61
+
62
+ $ promise = $ this ->factory ->createClient ('localhost:12345 ' );
63
+
64
+ $ capturedClient = Block \await ($ promise , $ this ->loop );
65
+ $ capturedClient ->close ();
66
+ }
67
+
57
68
public function testCreateClientIpv6 ()
58
69
{
59
70
$ promise = $ this ->factory ->createClient ('[::1]:12345 ' );
@@ -126,14 +137,6 @@ public function testCreateClientWithHostnameWillRejectIfResolverRejects()
126
137
Block \await ($ this ->factory ->createClient ('example.com:0 ' ), $ this ->loop );
127
138
}
128
139
129
- public function testCreateClientWithHostnameWillRejectIfNoResolverIsGiven ()
130
- {
131
- $ this ->factory = new Factory ($ this ->loop );
132
-
133
- $ this ->setExpectedException ('Exception ' );
134
- Block \await ($ this ->factory ->createClient ('example.com:0 ' ), $ this ->loop );
135
- }
136
-
137
140
/**
138
141
* @expectedException Exception
139
142
* @expectedExceptionMessage Unable to create client socket
You can’t perform that action at this time.
0 commit comments