44
55use React \Cache \ArrayCache ;
66use React \Cache \CacheInterface ;
7- use React \Dns \Query \Executor ;
8- use React \Dns \Query \CachedExecutor ;
9- use React \Dns \Query \RecordCache ;
7+ use React \Dns \Config \HostsFile ;
108use React \Dns \Protocol \Parser ;
119use React \Dns \Protocol \BinaryDumper ;
12- use React \EventLoop \LoopInterface ;
10+ use React \Dns \Query \CachedExecutor ;
11+ use React \Dns \Query \Executor ;
12+ use React \Dns \Query \ExecutorInterface ;
13+ use React \Dns \Query \HostsFileExecutor ;
14+ use React \Dns \Query \RecordCache ;
1315use React \Dns \Query \RetryExecutor ;
1416use React \Dns \Query \TimeoutExecutor ;
17+ use React \EventLoop \LoopInterface ;
1518
1619class Factory
1720{
1821 public function create ($ nameserver , LoopInterface $ loop )
1922 {
2023 $ nameserver = $ this ->addPortToServerIfMissing ($ nameserver );
21- $ executor = $ this ->createRetryExecutor ($ loop );
24+ $ executor = $ this ->decorateHostsFileExecutor ( $ this -> createRetryExecutor ($ loop) );
2225
2326 return new Resolver ($ nameserver , $ executor );
2427 }
@@ -30,11 +33,30 @@ public function createCached($nameserver, LoopInterface $loop, CacheInterface $c
3033 }
3134
3235 $ nameserver = $ this ->addPortToServerIfMissing ($ nameserver );
33- $ executor = $ this ->createCachedExecutor ($ loop , $ cache );
36+ $ executor = $ this ->decorateHostsFileExecutor ( $ this -> createCachedExecutor ($ loop , $ cache) );
3437
3538 return new Resolver ($ nameserver , $ executor );
3639 }
3740
41+ /**
42+ * Tries to load the hosts file and decorates the given executor on success
43+ *
44+ * @param ExecutorInterface $executor
45+ * @return ExecutorInterface
46+ * @codeCoverageIgnore
47+ */
48+ private function decorateHostsFileExecutor (ExecutorInterface $ executor )
49+ {
50+ try {
51+ $ hosts = HostsFile::loadFromPathBlocking ();
52+ } catch (\RuntimeException $ e ) {
53+ // ignore this file if it can not be loaded
54+ return $ executor ;
55+ }
56+
57+ return new HostsFileExecutor ($ hosts , $ executor );
58+ }
59+
3860 protected function createExecutor (LoopInterface $ loop )
3961 {
4062 return new TimeoutExecutor (
0 commit comments