mirror of
https://github.com/danog/dns.git
synced 2024-11-26 20:14:51 +01:00
1d7627f7ca
Match also subdomains with only one character |
||
---|---|---|
examples | ||
lib | ||
test | ||
.gitignore | ||
.travis.yml | ||
composer.json | ||
phpunit.xml | ||
README.md |
dns
Asynchronous DNS resolution built on the Amp concurrency framework
Examples
Synchronous Resolution Via Amp\wait()
<?php
require __DIR__ . '/vendor/autoload.php';
$resolver = new Amp\Dns\Resolver;
$name = 'google.com';
list($address, $type) = Amp\wait($resolver->resolve($name));
printf("%s resolved to %s\n", $name, $address);
Concurrent Synchronous Resolution Via wait()
<?php
require __DIR__ . '/vendor/autoload.php';
$resolver = new Amp\Dns\Resolver;
$names = [
'github.com',
'google.com',
'stackoverflow.com',
];
$promises = [];
foreach ($names as $name) {
$promises[$name] = $resolver->resolve($name);
}
$comboPromise = Amp\all($promises);
$results = Amp\wait($comboPromise);
foreach ($results as $name => $resultArray) {
list($addr, $type) = $resultArray;
printf("%s => %s\n", $name, $addr);
}
Event Loop Async
<?php
require __DIR__ . '/vendor/autoload.php';
Amp\run(function() {
$resolver = new Amp\Dns\Resolver;
$names = [
'github.com',
'google.com',
'stackoverflow.com',
'localhost',
'192.168.0.1',
'::1',
];
$promises = [];
foreach ($names as $name) {
$promise = $resolver->resolve($name);
$promises[$name] = $promise;
}
// Yield control until the combo promise resolves
list($errors, $successes) = (yield 'some' => $promises);
foreach ($names as $name) {
echo isset($errors[$name])
? "FAILED: {$name}\n"
: "{$name} => {$successes[$name][0]}\n";
}
// Stop the event loop so we don't sit around forever
Amp\stop();
});
Tests
Tests can be run from the command line using:
php vendor/bin/phpunit -c phpunit.xml
or to exclude tests that require a working internet connection:
php vendor/bin/phpunit -c phpunit.xml --exclude-group internet