Repository navigation
Expand file tree
/
Copy pathAbstractRdapProtocol.php
More file actions
135 lines (121 loc) · 3.73 KB
/
Copy pathAbstractRdapProtocol.php
File metadata and controls
135 lines (121 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace ArrayAccess\RdapClient\Protocols;
use ArrayAccess\RdapClient\Exceptions\InvalidServiceDefinitionException;
use ArrayAccess\RdapClient\Exceptions\RdapServerNotFoundException;
use ArrayAccess\RdapClient\Interfaces\RdapClientInterface;
use ArrayAccess\RdapClient\Interfaces\RdapHttpClientAwareInterface;
use ArrayAccess\RdapClient\Interfaces\RdapProtocolInterface;
use ArrayAccess\RdapClient\Interfaces\RdapRequestInterface;
use ArrayAccess\RdapClient\Interfaces\RdapServiceInterface;
use ArrayAccess\RdapClient\Services\AbstractRdapService;
use function explode;
use function is_string;
use function md5;
use function sprintf;
use function str_contains;
abstract class AbstractRdapProtocol implements RdapProtocolInterface
{
/**
* @var string $name The name of the protocol
*/
protected string $name;
/**
* @var RdapServiceInterface $services The RDAP service
*/
protected RdapServiceInterface $services;
/**
* @inheritDoc
*/
public function __construct(protected RdapClientInterface $client)
{
}
/**
* @inheritDoc
*/
public function getClient(): RdapClientInterface
{
return $this->client;
}
/**
* Load the service from an IANA bootstrap URL
*
* Uses the PSR-18 HTTP client and PSR-16 cache of the client when configured,
* otherwise falls back to {@see AbstractRdapService::fromURL()}.
*
* @template T of AbstractRdapService
* @param class-string<T> $serviceClass
* @param string $url
* @return T
* @throws \Exception
* @noinspection PhpFullyQualifiedNameUsageInspection
*/
protected function loadService(string $serviceClass, string $url): AbstractRdapService
{
$client = $this->getClient();
if (!$client instanceof RdapHttpClientAwareInterface
|| ($client->getHttpClient() === null && $client->getCache() === null)
) {
return $serviceClass::fromURL($url);
}
$cache = $client->getCache();
$cacheKey = 'rdap-client.bootstrap.' . md5($url);
$content = $cache?->get($cacheKey);
if (is_string($content)) {
try {
return $serviceClass::fromJson($content, $url);
} catch (InvalidServiceDefinitionException) {
$cache?->delete($cacheKey);
}
}
$content = $client->fetch($url);
$service = $serviceClass::fromJson($content, $url);
$cache?->set($cacheKey, $content, $serviceClass::cacheExpirations());
return $service;
}
/**
* @inheritDoc
*/
public function getName(): string
{
return $this->name;
}
/**
* @inheritDoc
*/
public function getService(): RdapServiceInterface
{
return $this->services;
}
/**
* @inheritDoc
*/
public function getFindURL(string $target): string
{
$normalize = $this->getService()->normalize($target);
if (!$normalize) {
$this->getService()->throwInvalidTarget($target);
}
$url = $this->getService()->getRdapURL($normalize);
if (!$url) {
throw new RdapServerNotFoundException(
sprintf('Could not get Rdap URL for %s', $target)
);
}
if (str_contains($url, '#')) {
[$url] = explode('#', $url);
}
if (str_contains($url, '?')) {
[$url] = explode('?', $url);
}
$path = trim($this->getSearchPath(), '/');
return rtrim($url, '/') . "/$path/$normalize";
}
/**
* @inheritDoc
*/
public function find(string $target): ?RdapRequestInterface
{
return new RdapRequestProtocol($target, $this);
}
}