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

Expose Executer via factory class #194

Open
wants to merge 9 commits into
base: 1.x
Choose a base branch
from
18 changes: 15 additions & 3 deletions src/Resolver/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;

final class Factory
class Factory
skydiablo marked this conversation as resolved.
Show resolved Hide resolved
{

/**
* Allow the developer to specify a custom additional ExecutorInterface
* or ResolverInterface in an extended Class.
* @param ExecutorInterface $executor
* @return ResolverInterface
*/
protected function wrapInResolver(ExecutorInterface $executor)
{
return new Resolver($executor);
}

/**
* Creates a DNS resolver instance for the given DNS config
*
Expand All @@ -40,7 +52,7 @@ public function create($config, LoopInterface $loop = null)
{
$executor = $this->decorateHostsFileExecutor($this->createExecutor($config, $loop ?: Loop::get()));

return new Resolver($executor);
return $this->wrapInResolver($executor);
}

/**
Expand Down Expand Up @@ -70,7 +82,7 @@ public function createCached($config, LoopInterface $loop = null, CacheInterface
$executor = new CachingExecutor($executor, $cache);
$executor = $this->decorateHostsFileExecutor($executor);

return new Resolver($executor);
return $this->wrapInResolver($executor);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
final class Resolver implements ResolverInterface
{
private $executor;
protected $executor;
skydiablo marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(ExecutorInterface $executor)
{
Expand All @@ -31,13 +31,17 @@ public function resolveAll($domain, $type)
$query = new Query($domain, $type, Message::CLASS_IN);
$that = $this;

return $this->executor->query(
$query
)->then(function (Message $response) use ($query, $that) {
return $this->resolveQuery($query)->then(function (Message $response) use ($query, $that) {
return $that->extractValues($query, $response);
});
}

public function resolveQuery(Query $query)
{
return $this->executor->query($query);
}


/**
* [Internal] extract all resource record values from response for this query
*
Expand Down
20 changes: 20 additions & 0 deletions src/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace React\Dns\Resolver;

use React\Dns\Model\Message;
use React\Dns\Query\Query;

interface ResolverInterface
{
/**
Expand Down Expand Up @@ -91,4 +94,21 @@ public function resolve($domain);
* Resolves with all record values on success or rejects with an Exception on error.
*/
public function resolveAll($domain, $type);

/**
* @param Query $query
* @return \React\Promise\PromiseInterface<Message>
skydiablo marked this conversation as resolved.
Show resolved Hide resolved
*
* This is the magic behind the main function of this package. It allows you to use low-level
* DNS query objects to send custom DNS queries to your DNS server.
*
* ```php
* $resolver->resolveQuery(new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN))->then(function (Message $message) {
* foreach($message->answers as $record) {
* echo 'IPv4 addresses for reactphp.org ' . $record->data . PHP_EOL;
* }
* });
* ```
*/
public function resolveQuery(Query $query);
skydiablo marked this conversation as resolved.
Show resolved Hide resolved
}
12 changes: 12 additions & 0 deletions tests/FunctionalResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Tests\Dns;

use React\Dns\Query\Query;
skydiablo marked this conversation as resolved.
Show resolved Hide resolved
use React\EventLoop\Factory as LoopFactory;
use React\Dns\Resolver\Factory;
use React\Dns\RecordNotFoundException;
Expand Down Expand Up @@ -47,6 +48,17 @@ public function testResolveGoogleResolves()
$this->loop->run();
}

/**
* @group internet
*/
public function testResolveQueryGoogleResolves()
{
$promise = $this->resolver->resolveQuery(new Query('google.com', Message::TYPE_A, Message::CLASS_IN));
$promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Dns\Model\Message')), $this->expectCallableNever());

$this->loop->run();
}

/**
* @group internet
*/
Expand Down