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