Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by supporting new default loop #182

Merged
merged 2 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@ factory. All you need to give it is a nameserver, then you can start resolving
names, baby!

```php
$loop = React\EventLoop\Factory::create();

$config = React\Dns\Config\Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new React\Dns\Resolver\Factory();
$dns = $factory->create($config, $loop);
$dns = $factory->create($config);

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

$loop->run();
```

See also the [first example](examples).
Expand All @@ -72,15 +68,13 @@ But there's more.
You can cache results by configuring the resolver to use a `CachedExecutor`:

```php
$loop = React\EventLoop\Factory::create();

$config = React\Dns\Config\Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new React\Dns\Resolver\Factory();
$dns = $factory->createCached($config, $loop);
$dns = $factory->createCached($config);

$dns->resolve('igor.io')->then(function ($ip) {
echo "Host: $ip\n";
Expand All @@ -91,8 +85,6 @@ $dns->resolve('igor.io')->then(function ($ip) {
$dns->resolve('igor.io')->then(function ($ip) {
echo "Host: $ip\n";
});

$loop->run();
```

If the first call returns before the second, only one query will be executed.
Expand All @@ -110,9 +102,8 @@ You can also specify a custom cache implementing [`CacheInterface`](https://gith

```php
$cache = new React\Cache\ArrayCache();
$loop = React\EventLoop\Factory::create();
$factory = new React\Dns\Resolver\Factory();
$dns = $factory->createCached('8.8.8.8', $loop, $cache);
$dns = $factory->createCached('8.8.8.8', null, $cache);
```

See also the wiki for possible [cache implementations](https://github.com/reactphp/react/wiki/Users#cache-implementations).
Expand Down Expand Up @@ -215,8 +206,7 @@ For more advanced usages one can utilize this class directly.
The following example looks up the `IPv6` address for `igor.io`.

```php
$loop = Factory::create();
$executor = new UdpTransportExecutor('8.8.8.8:53', $loop);
$executor = new UdpTransportExecutor('8.8.8.8:53');

$executor->query(
new Query($name, Message::TYPE_AAAA, Message::CLASS_IN)
Expand All @@ -225,8 +215,6 @@ $executor->query(
echo 'IPv6: ' . $answer->data . PHP_EOL;
}
}, 'printf');

$loop->run();
```

See also the [fourth example](examples).
Expand All @@ -236,9 +224,8 @@ want to use this in combination with a `TimeoutExecutor` like this:

```php
$executor = new TimeoutExecutor(
new UdpTransportExecutor($nameserver, $loop),
3.0,
$loop
new UdpTransportExecutor($nameserver),
3.0
);
```

Expand All @@ -249,9 +236,8 @@ combination with a `RetryExecutor` like this:
```php
$executor = new RetryExecutor(
new TimeoutExecutor(
new UdpTransportExecutor($nameserver, $loop),
3.0,
$loop
new UdpTransportExecutor($nameserver),
3.0
)
);
```
Expand All @@ -268,9 +254,8 @@ a `CoopExecutor` like this:
$executor = new CoopExecutor(
new RetryExecutor(
new TimeoutExecutor(
new UdpTransportExecutor($nameserver, $loop),
3.0,
$loop
new UdpTransportExecutor($nameserver),
3.0
)
)
);
Expand All @@ -293,8 +278,7 @@ For more advanced usages one can utilize this class directly.
The following example looks up the `IPv6` address for `reactphp.org`.

```php
$loop = Factory::create();
$executor = new TcpTransportExecutor('8.8.8.8:53', $loop);
$executor = new TcpTransportExecutor('8.8.8.8:53');

$executor->query(
new Query($name, Message::TYPE_AAAA, Message::CLASS_IN)
Expand All @@ -303,8 +287,6 @@ $executor->query(
echo 'IPv6: ' . $answer->data . PHP_EOL;
}
}, 'printf');

$loop->run();
```

See also [example #92](examples).
Expand All @@ -314,9 +296,8 @@ want to use this in combination with a `TimeoutExecutor` like this:

```php
$executor = new TimeoutExecutor(
new TcpTransportExecutor($nameserver, $loop),
3.0,
$loop
new TcpTransportExecutor($nameserver),
3.0
);
```

Expand All @@ -342,9 +323,8 @@ combination with a `CoopExecutor` like this:
```php
$executor = new CoopExecutor(
new TimeoutExecutor(
new TcpTransportExecutor($nameserver, $loop),
3.0,
$loop
new TcpTransportExecutor($nameserver),
3.0
)
);
```
Expand Down Expand Up @@ -412,7 +392,7 @@ use this code:
```php
$hosts = \React\Dns\Config\HostsFile::loadFromPathBlocking();

$executor = new UdpTransportExecutor('8.8.8.8:53', $loop);
$executor = new UdpTransportExecutor('8.8.8.8:53');
$executor = new HostsFileExecutor($hosts, $executor);

$executor->query(
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=5.3.0",
"react/cache": "^1.0 || ^0.6 || ^0.5",
"react/event-loop": "^1.0 || ^0.5",
"react/event-loop": "^1.2",
"react/promise": "^3.0 || ^2.7 || ^1.2.1",
"react/promise-timer": "^1.2"
},
Expand Down
6 changes: 1 addition & 5 deletions examples/01-one.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@

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

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

$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new Factory();
$resolver = $factory->create($config, $loop);
$resolver = $factory->create($config);

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

$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
}, 'printf');

$loop->run();
6 changes: 1 addition & 5 deletions examples/02-concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

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

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

$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new Factory();
$resolver = $factory->create($config, $loop);
$resolver = $factory->create($config);

$names = array_slice($argv, 1);
if (!$names) {
Expand All @@ -25,5 +23,3 @@
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
}, 'printf');
}

$loop->run();
13 changes: 5 additions & 8 deletions examples/03-cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@

use React\Dns\Config\Config;
use React\Dns\Resolver\Factory;
use React\EventLoop\Loop;

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

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

$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new Factory();
$resolver = $factory->createCached($config, $loop);
$resolver = $factory->createCached($config);

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

$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
}, 'printf');

$loop->addTimer(1.0, function() use ($name, $resolver) {
Loop::addTimer(1.0, function() use ($name, $resolver) {
$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
}, 'printf');
});

$loop->addTimer(2.0, function() use ($name, $resolver) {
Loop::addTimer(2.0, function() use ($name, $resolver) {
$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
}, 'printf');
});

$loop->addTimer(3.0, function() use ($name, $resolver) {
Loop::addTimer(3.0, function() use ($name, $resolver) {
$resolver->resolve($name)->then(function ($ip) use ($name) {
echo 'IP for ' . $name . ': ' . $ip . PHP_EOL;
}, 'printf');
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/11-all-ips.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

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

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

$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new Factory();
$resolver = $factory->create($config, $loop);
$resolver = $factory->create($config);

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

Expand All @@ -29,5 +27,3 @@
}, function (Exception $e) use ($name) {
echo 'No IPv6 addresses for ' . $name . ': ' . $e->getMessage() . PHP_EOL;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/12-all-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

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

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

$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new Factory();
$resolver = $factory->create($config, $loop);
$resolver = $factory->create($config);

$name = isset($argv[1]) ? $argv[1] : 'google.com';
$type = constant('React\Dns\Model\Message::TYPE_' . (isset($argv[2]) ? $argv[2] : 'TXT'));
Expand All @@ -26,5 +24,3 @@
}, function (Exception $e) {
echo $e->getMessage() . PHP_EOL;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/13-reverse-dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

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

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

$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new Factory();
$resolver = $factory->create($config, $loop);
$resolver = $factory->create($config);

$ip = isset($argv[1]) ? $argv[1] : '8.8.8.8';

Expand All @@ -33,5 +31,3 @@
}, function (Exception $e) {
echo $e->getMessage() . PHP_EOL;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/91-query-a-and-aaaa.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

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

$loop = Factory::create();
$executor = new UdpTransportExecutor('8.8.8.8:53', $loop);
$executor = new UdpTransportExecutor('8.8.8.8:53');

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

Expand All @@ -25,5 +24,3 @@
echo 'IPv6: ' . $answer->data . PHP_EOL;
}
}, 'printf');

$loop->run();
5 changes: 1 addition & 4 deletions examples/92-query-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

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

$loop = Factory::create();
$executor = new TcpTransportExecutor('8.8.8.8:53', $loop);
$executor = new TcpTransportExecutor('8.8.8.8:53');

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

Expand Down Expand Up @@ -80,5 +79,3 @@
echo $type . ': ' . $data . PHP_EOL;
}
}, 'printf');

$loop->run();
5 changes: 2 additions & 3 deletions src/Query/CoopExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
* $executor = new CoopExecutor(
* new RetryExecutor(
* new TimeoutExecutor(
* new UdpTransportExecutor($nameserver, $loop),
* 3.0,
* $loop
* new UdpTransportExecutor($nameserver),
* 3.0
* )
* )
* );
Expand Down
Loading