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; |
13
|
|
|
|
14
|
|
|
use League\Flysystem\Filesystem; |
15
|
|
|
use League\Flysystem\FilesystemAdapter; |
16
|
|
|
use League\Flysystem\FilesystemOperator; |
17
|
|
|
use Spiral\Distribution\UriResolverInterface; |
18
|
|
|
use Spiral\Storage\Bucket\ReadableTrait; |
19
|
|
|
use Spiral\Storage\Bucket\UriResolvableInterface; |
20
|
|
|
use Spiral\Storage\Bucket\WritableTrait; |
21
|
|
|
|
22
|
|
|
class Bucket implements BucketInterface |
23
|
|
|
{ |
24
|
|
|
use ReadableTrait; |
25
|
|
|
use WritableTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var FilesystemOperator |
29
|
|
|
*/ |
30
|
|
|
protected $fs; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var UriResolverInterface|null |
34
|
|
|
*/ |
35
|
|
|
protected $resolver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string|null |
39
|
|
|
*/ |
40
|
|
|
protected $name; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param FilesystemOperator $fs |
44
|
|
|
* @param string|null $name |
45
|
|
|
* @param UriResolverInterface|null $resolver |
46
|
|
|
*/ |
47
|
|
|
public function __construct(FilesystemOperator $fs, string $name = null, UriResolverInterface $resolver = null) |
48
|
|
|
{ |
49
|
|
|
$this->fs = $fs; |
50
|
|
|
$this->resolver = $resolver; |
51
|
|
|
$this->name = $name; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function getName(): ?string |
58
|
|
|
{ |
59
|
|
|
return $this->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function withName(?string $name): BucketInterface |
66
|
|
|
{ |
67
|
|
|
$self = clone $this; |
68
|
|
|
$self->name = $name; |
69
|
|
|
|
70
|
|
|
return $self; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function getUriResolver(): ?UriResolverInterface |
77
|
|
|
{ |
78
|
|
|
return $this->resolver; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
|
|
public function withUriResolver(?UriResolverInterface $resolver): UriResolvableInterface |
85
|
|
|
{ |
86
|
|
|
$self = clone $this; |
87
|
|
|
$self->resolver = $resolver; |
88
|
|
|
|
89
|
|
|
return $self; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function file(string $pathname): FileInterface |
96
|
|
|
{ |
97
|
|
|
return new File($this, $pathname, $this->resolver); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param FilesystemAdapter $adapter |
102
|
|
|
* @param string|null $name |
103
|
|
|
* @param UriResolverInterface|null $resolver |
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
|
|
public static function fromAdapter( |
107
|
|
|
FilesystemAdapter $adapter, |
108
|
|
|
string $name = null, |
109
|
|
|
UriResolverInterface $resolver = null |
110
|
|
|
): self { |
111
|
|
|
$fs = new Filesystem($adapter); |
112
|
|
|
|
113
|
|
|
return new self($fs, $name, $resolver); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return FilesystemOperator |
118
|
|
|
*/ |
119
|
|
|
protected function getOperator(): FilesystemOperator |
120
|
|
|
{ |
121
|
|
|
return $this->fs; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|