|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/data-file |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/data-file |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Node\FileSystem; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use League\Flysystem\AdapterInterface; |
|
18
|
|
|
use League\Flysystem\FileExistsException; |
|
19
|
|
|
use League\Flysystem\FileNotFoundException; |
|
20
|
|
|
use League\Flysystem\FilesystemInterface; |
|
21
|
|
|
use League\Flysystem\Handler; |
|
22
|
|
|
use League\Flysystem\PluginInterface; |
|
23
|
|
|
use League\Flysystem\RootViolationException; |
|
24
|
|
|
|
|
25
|
|
|
class FilesystemWrapper implements FilesystemWrapperInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var FilesystemInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fileSystem; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* FilesystemWrapper constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param FilesystemInterface $fileSystem |
|
36
|
|
|
*/ |
|
37
|
117 |
|
public function __construct(FilesystemInterface $fileSystem) |
|
38
|
|
|
{ |
|
39
|
117 |
|
$this->fileSystem = $fileSystem; |
|
40
|
117 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Check whether a file exists. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $path |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
69 |
|
public function has($path) |
|
50
|
|
|
{ |
|
51
|
69 |
|
return $this->fileSystem->has($path); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Read a file. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $path The path to the file. |
|
58
|
|
|
* |
|
59
|
|
|
* @throws FileNotFoundException |
|
60
|
|
|
* |
|
61
|
|
|
* @return string|false The file contents or false on failure. |
|
62
|
|
|
*/ |
|
63
|
25 |
|
public function read($path) |
|
64
|
|
|
{ |
|
65
|
25 |
|
return $this->fileSystem->read($path); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Retrieves a read-stream for a path. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $path The path to the file. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws FileNotFoundException |
|
74
|
|
|
* |
|
75
|
|
|
* @return resource|false The path resource or false on failure. |
|
76
|
|
|
*/ |
|
77
|
8 |
|
public function readStream($path) |
|
78
|
|
|
{ |
|
79
|
8 |
|
return $this->fileSystem->readStream($path); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* List contents of a directory. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $directory The directory to list. |
|
86
|
|
|
* @param bool $recursive Whether to list recursively. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array A list of file metadata. |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function listContents($directory = '', $recursive = false) |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->fileSystem->listContents($directory, $recursive); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get a file's metadata. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $path The path to the file. |
|
99
|
|
|
* |
|
100
|
|
|
* @throws FileNotFoundException |
|
101
|
|
|
* |
|
102
|
|
|
* @return array|false The file metadata or false on failure. |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function getMetadata($path) |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $this->fileSystem->getMetadata($path); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get a file's size. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $path The path to the file. |
|
113
|
|
|
* |
|
114
|
|
|
* @return int|false The file size or false on failure. |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function getSize($path) |
|
117
|
|
|
{ |
|
118
|
1 |
|
return $this->fileSystem->getSize($path); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get a file's mime-type. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $path The path to the file. |
|
125
|
|
|
* |
|
126
|
|
|
* @throws FileNotFoundException |
|
127
|
|
|
* |
|
128
|
|
|
* @return string|false The file mime-type or false on failure. |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function getMimetype($path) |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->fileSystem->getMimetype($path); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get a file's timestamp. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $path The path to the file. |
|
139
|
|
|
* |
|
140
|
|
|
* @throws FileNotFoundException |
|
141
|
|
|
* |
|
142
|
|
|
* @return string|false The timestamp or false on failure. |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function getTimestamp($path) |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $this->fileSystem->getTimestamp($path); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get a file's visibility. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $path The path to the file. |
|
153
|
|
|
* |
|
154
|
|
|
* @throws FileNotFoundException |
|
155
|
|
|
* |
|
156
|
|
|
* @return string|false The visibility (public|private) or false on failure. |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function getVisibility($path) |
|
159
|
|
|
{ |
|
160
|
1 |
|
return $this->fileSystem->getVisibility($path); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Write a new file. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $path The path of the new file. |
|
167
|
|
|
* @param string $contents The file contents. |
|
168
|
|
|
* @param array $config An optional configuration array. |
|
169
|
|
|
* |
|
170
|
|
|
* @throws FileExistsException |
|
171
|
|
|
* |
|
172
|
|
|
* @return bool True on success, false on failure. |
|
173
|
|
|
*/ |
|
174
|
2 |
|
public function write($path, $contents, array $config = []) |
|
175
|
|
|
{ |
|
176
|
2 |
|
return $this->fileSystem->write($path, $contents, $config); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Write a new file using a stream. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $path The path of the new file. |
|
183
|
|
|
* @param resource $resource The file handle. |
|
184
|
|
|
* @param array $config An optional configuration array. |
|
185
|
|
|
* |
|
186
|
|
|
* @throws \League\Flysystem\InvalidArgumentException If $resource is not a file handle. |
|
187
|
|
|
* @throws FileExistsException |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool True on success, false on failure. |
|
190
|
|
|
*/ |
|
191
|
4 |
|
public function writeStream($path, $resource, array $config = []) |
|
192
|
|
|
{ |
|
193
|
4 |
|
return $this->fileSystem->writeStream($path, $resource, $config); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Update an existing file. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $path The path of the existing file. |
|
200
|
|
|
* @param string $contents The file contents. |
|
201
|
|
|
* @param array $config An optional configuration array. |
|
202
|
|
|
* |
|
203
|
|
|
* @throws FileNotFoundException |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool True on success, false on failure. |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function update($path, $contents, array $config = []) |
|
208
|
|
|
{ |
|
209
|
1 |
|
return $this->fileSystem->update($path, $contents, $config); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Update an existing file using a stream. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $path The path of the existing file. |
|
216
|
|
|
* @param resource $resource The file handle. |
|
217
|
|
|
* @param array $config An optional configuration array. |
|
218
|
|
|
* |
|
219
|
|
|
* @throws \League\Flysystem\InvalidArgumentException If $resource is not a file handle. |
|
220
|
|
|
* @throws FileNotFoundException |
|
221
|
|
|
* |
|
222
|
|
|
* @return bool True on success, false on failure. |
|
223
|
|
|
*/ |
|
224
|
1 |
|
public function updateStream($path, $resource, array $config = []) |
|
225
|
|
|
{ |
|
226
|
1 |
|
return $this->fileSystem->updateStream($path, $resource, $config); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Rename a file. |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $path Path to the existing file. |
|
233
|
|
|
* @param string $newpath The new path of the file. |
|
234
|
|
|
* |
|
235
|
|
|
* @throws FileExistsException Thrown if $newpath exists. |
|
236
|
|
|
* @throws FileNotFoundException Thrown if $path does not exist. |
|
237
|
|
|
* |
|
238
|
|
|
* @return bool True on success, false on failure. |
|
239
|
|
|
*/ |
|
240
|
1 |
|
public function rename($path, $newpath) |
|
241
|
|
|
{ |
|
242
|
1 |
|
return $this->fileSystem->rename($path, $newpath); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Copy a file. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $path Path to the existing file. |
|
249
|
|
|
* @param string $newpath The new path of the file. |
|
250
|
|
|
* |
|
251
|
|
|
* @throws FileExistsException Thrown if $newpath exists. |
|
252
|
|
|
* @throws FileNotFoundException Thrown if $path does not exist. |
|
253
|
|
|
* |
|
254
|
|
|
* @return bool True on success, false on failure. |
|
255
|
|
|
*/ |
|
256
|
5 |
|
public function copy($path, $newpath) |
|
257
|
|
|
{ |
|
258
|
5 |
|
return $this->fileSystem->copy($path, $newpath); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Delete a file. |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $path |
|
265
|
|
|
* |
|
266
|
|
|
* @throws FileNotFoundException |
|
267
|
|
|
* |
|
268
|
|
|
* @return bool True on success, false on failure. |
|
269
|
|
|
*/ |
|
270
|
15 |
|
public function delete($path) |
|
271
|
|
|
{ |
|
272
|
15 |
|
return $this->fileSystem->delete($path); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Delete a directory. |
|
277
|
|
|
* |
|
278
|
|
|
* @param string $dirname |
|
279
|
|
|
* |
|
280
|
|
|
* @throws RootViolationException Thrown if $dirname is empty. |
|
281
|
|
|
* |
|
282
|
|
|
* @return bool True on success, false on failure. |
|
283
|
|
|
*/ |
|
284
|
1 |
|
public function deleteDir($dirname) |
|
285
|
|
|
{ |
|
286
|
1 |
|
return $this->fileSystem->deleteDir($dirname); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Create a directory. |
|
291
|
|
|
* |
|
292
|
|
|
* @param string $dirname The name of the new directory. |
|
293
|
|
|
* @param array $config An optional configuration array. |
|
294
|
|
|
* |
|
295
|
|
|
* @return bool True on success, false on failure. |
|
296
|
|
|
*/ |
|
297
|
10 |
|
public function createDir($dirname, array $config = []) |
|
298
|
|
|
{ |
|
299
|
10 |
|
return $this->fileSystem->createDir($dirname, $config); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Set the visibility for a file. |
|
304
|
|
|
* |
|
305
|
|
|
* @param string $path The path to the file. |
|
306
|
|
|
* @param string $visibility One of 'public' or 'private'. |
|
307
|
|
|
* |
|
308
|
|
|
* @return bool True on success, false on failure. |
|
309
|
|
|
*/ |
|
310
|
1 |
|
public function setVisibility($path, $visibility) |
|
311
|
|
|
{ |
|
312
|
1 |
|
return $this->fileSystem->setVisibility($path, $visibility); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Create a file or update if exists. |
|
317
|
|
|
* |
|
318
|
|
|
* @param string $path The path to the file. |
|
319
|
|
|
* @param string $contents The file contents. |
|
320
|
|
|
* @param array $config An optional configuration array. |
|
321
|
|
|
* |
|
322
|
|
|
* @return bool True on success, false on failure. |
|
323
|
|
|
*/ |
|
324
|
86 |
|
public function put($path, $contents, array $config = []) |
|
325
|
|
|
{ |
|
326
|
86 |
|
return $this->fileSystem->put($path, $contents, $config); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Create a file or update if exists. |
|
331
|
|
|
* |
|
332
|
|
|
* @param string $path The path to the file. |
|
333
|
|
|
* @param resource $resource The file handle. |
|
334
|
|
|
* @param array $config An optional configuration array. |
|
335
|
|
|
* |
|
336
|
|
|
* @throws \League\Flysystem\InvalidArgumentException Thrown if $resource is not a resource. |
|
337
|
|
|
* |
|
338
|
|
|
* @return bool True on success, false on failure. |
|
339
|
|
|
*/ |
|
340
|
1 |
|
public function putStream($path, $resource, array $config = []) |
|
341
|
|
|
{ |
|
342
|
1 |
|
return $this->fileSystem->putStream($path, $resource, $config); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Read and delete a file. |
|
347
|
|
|
* |
|
348
|
|
|
* @param string $path The path to the file. |
|
349
|
|
|
* |
|
350
|
|
|
* @throws FileNotFoundException |
|
351
|
|
|
* |
|
352
|
|
|
* @return string|false The file contents, or false on failure. |
|
353
|
|
|
*/ |
|
354
|
1 |
|
public function readAndDelete($path) |
|
355
|
|
|
{ |
|
356
|
1 |
|
return $this->fileSystem->readAndDelete($path); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Get a file/directory handler. |
|
361
|
|
|
* |
|
362
|
|
|
* @param string $path The path to the file. |
|
363
|
|
|
* @param Handler $handler An optional existing handler to populate. |
|
|
|
|
|
|
364
|
|
|
* |
|
365
|
|
|
* @return Handler Either a file or directory handler. |
|
366
|
|
|
*/ |
|
367
|
1 |
|
public function get($path, Handler $handler = null) |
|
368
|
|
|
{ |
|
369
|
1 |
|
return $this->fileSystem->get($path, $handler); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Register a plugin. |
|
374
|
|
|
* |
|
375
|
|
|
* @param PluginInterface $plugin The plugin to register. |
|
376
|
|
|
* |
|
377
|
|
|
* @return $this |
|
378
|
|
|
*/ |
|
379
|
1 |
|
public function addPlugin(PluginInterface $plugin) |
|
380
|
|
|
{ |
|
381
|
1 |
|
return $this->fileSystem->addPlugin($plugin); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* @return AdapterInterface |
|
386
|
|
|
*/ |
|
387
|
2 |
|
public function getAdapter() |
|
388
|
|
|
{ |
|
389
|
2 |
|
if (!method_exists($this->fileSystem, 'getAdapter')) { |
|
390
|
1 |
|
throw new InvalidArgumentException( |
|
391
|
|
|
"This wrapper requires the getAdapter method to exist on the fileSystem" |
|
392
|
1 |
|
); |
|
393
|
|
|
} |
|
394
|
1 |
|
return $this->fileSystem->getAdapter(); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.