1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Storage\Storage; |
6
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ExpectedValues; |
8
|
|
|
use Spiral\Storage\FileInterface; |
9
|
|
|
use Spiral\Storage\BucketInterface; |
10
|
|
|
use Spiral\Storage\Visibility; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @mixin WritableInterface |
14
|
|
|
*/ |
15
|
|
|
trait WritableTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@see StorageInterface::bucket()} |
19
|
|
|
*/ |
20
|
|
|
abstract public function bucket(?string $name = null): BucketInterface; |
21
|
|
|
|
22
|
|
|
public function create(string|\Stringable $id, array $config = []): FileInterface |
23
|
|
|
{ |
24
|
|
|
[$name, $pathname] = $this->parseUri($id); |
25
|
|
|
|
26
|
|
|
return $this->bucket($name)->create($pathname, $config); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function write(string|\Stringable $id, mixed $content, array $config = []): FileInterface |
30
|
|
|
{ |
31
|
|
|
[$name, $pathname] = $this->parseUri($id); |
32
|
|
|
|
33
|
|
|
return $this->bucket($name)->write($pathname, $content, $config); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setVisibility( |
37
|
|
|
string|\Stringable $id, |
38
|
|
|
#[ExpectedValues(valuesFromClass: Visibility::class)] |
39
|
|
|
string $visibility, |
40
|
|
|
): FileInterface { |
41
|
|
|
[$name, $pathname] = $this->parseUri($id); |
42
|
|
|
|
43
|
|
|
return $this->bucket($name)->setVisibility($pathname, $visibility); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function copy( |
47
|
|
|
string|\Stringable $source, |
48
|
|
|
string|\Stringable $destination, |
49
|
|
|
array $config = [], |
50
|
|
|
): FileInterface { |
51
|
|
|
[$sourceName, $sourcePathname] = $this->parseUri($source); |
52
|
|
|
[$destName, $destPathname] = $this->parseUri($destination, false); |
53
|
|
|
|
54
|
|
|
$sourceStorage = $this->bucket($sourceName); |
55
|
|
|
$destStorage = $destName ? $this->bucket($destName) : null; |
56
|
|
|
|
57
|
|
|
return $sourceStorage->copy($sourcePathname, $destPathname, $destStorage, $config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function move( |
61
|
|
|
string|\Stringable $source, |
62
|
|
|
string|\Stringable $destination, |
63
|
|
|
array $config = [], |
64
|
|
|
): FileInterface { |
65
|
|
|
[$sourceName, $sourcePathname] = $this->parseUri($source); |
66
|
|
|
[$destName, $destPathname] = $this->parseUri($destination, false); |
67
|
|
|
|
68
|
|
|
$sourceStorage = $this->bucket($sourceName); |
69
|
|
|
$destStorage = $destName ? $this->bucket($destName) : null; |
70
|
|
|
|
71
|
|
|
return $sourceStorage->move($sourcePathname, $destPathname, $destStorage, $config); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function delete(string|\Stringable $id, bool $clean = false): void |
75
|
|
|
{ |
76
|
|
|
[$name, $pathname] = $this->parseUri($id); |
77
|
|
|
|
78
|
|
|
$bucket = $this->bucket($name); |
79
|
|
|
|
80
|
|
|
$bucket->delete($pathname, $clean); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@see Storage::parseUri()} |
85
|
|
|
*/ |
86
|
|
|
abstract protected function parseUri(string|\Stringable $uri, bool $withScheme = true): array; |
87
|
|
|
} |
88
|
|
|
|