1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Filesystem; |
15
|
|
|
|
16
|
|
|
use League\Flysystem\FilesystemException; |
17
|
|
|
use League\Flysystem\FilesystemOperator as FlysystemInterface; |
18
|
|
|
use League\Flysystem\StorageAttributes; |
19
|
|
|
use Valkyrja\Filesystem\Contract\Filesystem as Contract; |
20
|
|
|
use Valkyrja\Filesystem\Enum\Visibility; |
21
|
|
|
|
22
|
|
|
use function array_map; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class FlysystemFilesystem. |
26
|
|
|
* |
27
|
|
|
* @author Melech Mizrachi |
28
|
|
|
*/ |
29
|
|
|
class FlysystemFilesystem implements Contract |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* FlysystemFilesystem constructor. |
33
|
|
|
* |
34
|
|
|
* @param FlysystemInterface $flysystem The Flysystem filesystem |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
protected FlysystemInterface $flysystem |
38
|
|
|
) { |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
* |
44
|
|
|
* @throws FilesystemException |
45
|
|
|
*/ |
46
|
|
|
public function exists(string $path): bool |
47
|
|
|
{ |
48
|
|
|
return $this->flysystem->has($path); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
* |
54
|
|
|
* @throws FilesystemException |
55
|
|
|
*/ |
56
|
|
|
public function read(string $path): string |
57
|
|
|
{ |
58
|
|
|
return $this->flysystem->read($path); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
* |
64
|
|
|
* @throws FilesystemException |
65
|
|
|
*/ |
66
|
|
|
public function write(string $path, string $contents): bool |
67
|
|
|
{ |
68
|
|
|
$this->flysystem->write($path, $contents); |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
* |
76
|
|
|
* @throws FilesystemException |
77
|
|
|
*/ |
78
|
|
|
public function writeStream(string $path, $resource): bool |
79
|
|
|
{ |
80
|
|
|
$this->flysystem->writeStream($path, $resource); |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
* |
88
|
|
|
* @throws FilesystemException |
89
|
|
|
*/ |
90
|
|
|
public function update(string $path, string $contents): bool |
91
|
|
|
{ |
92
|
|
|
return $this->write($path, $contents); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
* |
98
|
|
|
* @throws FilesystemException |
99
|
|
|
*/ |
100
|
|
|
public function updateStream(string $path, $resource): bool |
101
|
|
|
{ |
102
|
|
|
return $this->writeStream($path, $resource); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
* |
108
|
|
|
* @throws FilesystemException |
109
|
|
|
*/ |
110
|
|
|
public function put(string $path, string $contents): bool |
111
|
|
|
{ |
112
|
|
|
return $this->write($path, $contents); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
* |
118
|
|
|
* @throws FilesystemException |
119
|
|
|
*/ |
120
|
|
|
public function putStream(string $path, $resource): bool |
121
|
|
|
{ |
122
|
|
|
return $this->writeStream($path, $resource); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritDoc |
127
|
|
|
* |
128
|
|
|
* @throws FilesystemException |
129
|
|
|
*/ |
130
|
|
|
public function rename(string $path, string $newPath): bool |
131
|
|
|
{ |
132
|
|
|
$this->flysystem->move($path, $newPath); |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritDoc |
139
|
|
|
* |
140
|
|
|
* @throws FilesystemException |
141
|
|
|
*/ |
142
|
|
|
public function copy(string $path, string $newPath): bool |
143
|
|
|
{ |
144
|
|
|
$this->flysystem->copy($path, $newPath); |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
* |
152
|
|
|
* @throws FilesystemException |
153
|
|
|
*/ |
154
|
|
|
public function delete(string $path): bool |
155
|
|
|
{ |
156
|
|
|
$this->flysystem->delete($path); |
157
|
|
|
|
158
|
|
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritDoc |
163
|
|
|
*/ |
164
|
|
|
public function metadata(string $path): array|null |
165
|
|
|
{ |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
* |
172
|
|
|
* @throws FilesystemException |
173
|
|
|
*/ |
174
|
|
|
public function mimetype(string $path): string|null |
175
|
|
|
{ |
176
|
|
|
return $this->flysystem->mimeType($path); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
* |
182
|
|
|
* @throws FilesystemException |
183
|
|
|
*/ |
184
|
|
|
public function size(string $path): int|null |
185
|
|
|
{ |
186
|
|
|
return $this->flysystem->fileSize($path); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @inheritDoc |
191
|
|
|
* |
192
|
|
|
* @throws FilesystemException |
193
|
|
|
*/ |
194
|
|
|
public function timestamp(string $path): int|null |
195
|
|
|
{ |
196
|
|
|
return $this->flysystem->lastModified($path); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritDoc |
201
|
|
|
* |
202
|
|
|
* @throws FilesystemException |
203
|
|
|
*/ |
204
|
|
|
public function visibility(string $path): string|null |
205
|
|
|
{ |
206
|
|
|
return $this->flysystem->visibility($path); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritDoc |
211
|
|
|
* |
212
|
|
|
* @throws FilesystemException |
213
|
|
|
*/ |
214
|
|
|
public function setVisibility(string $path, Visibility $visibility): bool |
215
|
|
|
{ |
216
|
|
|
$this->flysystem->setVisibility($path, $visibility->value); |
217
|
|
|
|
218
|
|
|
return true; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @inheritDoc |
223
|
|
|
* |
224
|
|
|
* @throws FilesystemException |
225
|
|
|
*/ |
226
|
|
|
public function setVisibilityPublic(string $path): bool |
227
|
|
|
{ |
228
|
|
|
$this->flysystem->setVisibility($path, Visibility::PUBLIC->value); |
229
|
|
|
|
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @inheritDoc |
235
|
|
|
* |
236
|
|
|
* @throws FilesystemException |
237
|
|
|
*/ |
238
|
|
|
public function setVisibilityPrivate(string $path): bool |
239
|
|
|
{ |
240
|
|
|
$this->flysystem->setVisibility($path, Visibility::PRIVATE->value); |
241
|
|
|
|
242
|
|
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @inheritDoc |
247
|
|
|
* |
248
|
|
|
* @throws FilesystemException |
249
|
|
|
*/ |
250
|
|
|
public function createDir(string $path): bool |
251
|
|
|
{ |
252
|
|
|
$this->flysystem->createDirectory($path); |
253
|
|
|
|
254
|
|
|
return true; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritDoc |
259
|
|
|
* |
260
|
|
|
* @throws FilesystemException |
261
|
|
|
*/ |
262
|
|
|
public function deleteDir(string $path): bool |
263
|
|
|
{ |
264
|
|
|
$this->flysystem->deleteDirectory($path); |
265
|
|
|
|
266
|
|
|
return true; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @inheritDoc |
271
|
|
|
* |
272
|
|
|
* @throws FilesystemException |
273
|
|
|
* |
274
|
|
|
* @psalm-suppress MixedReturnTypeCoercion |
275
|
|
|
*/ |
276
|
|
|
public function listContents(string|null $directory = null, bool $recursive = false): array |
277
|
|
|
{ |
278
|
|
|
return array_map( |
279
|
|
|
/** |
280
|
|
|
* @return array<string, string|int> |
281
|
|
|
*/ |
282
|
|
|
static fn (StorageAttributes $attributes): array => (array) $attributes->jsonSerialize(), |
283
|
|
|
$this->flysystem->listContents($directory ?? '', $recursive)->toArray() |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @inheritDoc |
289
|
|
|
*/ |
290
|
|
|
public function getFlysystem(): FlysystemInterface |
291
|
|
|
{ |
292
|
|
|
return $this->flysystem; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|