8
8
9
9
class Factory
10
10
{
11
- public function createClient (LoopInterface $ loop , Resolver $ resolver , $ host , $ port )
11
+ protected $ loop ;
12
+ protected $ resolver ;
13
+
14
+ public function __construct (LoopInterface $ loop , Resolver $ resolver = null )
15
+ {
16
+ $ this ->loop = $ loop ;
17
+ $ this ->resolver = $ resolver ;
18
+ }
19
+
20
+ public function createClient ($ host , $ port )
12
21
{
13
22
$ factory = $ this ;
14
- return $ this ->resolve ($ resolver , $ host )->then (function ($ ip ) use ($ loop , $ port , $ factory ) {
23
+ $ loop = $ this ->loop ;
24
+ return $ this ->resolve ($ host )->then (function ($ ip ) use ($ loop , $ port , $ factory ) {
15
25
$ address = $ factory ->createAddress ($ ip , $ port );
16
26
$ socket = stream_socket_client ('udp:// ' . $ address , $ errno , $ errstr );
17
27
if (!$ socket ) {
@@ -23,18 +33,18 @@ public function createClient(LoopInterface $loop, Resolver $resolver, $host, $po
23
33
});
24
34
}
25
35
26
- public function createServer (LoopInterface $ loop , $ port , $ host = '127.0.0.1 ' )
36
+ public function createServer ($ port , $ host = '127.0.0.1 ' )
27
37
{
28
38
$ address = $ this ->createAddress ($ host , $ port );
29
39
30
40
$ socket = stream_socket_server ("udp:// " . $ address , $ errno , $ errstr , STREAM_SERVER_BIND );
31
41
if (!$ socket ) {
32
42
die ("$ errstr ( $ errno) " );
33
43
}
34
- return When::resolve (new Server ($ loop , $ socket , $ address ));
44
+ return When::resolve (new Server ($ this -> loop , $ socket , $ address ));
35
45
}
36
46
37
- protected function resolve ($ resolver , $ host )
47
+ protected function resolve ($ host )
38
48
{
39
49
// there's no need to resolve if the host is already given as an IP address
40
50
if (false !== filter_var ($ host , FILTER_VALIDATE_IP )) {
@@ -44,7 +54,11 @@ protected function resolve($resolver, $host)
44
54
if ($ host === 'localhost ' ) {
45
55
return When::resolve ('127.0.0.1 ' );
46
56
}
47
- return $ resolver ->resolve ($ host );
57
+
58
+ if ($ resolver === null ) {
59
+ return When::reject (\Exception ('No resolver given in order to get IP address for given hostname ' ));
60
+ }
61
+ return $ this ->resolver ->resolve ($ host );
48
62
}
49
63
50
64
public function createAddress ($ host , $ port )
0 commit comments