|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Twistor\Flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use Guzzle\Http\Client; |
|
6
|
|
|
use Guzzle\Http\ClientInterface; |
|
7
|
|
|
use Guzzle\Http\Exception\BadResponseException; |
|
8
|
|
|
use League\Flysystem\AdapterInterface; |
|
9
|
|
|
use League\Flysystem\Config; |
|
10
|
|
|
use League\Flysystem\Util\MimeType; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Uses Guzzle as a backend for HTTP URLs. |
|
14
|
|
|
*/ |
|
15
|
|
|
class GuzzleAdapter implements AdapterInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The base URL. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $base; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The Guzzle HTTP client. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $client; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The visibility of this adapter. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructs a GuzzleAdapter object. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $base The base URL. |
|
42
|
|
|
* @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function __construct($base, ClientInterface $client = null) |
|
45
|
|
|
{ |
|
46
|
3 |
|
$this->client = $client ?: new Client(); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
3 |
|
$parsed = parse_url($base); |
|
49
|
3 |
|
$this->base = $parsed['scheme'] . '://'; |
|
50
|
|
|
|
|
51
|
3 |
|
if (isset($parsed['user'])) { |
|
52
|
3 |
|
$this->visibility = AdapterInterface::VISIBILITY_PRIVATE; |
|
53
|
3 |
|
$this->base .= $parsed['user']; |
|
54
|
|
|
|
|
55
|
3 |
View Code Duplication |
if (isset($parsed['pass']) && $parsed['pass'] !== '') { |
|
|
|
|
|
|
56
|
3 |
|
$this->base .= ':' . $parsed['pass']; |
|
57
|
3 |
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
$this->base .= '@'; |
|
60
|
3 |
|
}; |
|
61
|
|
|
|
|
62
|
3 |
|
$this->base .= $parsed['host'] . '/'; |
|
63
|
|
|
|
|
64
|
3 |
View Code Duplication |
if (isset($parsed['path']) && $parsed['path'] !== '/') { |
|
|
|
|
|
|
65
|
3 |
|
$this->base .= trim($parsed['path'], '/') . '/'; |
|
66
|
3 |
|
} |
|
67
|
3 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the base URL. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string The base URL. |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function getBaseUrl() |
|
75
|
|
|
{ |
|
76
|
3 |
|
return $this->base; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function copy($path, $newpath) |
|
83
|
|
|
{ |
|
84
|
3 |
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function createDir($path, Config $config) |
|
91
|
|
|
{ |
|
92
|
3 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
*/ |
|
98
|
3 |
|
public function delete($path) |
|
99
|
|
|
{ |
|
100
|
3 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @inheritdoc |
|
105
|
|
|
*/ |
|
106
|
3 |
|
public function deleteDir($path) |
|
107
|
|
|
{ |
|
108
|
3 |
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @inheritdoc |
|
113
|
|
|
*/ |
|
114
|
3 |
|
public function getMetadata($path) |
|
115
|
|
|
{ |
|
116
|
3 |
|
if (! $response = $this->head($path)) { |
|
117
|
3 |
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
if ($mimetype = $response->getHeader('Content-Type')) { |
|
121
|
3 |
|
list($mimetype) = explode(';', $mimetype, 2); |
|
122
|
3 |
|
$mimetype = trim($mimetype); |
|
123
|
3 |
|
} else { |
|
124
|
|
|
// Remove any query strings or fragments. |
|
125
|
3 |
|
list($path) = explode('#', $path, 2); |
|
126
|
3 |
|
list($path) = explode('?', $path, 2); |
|
127
|
3 |
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
|
128
|
3 |
|
$mimetype = $extension ? MimeType::detectByFileExtension($extension) : 'text/plain'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return [ |
|
132
|
3 |
|
'type' => 'file', |
|
133
|
3 |
|
'path' => $path, |
|
134
|
3 |
|
'timestamp' => (int) strtotime($response->getHeader('Last-Modified')), |
|
135
|
3 |
|
'size' => (int) trim($response->getHeader('Content-Length')), |
|
136
|
3 |
|
'visibility' => $this->visibility, |
|
137
|
3 |
|
'mimetype' => $mimetype, |
|
138
|
3 |
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @inheritdoc |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function getMimetype($path) |
|
145
|
|
|
{ |
|
146
|
3 |
|
return $this->getMetadata($path); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @inheritdoc |
|
151
|
|
|
*/ |
|
152
|
3 |
|
public function getSize($path) |
|
153
|
|
|
{ |
|
154
|
3 |
|
return $this->getMetadata($path); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @inheritdoc |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public function getTimestamp($path) |
|
161
|
|
|
{ |
|
162
|
3 |
|
return $this->getMetadata($path); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @inheritdoc |
|
167
|
|
|
*/ |
|
168
|
3 |
|
public function getVisibility($path) |
|
169
|
|
|
{ |
|
170
|
|
|
return [ |
|
171
|
3 |
|
'path' => $path, |
|
172
|
3 |
|
'visibility' => $this->visibility, |
|
173
|
3 |
|
]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @inheritdoc |
|
178
|
|
|
*/ |
|
179
|
3 |
|
public function has($path) |
|
180
|
|
|
{ |
|
181
|
3 |
|
if (! $response = $this->head($path)) { |
|
182
|
3 |
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
3 |
|
return $response->getStatusCode() === 200; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @inheritdoc |
|
190
|
|
|
*/ |
|
191
|
3 |
|
public function listContents($directory = '', $recursive = false) |
|
192
|
|
|
{ |
|
193
|
3 |
|
return []; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @inheritdoc |
|
198
|
|
|
*/ |
|
199
|
3 |
|
public function read($path) |
|
200
|
|
|
{ |
|
201
|
3 |
|
if (! $response = $this->get($path)) { |
|
202
|
3 |
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return [ |
|
206
|
3 |
|
'path' => $path, |
|
207
|
3 |
|
'contents' => (string) $response->getBody(), |
|
208
|
3 |
|
]; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @inheritdoc |
|
213
|
|
|
*/ |
|
214
|
3 |
|
public function readStream($path) |
|
215
|
|
|
{ |
|
216
|
3 |
|
if (! $response = $this->get($path)) { |
|
217
|
3 |
|
return false; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return [ |
|
221
|
3 |
|
'path' => $path, |
|
222
|
3 |
|
'stream' => $response->getBody()->getStream(), |
|
223
|
3 |
|
]; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @inheritdoc |
|
228
|
|
|
*/ |
|
229
|
3 |
|
public function rename($path, $newpath) |
|
230
|
|
|
{ |
|
231
|
3 |
|
return false; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @inheritdoc |
|
236
|
|
|
*/ |
|
237
|
3 |
|
public function setVisibility($path, $visibility) |
|
238
|
|
|
{ |
|
239
|
3 |
|
if ($visibility === $this->visibility) { |
|
240
|
3 |
|
return $this->getVisibility($path); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
3 |
|
return false; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @inheritdoc |
|
248
|
|
|
*/ |
|
249
|
3 |
|
public function update($path, $contents, Config $conf) |
|
250
|
|
|
{ |
|
251
|
3 |
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @inheritdoc |
|
256
|
|
|
*/ |
|
257
|
3 |
|
public function updateStream($path, $resource, Config $config) |
|
258
|
|
|
{ |
|
259
|
3 |
|
return false; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @inheritdoc |
|
264
|
|
|
*/ |
|
265
|
3 |
|
public function write($path, $contents, Config $config) |
|
266
|
|
|
{ |
|
267
|
3 |
|
return false; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @inheritdoc |
|
272
|
|
|
*/ |
|
273
|
3 |
|
public function writeStream($path, $resource, Config $config) |
|
274
|
|
|
{ |
|
275
|
3 |
|
return false; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Performs a GET request. |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $path The path to GET. |
|
282
|
|
|
* |
|
283
|
|
|
* @return Response|false The response or false if failed. |
|
284
|
|
|
*/ |
|
285
|
6 |
View Code Duplication |
protected function get($path) |
|
|
|
|
|
|
286
|
|
|
{ |
|
287
|
|
|
try { |
|
288
|
6 |
|
$response = $this->client->get($this->base . $path)->send(); |
|
289
|
6 |
|
} catch (BadResponseException $e) { |
|
290
|
6 |
|
return false; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
6 |
|
if ($response->getStatusCode() !== 200) { |
|
294
|
3 |
|
return false; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
6 |
|
return $response; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Performs a HEAD request. |
|
302
|
|
|
* |
|
303
|
|
|
* @param string $path The path to HEAD. |
|
304
|
|
|
* |
|
305
|
|
|
* @return Response|false The response or false if failed. |
|
306
|
|
|
*/ |
|
307
|
6 |
View Code Duplication |
protected function head($path) |
|
|
|
|
|
|
308
|
|
|
{ |
|
309
|
|
|
try { |
|
310
|
6 |
|
$response = $this->client->head($this->base . $path)->send(); |
|
311
|
6 |
|
} catch (BadResponseException $e) { |
|
312
|
6 |
|
return false; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
6 |
|
if ($response->getStatusCode() !== 200) { |
|
316
|
3 |
|
return false; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
6 |
|
return $response; |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..