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