forked from dunglas/solid-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolidClient.php
More file actions
138 lines (112 loc) · 4.68 KB
/
SolidClient.php
File metadata and controls
138 lines (112 loc) · 4.68 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
136
137
138
<?php
/*
* This file is part of the Solid Client PHP project.
* (c) Kévin Dunglas <kevin@dunglas.fr>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Dunglas\PhpSolidClient;
use EasyRdf\Graph;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Kévin Dunglas <kevin@dunglas.fr>
*/
final class SolidClient
{
private const DEFAULT_MIME_TYPE = 'text/turtle';
private const LDP_BASIC_CONTAINER = 'http://www.w3.org/ns/ldp#BasicContainer';
private const LDP_RESOURCE = 'http://www.w3.org/ns/ldp#Resource';
private const OIDC_ISSUER = 'http://www.w3.org/ns/solid/terms#oidcIssuer';
public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly ?OidcClient $oidcClient = null,
) {
}
public function createContainer(string $parentUrl, string $name, ?string $data = null): ResponseInterface
{
return $this->post($parentUrl, $data, $name, true);
}
/**
* Creates a new resource by performing a Solid/LDP POST operation to a specified container.
*
* @see https://github.com/solid/solid-web-client/blob/main/src/client.js#L231=
*/
public function post(string $url, ?string $data = null, ?string $slug = null, bool $isContainer = false, array $options = []): ResponseInterface
{
if ($isContainer || !isset($options['headers']['Content-Type'])) {
$options['headers']['Content-Type'] = self::DEFAULT_MIME_TYPE;
}
if (null !== $data) {
$options['body'] = $data;
}
if (null !== $slug) {
$options['headers']['Slug'] = $slug;
}
$options['headers']['Link'] = \sprintf('<%s>; rel="type"', $isContainer ? self::LDP_BASIC_CONTAINER : self::LDP_RESOURCE);
return $this->request('POST', $url, $options);
}
public function put(string $url, ?string $data = null, bool $isContainer = false, array $options = []): ResponseInterface
{
if (!isset($options['headers']['Content-Type'])) {
$options['headers']['Content-Type'] = self::DEFAULT_MIME_TYPE;
}
if (null !== $data) {
$options['body'] = $data;
}
if ($isContainer) {
$options['headers']['Link'] = \sprintf('<%s>; rel="type"', self::LDP_BASIC_CONTAINER);
}
return $this->request('PUT', $url, $options);
}
public function get(string $url, array $options = []): ResponseInterface
{
return $this->request('GET', $url, $options);
}
public function head(string $url, array $options = []): ResponseInterface
{
return $this->request('HEAD', $url, $options);
}
public function delete(string $url, array $options = []): ResponseInterface
{
return $this->request('DELETE', $url, $options);
}
public function patch(string $url, string $data, string $contentType = 'application/sparql-update', array $options = []): ResponseInterface
{
$options['headers']['Content-Type'] = $contentType;
$options['body'] = $data;
return $this->request('PATCH', $url, $options);
}
public function getResourceMetadata(string $url, array $options = []): ResourceMetadata
{
$response = $this->head($url, $options);
return ResourceMetadata::fromResponseHeaders($response->getHeaders(false));
}
public function request(string $method, string $url, array $options = []): ResponseInterface
{
if ($accessToken = $this->oidcClient?->getAccessToken()) {
$options['headers']['Authorization'] = 'DPoP '.$accessToken;
$options['headers']['DPoP'] = $this->oidcClient->createDPoP($method, $url, true);
}
return $this->httpClient->request($method, $url, $options);
}
public function getProfile(string $webId, array $options = []): Graph
{
$response = $this->get($webId, $options);
if (null !== $format = $response->getHeaders()['content-type'][0] ?? null) {
// strip parameters (such as charset) if any
$format = explode(';', $format, 2)[0];
}
return new Graph($webId, $response->getContent(), $format);
}
public function getOidcIssuer(string $webId, array $options = []): string
{
$graph = $this->getProfile($webId, $options);
$issuer = $graph->get($webId, \sprintf('<%s>', self::OIDC_ISSUER))?->getUri();
if (!\is_string($issuer)) {
throw new Exception('Unable to find the OIDC issuer associated with this WebID', 1);
}
return $issuer;
}
}