Repository navigation
Expand file tree
/
Copy pathPolyscope.php
More file actions
183 lines (137 loc) · 4.99 KB
/
Copy pathPolyscope.php
File metadata and controls
183 lines (137 loc) · 4.99 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
namespace Polyscope\Laravel;
use GuzzleHttp\Client as HttpClient;
class Polyscope
{
use Actions\ManagesRepositories;
use Actions\ManagesServers;
use Actions\ManagesWorkspaceActions;
use Actions\ManagesWorkspaceMessages;
use Actions\ManagesWorkspaces;
use MakesHttpRequests;
public const DEFAULT_BASE_URL = 'https://getpolyscope.com';
protected ?string $apiToken = null;
protected string $baseUrl = self::DEFAULT_BASE_URL;
public int $timeout = 30;
public ?HttpClient $guzzle = null;
protected bool $usesCustomClient = false;
public function __construct(?string $apiToken = null, ?HttpClient $guzzle = null, ?string $baseUrl = null)
{
if ($baseUrl !== null) {
$this->setBaseUrl($baseUrl);
}
$apiToken ??= $this->resolveFallbackApiToken();
if ($apiToken !== null) {
$this->setApiToken($apiToken, $guzzle);
return;
}
if ($guzzle !== null) {
$this->guzzle = $guzzle;
}
}
/**
* @param array<int, array<string, mixed>> $collection
* @param class-string $class
* @param array<string, mixed> $extraData
* @return array<int, object>
*/
protected function transformCollection(array $collection, string $class, array $extraData = []): array
{
return array_map(function (array $attributes) use ($class, $extraData): object {
return new $class($attributes + $extraData, $this);
}, $collection);
}
public function setApiToken(string $apiToken, ?HttpClient $guzzle = null): static
{
$this->apiToken = $apiToken;
$this->usesCustomClient = $guzzle !== null;
$this->guzzle = $guzzle ?? new HttpClient([
'base_uri' => rtrim($this->baseUrl, '/').'/api/',
'http_errors' => false,
'headers' => [
'Authorization' => 'Bearer '.$this->apiToken,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => 'Polyscope Laravel SDK/0.1',
],
]);
return $this;
}
public function setApiKey(string $apiToken, ?HttpClient $guzzle = null): static
{
return $this->setApiToken($apiToken, $guzzle);
}
public function getApiToken(): ?string
{
return $this->apiToken;
}
public function setBaseUrl(string $baseUrl): static
{
$this->baseUrl = rtrim($baseUrl, '/');
if ($this->apiToken !== null && ! $this->usesCustomClient) {
$this->setApiToken($this->apiToken);
}
return $this;
}
public function getBaseUrl(): string
{
return $this->baseUrl;
}
public function setTimeout(int $timeout): static
{
$this->timeout = $timeout;
return $this;
}
public function getTimeout(): int
{
return $this->timeout;
}
protected function resolveFallbackApiToken(): ?string
{
foreach ($this->candidateSettingsPaths() as $settingsPath) {
if (! is_file($settingsPath) || ! is_readable($settingsPath)) {
continue;
}
$settingsContents = file_get_contents($settingsPath);
if (! is_string($settingsContents) || trim($settingsContents) === '') {
continue;
}
$settings = json_decode($settingsContents, true);
if (! is_array($settings)) {
continue;
}
$token = $settings['authToken'] ?? null;
if (is_string($token) && trim($token) !== '') {
return trim($token);
}
}
return null;
}
/**
* @return array<int, string>
*/
protected function candidateSettingsPaths(): array
{
$paths = [];
$customSettingsPath = getenv('POLYSCOPE_SETTINGS_PATH');
if (is_string($customSettingsPath) && trim($customSettingsPath) !== '') {
$paths[] = trim($customSettingsPath);
}
$home = getenv('HOME');
if (! is_string($home) || trim($home) === '') {
$home = getenv('USERPROFILE');
}
if (is_string($home) && trim($home) !== '') {
$home = rtrim($home, '/\\');
$paths[] = $home.DIRECTORY_SEPARATOR.'.polyscope'.DIRECTORY_SEPARATOR.'settings.json';
$paths[] = $home.DIRECTORY_SEPARATOR.'Library'.DIRECTORY_SEPARATOR.'Application Support'.DIRECTORY_SEPARATOR.'com.getpolyscope.app'.DIRECTORY_SEPARATOR.'settings.json';
$paths[] = $home.DIRECTORY_SEPARATOR.'.config'.DIRECTORY_SEPARATOR.'com.getpolyscope.app'.DIRECTORY_SEPARATOR.'settings.json';
}
$appData = getenv('APPDATA');
if (is_string($appData) && trim($appData) !== '') {
$paths[] = rtrim($appData, '/\\').DIRECTORY_SEPARATOR.'com.getpolyscope.app'.DIRECTORY_SEPARATOR.'settings.json';
}
return array_values(array_unique($paths));
}
}