Skip to content

Commit 81d5781

Browse files
authored
Merge pull request #93 from clue-labs/config
Add Config::loadSystemConfigBlocking() to load default system config
2 parents c74a0af + 3963af3 commit 81d5781

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ names, baby!
2828

2929
```php
3030
$loop = React\EventLoop\Factory::create();
31+
32+
$config = React\Dns\Config\Config::loadSystemConfigBlocking();
33+
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
34+
3135
$factory = new React\Dns\Resolver\Factory();
32-
$dns = $factory->create('8.8.8.8', $loop);
36+
$dns = $factory->create($server, $loop);
3337

3438
$dns->resolve('igor.io')->then(function ($ip) {
3539
echo "Host: $ip\n";
@@ -40,6 +44,14 @@ $loop->run();
4044

4145
See also the [first example](examples).
4246

47+
The `Config` class can be used to load the system default config. This is an
48+
operation that may access the filesystem and block. Ideally, this method should
49+
thus be executed only once before the loop starts and not repeatedly while it is
50+
running.
51+
Note that this class may return an *empty* configuration if the system config
52+
can not be loaded. As such, you'll likely want to apply a default nameserver
53+
as above if none can be found.
54+
4355
> Note that the factory loads the hosts file from the filesystem once when
4456
creating the resolver instance.
4557
Ideally, this method should thus be executed only once before the loop starts
@@ -61,8 +73,12 @@ You can cache results by configuring the resolver to use a `CachedExecutor`:
6173

6274
```php
6375
$loop = React\EventLoop\Factory::create();
76+
77+
$config = React\Dns\Config\Config::loadSystemConfigBlocking();
78+
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
79+
6480
$factory = new React\Dns\Resolver\Factory();
65-
$dns = $factory->createCached('8.8.8.8', $loop);
81+
$dns = $factory->createCached($server, $loop);
6682

6783
$dns->resolve('igor.io')->then(function ($ip) {
6884
echo "Host: $ip\n";

examples/01-one.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

3+
use React\Dns\Config\Config;
34
use React\Dns\Resolver\Factory;
45

56
require __DIR__ . '/../vendor/autoload.php';
67

78
$loop = React\EventLoop\Factory::create();
89

10+
$config = Config::loadSystemConfigBlocking();
11+
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
12+
913
$factory = new Factory();
10-
$resolver = $factory->create('8.8.8.8', $loop);
14+
$resolver = $factory->create($server, $loop);
1115

1216
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
1317

examples/02-concurrent.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

3+
use React\Dns\Config\Config;
34
use React\Dns\Resolver\Factory;
45

56
require __DIR__ . '/../vendor/autoload.php';
67

78
$loop = React\EventLoop\Factory::create();
89

10+
$config = Config::loadSystemConfigBlocking();
11+
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
12+
913
$factory = new Factory();
10-
$resolver = $factory->create('8.8.8.8', $loop);
14+
$resolver = $factory->create($server, $loop);
1115

1216
$names = array_slice($argv, 1);
1317
if (!$names) {

examples/03-cached.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

3+
use React\Dns\Config\Config;
34
use React\Dns\Resolver\Factory;
45

56
require __DIR__ . '/../vendor/autoload.php';
67

78
$loop = React\EventLoop\Factory::create();
89

10+
$config = Config::loadSystemConfigBlocking();
11+
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
12+
913
$factory = new Factory();
10-
$resolver = $factory->createCached('8.8.8.8', $loop);
14+
$resolver = $factory->createCached($server, $loop);
1115

1216
$name = isset($argv[1]) ? $argv[1] : 'www.google.com';
1317

src/Config/Config.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,28 @@
44

55
class Config
66
{
7+
/**
8+
* Loads the system DNS configuration
9+
*
10+
* Note that this method may block while loading its internal files and/or
11+
* commands and should thus be used with care! While this should be
12+
* relatively fast for most systems, it remains unknown if this may block
13+
* under certain circumstances. In particular, this method should only be
14+
* executed before the loop starts, not while it is running.
15+
*
16+
* Note that this method will try to access its files and/or commands and
17+
* try to parse its output. Currently, this is only a placeholder that does
18+
* not actually parse any nameserver entries.
19+
*
20+
* Note that the previous section implies that this may return an empty
21+
* `Config` object if no valid nameserver entries can be found.
22+
*
23+
* @return self
24+
*/
25+
public static function loadSystemConfigBlocking()
26+
{
27+
return new self();
28+
}
29+
730
public $nameservers = array();
831
}

0 commit comments

Comments
 (0)