|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Storage\Bucket; |
|
13
|
|
|
|
|
14
|
|
|
use JetBrains\PhpStorm\ExpectedValues; |
|
15
|
|
|
use League\Flysystem\FilesystemException; |
|
16
|
|
|
use League\Flysystem\FilesystemOperator; |
|
17
|
|
|
use Spiral\Storage\Exception\FileOperationException; |
|
18
|
|
|
use Spiral\Storage\FileInterface; |
|
19
|
|
|
use Spiral\Storage\BucketInterface; |
|
20
|
|
|
use Spiral\Storage\Visibility; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @mixin WritableInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
trait WritableTrait |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritDoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function create(string $pathname, array $config = []): FileInterface |
|
31
|
|
|
{ |
|
32
|
|
|
if ($this instanceof ReadableInterface && !$this->exists($pathname)) { |
|
33
|
|
|
return $this->write($pathname, '', $config); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $this->file($pathname); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritDoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function write(string $pathname, $content, array $config = []): FileInterface |
|
43
|
|
|
{ |
|
44
|
|
|
assert(\is_resource($content) || $this->isStringable($content)); |
|
45
|
|
|
|
|
46
|
|
|
$fs = $this->getOperator(); |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
switch (true) { |
|
50
|
|
|
case \is_object($content): |
|
|
|
|
|
|
51
|
|
|
case \is_string($content): |
|
52
|
|
|
$fs->write($pathname, (string)$content, $config); |
|
53
|
|
|
break; |
|
54
|
|
|
|
|
55
|
|
|
case \is_resource($content): |
|
56
|
|
|
$fs->writeStream($pathname, $content, $config); |
|
57
|
|
|
break; |
|
58
|
|
|
|
|
59
|
|
|
default: |
|
60
|
|
|
$message = 'Content must be a resource stream or stringable type, but %s passed'; |
|
61
|
|
|
throw new \InvalidArgumentException(\sprintf($message, \get_debug_type($content))); |
|
62
|
|
|
} |
|
63
|
|
|
} catch (FilesystemException $e) { |
|
64
|
|
|
throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->file($pathname); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setVisibility( |
|
74
|
|
|
string $pathname, |
|
75
|
|
|
#[ExpectedValues(valuesFromClass: Visibility::class)] |
|
76
|
|
|
string $visibility |
|
77
|
|
|
): FileInterface { |
|
78
|
|
|
$fs = $this->getOperator(); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$fs->setVisibility($pathname, $this->toFlysystemVisibility($visibility)); |
|
82
|
|
|
} catch (FilesystemException $e) { |
|
83
|
|
|
throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->file($pathname); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritDoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function copy( |
|
93
|
|
|
string $source, |
|
94
|
|
|
string $destination, |
|
95
|
|
|
BucketInterface $storage = null, |
|
96
|
|
|
array $config = [] |
|
97
|
|
|
): FileInterface { |
|
98
|
|
|
$fs = $this->getOperator(); |
|
99
|
|
|
|
|
100
|
|
|
if ($storage === null || $storage === $this) { |
|
101
|
|
|
try { |
|
102
|
|
|
$fs->copy($source, $destination, $config); |
|
103
|
|
|
} catch (FilesystemException $e) { |
|
104
|
|
|
throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->file($destination); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $storage->write($destination, $this->getStream($source), $config); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritDoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function move( |
|
117
|
|
|
string $source, |
|
118
|
|
|
string $destination, |
|
119
|
|
|
BucketInterface $storage = null, |
|
120
|
|
|
array $config = [] |
|
121
|
|
|
): FileInterface { |
|
122
|
|
|
$fs = $this->getOperator(); |
|
123
|
|
|
|
|
124
|
|
|
if ($storage === null || $storage === $this) { |
|
125
|
|
|
try { |
|
126
|
|
|
$fs->move($source, $destination, $config); |
|
127
|
|
|
} catch (FilesystemException $e) { |
|
128
|
|
|
throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->file($destination); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$result = $storage->write($destination, $this->getStream($source), $config); |
|
135
|
|
|
|
|
136
|
|
|
$fs->delete($source); |
|
137
|
|
|
|
|
138
|
|
|
return $result; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritDoc} |
|
143
|
|
|
*/ |
|
144
|
|
|
public function delete(string $pathname, bool $clean = false): void |
|
145
|
|
|
{ |
|
146
|
|
|
$fs = $this->getOperator(); |
|
147
|
|
|
|
|
148
|
|
|
try { |
|
149
|
|
|
$fs->delete($pathname); |
|
150
|
|
|
|
|
151
|
|
|
if ($clean) { |
|
152
|
|
|
$this->deleteEmptyDirectories($this->getParentDirectory($pathname)); |
|
153
|
|
|
} |
|
154
|
|
|
} catch (FilesystemException $e) { |
|
155
|
|
|
throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
/** |
|
159
|
|
|
* @return FilesystemOperator |
|
160
|
|
|
*/ |
|
161
|
|
|
abstract protected function getOperator(): FilesystemOperator; |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $visibility |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
#[ExpectedValues(valuesFromClass: \League\Flysystem\Visibility::class)] |
|
168
|
|
|
private function toFlysystemVisibility( |
|
169
|
|
|
#[ExpectedValues(valuesFromClass: Visibility::class)] |
|
170
|
|
|
string $visibility |
|
171
|
|
|
): string { |
|
172
|
|
|
return ($visibility === Visibility::VISIBILITY_PUBLIC) |
|
173
|
|
|
? \League\Flysystem\Visibility::PUBLIC |
|
174
|
|
|
: \League\Flysystem\Visibility::PRIVATE; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Internal helper method that returns directory name of passed path. |
|
179
|
|
|
* |
|
180
|
|
|
* Please note that the use of the PHP {@see \dirname()} function depends |
|
181
|
|
|
* on the operating system and it MAY NOT return correct parent directory |
|
182
|
|
|
* in the case of slash character (`/` or `\`) incompatible with the |
|
183
|
|
|
* current runtime. |
|
184
|
|
|
* |
|
185
|
|
|
* @internal This is an internal method, please do not use it in your code. |
|
186
|
|
|
* @psalm-internal Spiral\Storage\Storage |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $path |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
private function getParentDirectory(string $path): string |
|
192
|
|
|
{ |
|
193
|
|
|
return \dirname(\str_replace(['\\', '/'], \DIRECTORY_SEPARATOR, $path)); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Internal helper method that returns bool {@see true} if the passed |
|
198
|
|
|
* directory is the root for the file. |
|
199
|
|
|
* |
|
200
|
|
|
* @internal This is an internal method, please do not use it in your code. |
|
201
|
|
|
* @psalm-internal Spiral\Storage\Storage |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $directory |
|
204
|
|
|
* @return bool |
|
205
|
|
|
*/ |
|
206
|
|
|
private function hasParentDirectory(string $directory): bool |
|
207
|
|
|
{ |
|
208
|
|
|
return $directory !== '' && $directory !== '.'; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Internal helper method that recursively deletes empty directories. |
|
213
|
|
|
* |
|
214
|
|
|
* @internal This is an internal method, please do not use it in your code. |
|
215
|
|
|
* @psalm-internal Spiral\Storage\Storage |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $directory |
|
218
|
|
|
* @throws FileOperationException |
|
219
|
|
|
*/ |
|
220
|
|
|
private function deleteEmptyDirectories(string $directory): void |
|
221
|
|
|
{ |
|
222
|
|
|
if (!$this->hasParentDirectory($directory)) { |
|
223
|
|
|
return; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
$fs = $this->getOperator(); |
|
227
|
|
|
|
|
228
|
|
|
try { |
|
229
|
|
|
if (!$this->hasFiles($directory)) { |
|
230
|
|
|
$fs->deleteDirectory($directory); |
|
231
|
|
|
|
|
232
|
|
|
$this->deleteEmptyDirectories($this->getParentDirectory($directory)); |
|
233
|
|
|
} |
|
234
|
|
|
} catch (FilesystemException $e) { |
|
235
|
|
|
throw new FileOperationException($e->getMessage(), (int)$e->getCode(), $e); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Internal helper method that returns bool {@see true} if directory |
|
241
|
|
|
* not empty. |
|
242
|
|
|
* |
|
243
|
|
|
* Note: Be careful, this method can be quite slow as it asks for a |
|
244
|
|
|
* list of files from filesystem. |
|
245
|
|
|
* |
|
246
|
|
|
* @internal This is an internal method, please do not use it in your code. |
|
247
|
|
|
* @psalm-internal Spiral\Storage\Storage |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $directory |
|
250
|
|
|
* @return bool |
|
251
|
|
|
* @throws FilesystemException |
|
252
|
|
|
*/ |
|
253
|
|
|
private function hasFiles(string $directory): bool |
|
254
|
|
|
{ |
|
255
|
|
|
$fs = $this->getOperator(); |
|
256
|
|
|
|
|
257
|
|
|
foreach ($fs->listContents($directory) as $_) { |
|
258
|
|
|
return true; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Internal helper method that returns bool {@see true} if passed argument |
|
266
|
|
|
* can be converted to string. |
|
267
|
|
|
* |
|
268
|
|
|
* @param string|\Stringable $value |
|
269
|
|
|
* @return bool |
|
270
|
|
|
*/ |
|
271
|
|
|
private function isStringable($value): bool |
|
272
|
|
|
{ |
|
273
|
|
|
if (\is_string($value)) { |
|
274
|
|
|
return true; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
if (!\is_object($value)) { |
|
278
|
|
|
return false; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
if (\PHP_VERSION_ID >= 80000) { |
|
282
|
|
|
return $value instanceof \Stringable; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return \method_exists($value, '__toString'); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|