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