-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from hostinger/feat/add-dig-lookup-func
feat: Add dig lookup functionality
- Loading branch information
Showing
11 changed files
with
400 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Command; | ||
|
||
class DigCommand | ||
{ | ||
public function __construct( | ||
private readonly string $name, | ||
private readonly string $type, | ||
private readonly ?DigOptions $options = null, | ||
private readonly ?DigQuery $query = null, | ||
) { | ||
} | ||
|
||
public function toCliCommand(): string | ||
{ | ||
$args = []; | ||
$args[] = escapeshellcmd('dig'); | ||
|
||
if ($this->options) { | ||
$args = array_merge($args, array_values($this->options->toCliOptions())); | ||
} | ||
|
||
if ($this->query) { | ||
$args = array_merge($args, array_values($this->query->toCliQuery())); | ||
} | ||
|
||
$args[] = escapeshellarg($this->name); | ||
$args[] = escapeshellarg($this->type); | ||
|
||
return join(' ', $args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Command; | ||
|
||
class DigOptions | ||
{ | ||
public function __construct( | ||
public readonly ?string $server = null, | ||
public readonly ?string $name = null, | ||
public readonly ?string $type = null, | ||
) { | ||
} | ||
|
||
public function toCliOptions(): array | ||
{ | ||
$opts = []; | ||
|
||
if ($this->server !== null) { | ||
$opts['server'] = escapeshellarg('@' . $this->server); | ||
} | ||
|
||
if ($this->name !== null) { | ||
$opts['name'] = '-q ' . escapeshellarg($this->name); | ||
} | ||
|
||
if ($this->type !== null) { | ||
$opts['type'] = '-t ' . escapeshellarg($this->type); | ||
} | ||
|
||
return $opts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Command; | ||
|
||
class DigQuery | ||
{ | ||
public function __construct( | ||
public readonly ?bool $all = null, | ||
public readonly ?bool $answer = null, | ||
public readonly ?bool $authority = null, | ||
public readonly ?int $time = null, | ||
) { | ||
} | ||
|
||
public function toCliQuery(): array | ||
{ | ||
$opts = []; | ||
|
||
if ($this->all !== null) { | ||
$opts['all'] = escapeshellarg('+' . ($this->all ? 'all' : 'noall')); | ||
} | ||
|
||
if ($this->answer !== null) { | ||
$opts['answer'] = escapeshellarg('+' . ($this->answer ? 'answer' : 'noanswer')); | ||
} | ||
|
||
if ($this->authority !== null) { | ||
$opts['authority'] = escapeshellarg('+' . ($this->authority ? 'authority' : 'noauthority')); | ||
} | ||
|
||
if ($this->time !== null) { | ||
$opts['time'] = escapeshellarg('+time=' . $this->time); | ||
} | ||
|
||
return $opts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class DigExecException extends RuntimeException | ||
{ | ||
public function __construct(string $reason) | ||
{ | ||
parent::__construct("Unable to run dig: $reason"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class UnsupportedRecordTypeException extends RuntimeException | ||
{ | ||
public function __construct(int $type) | ||
{ | ||
parent::__construct("Unsupported record type provided: $type"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Tests\Command; | ||
|
||
use Generator; | ||
use Hostinger\Dig\Command\DigCommand; | ||
use Hostinger\Dig\Command\DigOptions; | ||
use Hostinger\Dig\Command\DigQuery; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DigCommandTest extends TestCase | ||
{ | ||
public static function dataProvider(): Generator | ||
{ | ||
yield 'basic' => [ | ||
[ | ||
'test.com', | ||
'NS', | ||
], | ||
"dig 'test.com' 'NS'", | ||
]; | ||
|
||
yield 'with options' => [ | ||
[ | ||
'test.com', | ||
'NS', | ||
new DigOptions( | ||
'ping.com', | ||
), | ||
], | ||
"dig '@ping.com' 'test.com' 'NS'", | ||
]; | ||
|
||
yield 'with options and query' => [ | ||
[ | ||
'test.com', | ||
'NS', | ||
new DigOptions( | ||
'ping.com', | ||
), | ||
new DigQuery( | ||
false, | ||
true, | ||
false, | ||
10, | ||
), | ||
], | ||
"dig '@ping.com' '+noall' '+answer' '+noauthority' '+time=10' 'test.com' 'NS'", | ||
]; | ||
|
||
yield 'escapes args' => [ | ||
[ | ||
'test.com && exit 1', | ||
'NS && ls -la', | ||
], | ||
"dig 'test.com && exit 1' 'NS && ls -la'", | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testToCliCommand(array $args, string $expected): void | ||
{ | ||
$command = new DigCommand(...$args); | ||
|
||
$this->assertEquals($expected, $command->toCliCommand()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Hostinger\Dig\Tests\Command; | ||
|
||
use Generator; | ||
use Hostinger\Dig\Command\DigOptions; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DigOptionsTest extends TestCase | ||
{ | ||
public static function optionsDataProvider(): Generator | ||
{ | ||
yield 'empty' => [ | ||
[ | ||
null, | ||
null, | ||
null, | ||
], | ||
[], | ||
]; | ||
|
||
yield 'all set' => [ | ||
[ | ||
'test.com', | ||
'ping.com', | ||
'NS', | ||
], | ||
[ | ||
'server' => escapeshellarg('@test.com'), | ||
'name' => '-q ' . escapeshellarg('ping.com'), | ||
'type' => '-t ' . escapeshellarg('NS'), | ||
], | ||
]; | ||
|
||
yield 'some set' => [ | ||
[ | ||
null, | ||
null, | ||
'NS', | ||
], | ||
[ | ||
'type' => '-t ' . escapeshellarg('NS'), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider optionsDataProvider | ||
*/ | ||
public function testToCliOptions(array $args, array $expected): void | ||
{ | ||
$query = new DigOptions(...$args); | ||
|
||
$this->assertEquals($expected, $query->toCliOptions()); | ||
} | ||
} |
Oops, something went wrong.