Skip to content

Commit

Permalink
Merge a1ad86e into 1d69e0a
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Thomas committed Dec 3, 2018
2 parents 1d69e0a + a1ad86e commit cecfcad
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 81 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ These resolvers at the least implement the `Resolvers\Interfaces\DNSQuery` inter

```php
$resolver = new Resolvers\GoogleDNS();
// validates hostname
$hostname = Hostname::createFromString('google.com');

// OR

$hostname = 'google.com';

// can query via convenience methods
$records = $resolver->getARecords($hostname); // returns a collection of DNSRecords

// can also query by any type
// can also query by any type. Types are a proper object that validate you have the right type
$recordType = DNSRecordType::createAAAA();

// OR

// loose and fast, but be warned validation errors will still get thrown if you pass in garbage
$recordType = 'AAAA';

$moreRecords = $resolver->getRecords($hostname, $recordType);

// can query to see if any resolvers find a record or type.
Expand Down Expand Up @@ -84,7 +95,7 @@ Check them out here:
$chainResolver->withConsensusResults()->getARecords(new Hostname('facebook.com'));

// returns the first non empty result set
$chainResolver->withFirstResults()->getARecords(new Hostname('facebook.com'));
$chainResolver->withFirstResults()->getARecords('facebook.com');

// returns all collective responses with duplicates filtered out
$chainResolver->withFirstResults()->getARecords(new Hostname('facebook.com'));
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^6.3",
"psr/cache": "^1.0",
"symfony/event-dispatcher": "~4.0 || ~3.0"
"symfony/event-dispatcher": "~4.0 || ~3.0",
"psr/log": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^7.4",
Expand Down
96 changes: 48 additions & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Observability/Traits/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

trait Dispatcher
{
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|null
*/
private $dispatcher = null;

public function setDispatcher(EventDispatcherInterface $dispatcher): void
Expand Down
27 changes: 27 additions & 0 deletions src/Observability/Traits/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace RemotelyLiving\PHPDNS\Observability\Traits;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

trait Logger
{
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger = null;

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

protected function getLogger(): LoggerInterface
{
if ($this->logger === null) {
$this->logger = new NullLogger();
}

return $this->logger;
}
}
14 changes: 7 additions & 7 deletions src/Resolvers/Interfaces/DNSQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ interface DNSQuery
/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function getARecords(Hostname $hostname): DNSRecordCollection;
public function getARecords(string $hostname): DNSRecordCollection;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function getAAAARecords(Hostname $hostname): DNSRecordCollection;
public function getAAAARecords(string $hostname): DNSRecordCollection;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function getCNAMERecords(Hostname $hostname): DNSRecordCollection;
public function getCNAMERecords(string $hostname): DNSRecordCollection;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function getTXTRecords(Hostname $hostname): DNSRecordCollection;
public function getTXTRecords(string $hostname): DNSRecordCollection;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function getMXRecords(Hostname $hostname): DNSRecordCollection;
public function getMXRecords(string $hostname): DNSRecordCollection;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function getRecords(Hostname $hostname, DNSRecordType $recordType = null): DNSRecordCollection;
public function getRecords(string $hostname, string $recordType = null): DNSRecordCollection;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
*/
public function recordTypeExists(Hostname $hostname, DNSRecordType $recordType): bool;
public function recordTypeExists(string $hostname, string $recordType): bool;

/**
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
Expand Down
3 changes: 2 additions & 1 deletion src/Resolvers/Interfaces/ObservableResolver.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace RemotelyLiving\PHPDNS\Resolvers\Interfaces;

use Psr\Log\LoggerAwareInterface;
use RemotelyLiving\PHPDNS\Observability\Interfaces\Observable;

interface ObservableResolver extends Resolver, Observable
interface ObservableResolver extends Resolver, Observable, LoggerAwareInterface
{

}
6 changes: 1 addition & 5 deletions src/Resolvers/Interfaces/ReverseDNSQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
interface ReverseDNSQuery
{
/**
* @param \RemotelyLiving\PHPDNS\Entities\IPAddress $IPAddress
*
* @return \RemotelyLiving\PHPDNS\Entities\Hostname
*
* @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\ReverseLookupFailure
*/
public function getHostnameByAddress(IPAddress $IPAddress): Hostname;
public function getHostnameByAddress(string $IPAddress): Hostname;
}
4 changes: 2 additions & 2 deletions src/Resolvers/LocalSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function __construct(LocalSystemDNS $systemDNS = null, LocalMapper $mappe
$this->mapper = $mapper ?? new LocalMapper();
}

public function getHostnameByAddress(IPAddress $IPAddress): Hostname
public function getHostnameByAddress(string $IPAddress): Hostname
{
$result = $this->systemDNS->getHostnameByAddress($IPAddress);
$result = $this->systemDNS->getHostnameByAddress(new IPAddress($IPAddress));

return Hostname::createFromString($result);
}
Expand Down
42 changes: 27 additions & 15 deletions src/Resolvers/ResolverAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use RemotelyLiving\PHPDNS\Observability\Events\DNSQueryFailed;
use RemotelyLiving\PHPDNS\Observability\Events\DNSQueryProfiled;
use RemotelyLiving\PHPDNS\Observability\Traits\Dispatcher;
use RemotelyLiving\PHPDNS\Observability\Traits\Logger;
use RemotelyLiving\PHPDNS\Observability\Traits\Profileable;
use RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure;
use RemotelyLiving\PHPDNS\Resolvers\Interfaces\ObservableResolver;

abstract class ResolverAbstract implements ObservableResolver
{
use Dispatcher,
use Logger,
Dispatcher,
Profileable;

private $name = null;
Expand All @@ -30,32 +32,32 @@ public function getName(): string
return $this->name;
}

public function getARecords(Hostname $hostname): DNSRecordCollection
public function getARecords(string $hostname): DNSRecordCollection
{
return $this->getRecords($hostname, DNSRecordType::createA());
}

public function getAAAARecords(Hostname $hostname): DNSRecordCollection
public function getAAAARecords(string $hostname): DNSRecordCollection
{
return $this->getRecords($hostname, DNSRecordType::createAAAA());
}

public function getCNAMERecords(Hostname $hostname): DNSRecordCollection
public function getCNAMERecords(string $hostname): DNSRecordCollection
{
return $this->getRecords($hostname, DNSRecordType::createCNAME());
}

public function getTXTRecords(Hostname $hostname): DNSRecordCollection
public function getTXTRecords(string $hostname): DNSRecordCollection
{
return $this->getRecords($hostname, DNSRecordType::createTXT());
}

public function getMXRecords(Hostname $hostname): DNSRecordCollection
public function getMXRecords(string $hostname): DNSRecordCollection
{
return $this->getRecords($hostname, DNSRecordType::createMX());
}

public function recordTypeExists(Hostname $hostname, DNSRecordType $recordType): bool
public function recordTypeExists(string $hostname, string $recordType): bool
{
return !$this->getRecords($hostname, $recordType)->isEmpty();
}
Expand All @@ -66,28 +68,38 @@ public function hasRecord(DNSRecord $record): bool
->has($record);
}

public function getRecords(Hostname $hostname, DNSRecordType $recordType = null): DNSRecordCollection
public function getRecords(string $hostname, string $recordType = null): DNSRecordCollection
{
$any = DNSRecordType::createANY();
$recordType = $recordType ?? $any;
$recordType = DNSRecordType::createFromString($recordType ?? 'ANY');
$hostname = Hostname::createFromString($hostname);

$profile = $this->createProfile("{$this->getName()}:{$hostname}:{$recordType}");
$profile->startTransaction();

try {
$result = ($recordType->equals($any))
$result = ($recordType->equals(DNSRecordType::createANY()))
? $this->doQuery($hostname, $recordType)
: $this->doQuery($hostname, $recordType)->filteredByType($recordType);
} catch (QueryFailure $e) {
$this->dispatch(new DNSQueryFailed($this, $hostname, $recordType, $e));
$dnsQueryFailureEvent = new DNSQueryFailed($this, $hostname, $recordType, $e);
$this->dispatch($dnsQueryFailureEvent);
$this->getLogger()->error(
'DNS query failed',
['event' => json_encode($dnsQueryFailureEvent), 'exception' => $e]
);

throw $e;
} finally {
$profile->endTransaction();
$profile->samplePeakMemoryUsage();
$this->dispatch(new DNSQueryProfiled($profile));
$dnsQueryProfiledEvent = new DNSQueryProfiled($profile);
$this->dispatch($dnsQueryProfiledEvent);
$this->getLogger()->info('DNS query profiled', ['event' => json_encode($dnsQueryProfiledEvent)]);
}

$this->dispatch(new DNSQueried($this, $hostname, $recordType, $result));

$dnsQueriedEvent = new DNSQueried($this, $hostname, $recordType, $result);
$this->dispatch($dnsQueriedEvent);
$this->getLogger()->info('DNS queried', ['event' => json_encode($dnsQueriedEvent)]);
return $result;
}

Expand Down

0 comments on commit cecfcad

Please sign in to comment.