Skip to content

Commit

Permalink
add change & patch bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Nov 12, 2023
1 parent 5a35e87 commit 05bd363
Show file tree
Hide file tree
Showing 54 changed files with 586 additions and 274 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
run: php vendor/bin/phpcs --standard=phpcs.xml
- name: "Run PHP Unit Test"
run: php vendor/bin/phpunit --configuration=phpunit.xml
- name: "Run Phpstan"
run: php vendor/bin/phpstan --configuration=phpstan.neon --memory-limit=256M
continuous-integration-php82:
name: "Continuous Integration php8.2"
runs-on: ubuntu-latest
Expand All @@ -45,6 +47,8 @@ jobs:
run: php vendor/bin/phpcs --standard=phpcs.xml
- name: "Run PHP Unit Test"
run: php vendor/bin/phpunit --configuration=phpunit.xml
- name: "Run Phpstan"
run: php vendor/bin/phpstan --configuration=phpstan.neon --memory-limit=256M
coding-standards-php83:
name: "Continuous Integration php8.3"
runs-on: ubuntu-latest
Expand All @@ -65,3 +69,5 @@ jobs:
run: php vendor/bin/phpcs --standard=phpcs.xml
- name: "Run PHP Unit Test"
run: php vendor/bin/phpunit --configuration=phpunit.xml
- name: "Run Phpstan"
run: php vendor/bin/phpstan --configuration=phpstan.neon --memory-limit=256M
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ $resolver = new Resolver($dnsServer, $cache);
*/
$useCache = true; // default to true
$timeout = 3.5; // 3.5 seconds
$response = $resolver->lookup('domainname.ext', 'A', 'IN', $timeout, $useCache);
$response = $resolver->lookup('domain-name.ext', 'A', 'IN', $timeout, $useCache);

/**
* Enable Pseudo OPT
*/
$resolver->setDnsSec(true);
$response = $resolver->lookup('domainname.ext', 'A', 'IN');
$response = $resolver->lookup('domain-name.ext', 'A', 'IN');
$answers = $response->getAnswers();
$records = $answers->getRecords();
// Filter "A" Address Only
$arrayA = $records->getFilteredType('A');

```

> IXFR & AXFR not yet implemented
> IXFR & AXFR aren't fully implemented yet

## Note
Expand Down
19 changes: 17 additions & 2 deletions src/Abstracts/AbstractDnsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getSecondaryServer(): ?string
public function getName(): string
{
// fallback default class name
return $this->name ??= ltrim(strrchr($this::class, '\\'))?:$this::class;
return $this->name ??= ltrim(strrchr($this::class, '\\')?:$this::class)?:$this::class;
}

/**
Expand Down Expand Up @@ -97,6 +97,15 @@ public function unserialize(string $data) : void
$this->__unserialize(unserialize($data));
}

/**
* @return array{
* identity:string,
* name: string,
* primaryServer: string,
* secondaryServer: ?string,
* port: int,
* }
*/
public function __serialize(): array
{
return [
Expand All @@ -111,7 +120,13 @@ public function __serialize(): array
/**
* Magic method for unserialize
*
* @param array $data
* @param array{
* identity:string,
* name: string,
* primaryServer: string,
* secondaryServer: ?string,
* port: int,
* } $data
* @return void
*/
public function __unserialize(array $data): void
Expand Down
47 changes: 21 additions & 26 deletions src/Abstracts/AbstractResourceRecordType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordMetaTypeInterface;
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordQTypeDefinitionInterface;
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordTypeInterface;
use ArrayAccess\DnsRecord\ResourceRecord\Definitions\QType;
use ArrayAccess\DnsRecord\Utils\Buffer;
use ArrayAccess\DnsRecord\Utils\Lookup;
use function is_string;
use function is_array;
use function ord;
use function serialize;
use function sprintf;
Expand Down Expand Up @@ -47,11 +46,11 @@ abstract class AbstractResourceRecordType implements ResourceRecordTypeInterface
/**
* Response type
*
* @var string|ResourceRecordQTypeDefinitionInterface
* @var ResourceRecordQTypeDefinitionInterface
* @see ResourceRecordTypeInterface::getType()
* @see Lookup::RR_TYPES
*/
protected ResourceRecordQTypeDefinitionInterface|string $type;
protected ResourceRecordQTypeDefinitionInterface $type;

/**
* @var ResourceRecordClassInterface
Expand Down Expand Up @@ -112,10 +111,6 @@ public function getOffsetPosition(): int
protected function parseMessage(): void
{
$type = static::TYPE;
if (!isset($this->type) && is_string($type)) {
$this->type = $type;
}

$message = $this->message->getMessage();
$offsetPosition = $this->offsetPosition;
$this->name = Buffer::readLabel($message, $offsetPosition);
Expand All @@ -125,28 +120,32 @@ protected function parseMessage(): void
'Response header length is invalid'
);
}
[
'type' => $type,
'class' => $class,
'ttl' => $this->ttl,
'length' => $this->rdLength,
] = unpack("ntype/nclass/Nttl/nlength", $this->header);
$headerArray = unpack("ntype/nclass/Nttl/nlength", $this->header);
$this->rdLength = 0;
if (is_array($headerArray)) {
[
'type' => $type,
'class' => $class,
'ttl' => $this->ttl,
'length' => $this->rdLength,
] = $headerArray;
}

$this->rData = substr($message, $offsetPosition, $this->rdLength);
if (strlen($this->rData) !== $this->rdLength) {
throw new LengthException(
'Rdata & length from response header is mismatch'
);
}

$type = Lookup::resourceType($type);
$class = Lookup::resourceClass($class);
if (isset($this->type)) {
$originType = $this->getType();
if ($originType->getName() !== $type->getName()) {
$type = $type ? Lookup::resourceType($type) : null;
$class = Lookup::resourceClass($class??'');
if (isset($this->type) || !$type) {
if ($this->type->getName() !== $type?->getName()) {
throw new MalformedDataException(
sprintf(
'Response type does not match with current object type. object type: [%s] response type: [%s]',
$originType,
$this->type->getName(),
$type
)
);
Expand All @@ -165,6 +164,7 @@ protected function parseMessage(): void
* @param string $message
* @param int $rdataOffset
* @noinspection PhpMissingReturnTypeInspection
* @phpstan-ignore-next-line
*/
protected function parseRData(string $message, int $rdataOffset)
{
Expand Down Expand Up @@ -212,11 +212,6 @@ public function getMessage(): PacketMessageInterface
*/
public function getType(): ResourceRecordQTypeDefinitionInterface
{
if (isset($this->type)) {
is_string($this->type) && $this->type = QType::create($this->type);
return $this->type;
}

return $this->type;
}

Expand Down Expand Up @@ -294,7 +289,7 @@ public function __serialize(): array
/**
* Magic method for unserialize
*
* @param array $data
* @param array{message: PacketMessageInterface, offsetPosition: int} $data
* @return void
* @throws MalformedDataException
*/
Expand Down
36 changes: 27 additions & 9 deletions src/Cache/Adapter/FileCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use function is_dir;
use function is_file;
use function is_int;
use function is_resource;
use function is_string;
use function md5;
use function preg_match;
Expand All @@ -38,6 +39,7 @@
use function restore_error_handler;
use function serialize;
use function set_error_handler;
use function sprintf;
use function str_replace;
use function strlen;
use function substr;
Expand Down Expand Up @@ -139,9 +141,10 @@ private function doInit(string $namespace, ?string $directory): void
{
$namespace = trim($namespace);
$namespace = preg_replace('~[^a-z0-9_\-.]~i', '', $namespace);
if ($namespace === '') {
if (!$namespace || !is_string($namespace)) {
$namespace = '@';
}

if (!$directory
|| trim($directory) === ''
|| trim(trim($directory), '/\\') === ''
Expand Down Expand Up @@ -187,12 +190,17 @@ private static function isOpcacheSupport(): bool
);
}

/**
* @param string $directory
* @param bool $loopDir
* @return Generator<string, string>
*/
private function scanFiles(string $directory, bool $loopDir = false) : Generator
{
if (!is_dir($directory)) {
return '';
}
$directory = rtrim(realpath($directory)??$directory, '\\/');
$directory = rtrim(realpath($directory)?:$directory, '\\/');
$chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$length = strlen($chars);
for ($i = 0; $i < $length; ++$i) {
Expand Down Expand Up @@ -235,9 +243,12 @@ public function prune(): bool
}
$value = null;
$valid = $this->validatePartialFile($file);
if (!$valid) {
$this->doUnlink($file);
continue;
}
try {
if ($valid
&& is_array(($expiresAt = include $file))
if (is_array(($expiresAt = include $file))
&& count($expiresAt) === 3
&& is_int($expiresAt[0]??null)
&& is_string($expiresAt[1]??null)
Expand Down Expand Up @@ -285,7 +296,7 @@ private function clearLocalCache(): void
}
if (count($this->files) > $maximum) {
while (count($this->files) > $max) {
array_shift(self::$cachesExpires);
array_shift($this->files);
}
}
}
Expand Down Expand Up @@ -359,7 +370,14 @@ private function write(string $file, string $data) : bool
$tmp = $this->generateTempFile();
$h = fopen($tmp, 'x');
}

if (!is_resource($h)) {
throw new Exception(
sprintf(
'Canot create resource from file : %s',
$tmp
)
);
}
fwrite($h, $data);
fclose($h);
$unlink = true;
Expand Down Expand Up @@ -426,7 +444,7 @@ private function doDelete(string $id, string ...$ids) : bool
return $this->doIdDelete(...$arrayId);
}

private function doUnlink(string $file)
private function doUnlink(string $file) : bool
{
if (self::isOpcacheSupport()) {
Caller::call('opcache_invalidate', $file, true);
Expand Down Expand Up @@ -508,7 +526,7 @@ private function doFetch(string $key): ?CacheDataInterface
$unlink = true;
return null;
} finally {
if ($unlink && file_exists($file)) {
if ($unlink) {
self::$cachesExpires[$file] = 0;
unset($this->values[$key]);
$this->doUnlink($file);
Expand Down Expand Up @@ -559,7 +577,7 @@ public function getItem(string $key): CacheDataInterface
if (isset($this->values[$key])) {
return $this->values[$key];
}
return $this->doFetch($key)??(new CacheData($key))->expiresAt(
return $this->doFetch($key)??(new CacheData($key))->expiresAfter(
$this->defaultLifetime === 0
? null
: $this->defaultLifetime
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/Adapter/Psr6CacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function saveItem(CacheDataInterface $cacheData): bool
$item
->set($cacheData)
->expiresAfter($cacheData->getExpiresAfter());
return $this->getCacheItemPool()->save($item);
return $this->getCacheItemPool()?->save($item)??false;
}

/**
Expand Down Expand Up @@ -95,7 +95,7 @@ public function deleteItems(string ...$keys): bool
public function getItem(string $key): CacheDataInterface
{
$cacheItem = $this->getCacheItem($key);
$cacheItem = $cacheItem->get();
$cacheItem = $cacheItem?->get();
if ($cacheItem instanceof CacheDataInterface) {
return $cacheItem;
}
Expand Down
20 changes: 7 additions & 13 deletions src/Cache/CacheData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

namespace ArrayAccess\DnsRecord\Cache;

use ArrayAccess\DnsRecord\Exceptions\CacheException;
use ArrayAccess\DnsRecord\Interfaces\Cache\CacheDataInterface;
use DateInterval;
use DateTimeInterface;
use function is_array;
use function is_string;
use function serialize;
use function unserialize;

Expand Down Expand Up @@ -121,23 +118,20 @@ public function serialize(): string

/**
* @inheritdoc
* @throws CacheException
*/
public function unserialize(string $data): void
{
$data = unserialize($data);
if (!is_array($data) || !is_string($data['key']??null)) {
throw new CacheException(
'Invalid serialized data'
);
}
$this->__unserialize($data);
$this->__unserialize(unserialize($data));
}

/**
* Magic method for unserialize
*
* @param array $data
* @param array{
* key: string,
* data: mixed,
* ttl: ?int
* } $data
* @return void
*/
public function __unserialize(array $data): void
Expand All @@ -150,7 +144,7 @@ public function __unserialize(array $data): void
/**
* Magic method for serializing
*
* @return array{key: string, data: mixed, ttl:int}
* @return array{key: string, data: mixed, ttl: ?int}
*/
public function __serialize(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/DnsServer/CustomDnsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* User defined dns server
*/
class CustomDnsServer extends AbstractDnsServer
final class CustomDnsServer extends AbstractDnsServer
{
use DisableSetterTrait;

Expand Down
Loading

0 comments on commit 05bd363

Please sign in to comment.