1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Simple S3 package. |
4
|
|
|
* |
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3; |
13
|
|
|
|
14
|
|
|
use Aws\ResultInterface; |
15
|
|
|
use Aws\S3\S3Client; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
19
|
|
|
use Matecat\SimpleS3\Components\Cache\CacheInterface; |
20
|
|
|
use Matecat\SimpleS3\Components\Encoders\SafeNameEncoderInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Client |
24
|
|
|
* |
25
|
|
|
* This class is a simple wrapper of Aws\S3\S3Client |
26
|
|
|
* ------------------------------------------------------------------------- |
27
|
|
|
* |
28
|
|
|
* Method list: |
29
|
|
|
* |
30
|
|
|
* @method bool clearBucket(array $input) |
31
|
|
|
* @method bool copyFolder(array $input) |
32
|
|
|
* @method bool copyInBatch(array $input) |
33
|
|
|
* @method bool copyItem(array $input) |
34
|
|
|
* @method bool createBucketIfItDoesNotExist(array $input) |
35
|
|
|
* @method bool createFolder(array $input) |
36
|
|
|
* @method bool deleteBucket(array $input) |
37
|
|
|
* @method bool deleteBucketPolicy(array $input) |
38
|
|
|
* @method bool deleteFolder(array $input) |
39
|
|
|
* @method bool deleteItem(array $input) |
40
|
|
|
* @method bool downloadItem(array $input) |
41
|
|
|
* @method bool enableAcceleration(array $input) |
42
|
|
|
* @method ResultInterface|mixed getBucketLifeCycleConfiguration(array $input) |
43
|
|
|
* @method mixed getBucketPolicy(array $input) |
44
|
|
|
* @method int|mixed getBucketSize(array $input) |
45
|
|
|
* @method null|string getCurrentItemVersion(array $input) |
46
|
|
|
* @method ResultInterface|mixed getItem(array $input) |
47
|
|
|
* @method array|mixed getItemsInABucket(array $input) |
48
|
|
|
* @method array|mixed getItemsInAVersionedBucket(array $input) |
49
|
|
|
* @method mixed|UriInterface getPublicItemLink(array $input) |
50
|
|
|
* @method bool hasBucket(array $input) |
51
|
|
|
* @method bool hasFolder(array $input) |
52
|
|
|
* @method bool hasItem(array $input) |
53
|
|
|
* @method bool isBucketVersioned(array $input) |
54
|
|
|
* @method mixed|UriInterface openItem(array $input) |
55
|
|
|
* @method bool restoreItem(array $input) |
56
|
|
|
* @method bool setBucketLifecycleConfiguration(array $input) |
57
|
|
|
* @method bool setBucketPolicy(array $input) |
58
|
|
|
* @method bool setBucketVersioning(array $input) |
59
|
|
|
* @method bool transfer(array $input) |
60
|
|
|
* @method bool uploadItem(array $input) |
61
|
|
|
* @method bool uploadItemFromBody(array $input) |
62
|
|
|
* |
63
|
|
|
* @package SimpleS3 |
64
|
|
|
*/ |
65
|
|
|
final class Client |
66
|
|
|
{ |
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
private $prefixSeparator = DIRECTORY_SEPARATOR; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var CacheInterface |
74
|
|
|
*/ |
75
|
|
|
private $cache; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var SafeNameEncoderInterface |
79
|
|
|
*/ |
80
|
|
|
private $encoder; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var LoggerInterface |
84
|
|
|
*/ |
85
|
|
|
private $logger; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var S3Client |
89
|
|
|
*/ |
90
|
|
|
private $s3; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var bool |
94
|
|
|
*/ |
95
|
|
|
private $sslVerify = true; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var int |
99
|
|
|
*/ |
100
|
|
|
private $filenameMaxSize; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Client constructor. |
104
|
|
|
* |
105
|
|
|
* @param array $config |
106
|
|
|
*/ |
107
|
12 |
|
public function __construct(array $config) |
108
|
|
|
{ |
109
|
12 |
|
$this->s3 = ClientFactory::create($config); |
110
|
12 |
|
$this->filenameMaxSize = 255; |
111
|
12 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Calls the invoked CommandHandler. |
115
|
|
|
* It checks if the class exists and |
116
|
|
|
* if the passed parameters are valid |
117
|
|
|
* |
118
|
|
|
* @param string $name |
119
|
|
|
* @param mixed $args |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
12 |
|
public function __call($name, $args) |
124
|
|
|
{ |
125
|
12 |
|
$params = isset($args[0]) ? $args[0] : []; |
126
|
|
|
|
127
|
12 |
|
$commandHandler = 'Matecat\\SimpleS3\\Commands\\Handlers\\'.ucfirst($name); |
128
|
|
|
|
129
|
12 |
|
if (false === class_exists($commandHandler)) { |
130
|
|
|
throw new \InvalidArgumentException($commandHandler . ' is not a valid command name. Please refer to README to get the complete command list.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @var CommandHandler $commandHandler */ |
134
|
12 |
|
$commandHandler = new $commandHandler($this); |
135
|
|
|
|
136
|
12 |
|
if ($commandHandler->validateParams($params)) { |
137
|
12 |
|
return $commandHandler->handle($params); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param CacheInterface $cache |
143
|
|
|
*/ |
144
|
9 |
|
public function addCache(CacheInterface $cache) |
145
|
|
|
{ |
146
|
9 |
|
$this->cache = $cache; |
147
|
9 |
|
$this->cache->setPrefixSeparator($this->prefixSeparator); |
148
|
9 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function hasCache() |
154
|
|
|
{ |
155
|
|
|
return null !== $this->cache; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return CacheInterface |
160
|
|
|
*/ |
161
|
|
|
public function getCache() |
162
|
|
|
{ |
163
|
|
|
return $this->cache; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param SafeNameEncoderInterface $encoder |
168
|
|
|
*/ |
169
|
12 |
|
public function addEncoder(SafeNameEncoderInterface $encoder) |
170
|
|
|
{ |
171
|
12 |
|
$this->encoder = $encoder; |
172
|
12 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
2 |
|
public function hasEncoder() |
178
|
|
|
{ |
179
|
2 |
|
return null !== $this->encoder; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return SafeNameEncoderInterface |
184
|
|
|
*/ |
185
|
2 |
|
public function getEncoder() |
186
|
|
|
{ |
187
|
2 |
|
return $this->encoder; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param LoggerInterface $logger |
192
|
|
|
*/ |
193
|
9 |
|
public function addLogger(LoggerInterface $logger) |
194
|
|
|
{ |
195
|
9 |
|
$this->logger = $logger; |
196
|
9 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
12 |
|
public function hasLogger() |
202
|
|
|
{ |
203
|
12 |
|
return null !== $this->logger; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return LoggerInterface |
208
|
|
|
*/ |
209
|
9 |
|
public function getLogger() |
210
|
|
|
{ |
211
|
9 |
|
return $this->logger; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return S3Client |
216
|
|
|
*/ |
217
|
10 |
|
public function getConn() |
218
|
|
|
{ |
219
|
10 |
|
return $this->s3; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Disable SSL verify |
224
|
|
|
*/ |
225
|
|
|
public function disableSslVerify() |
226
|
|
|
{ |
227
|
|
|
$this->sslVerify = false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
2 |
|
public function hasSslVerify() |
234
|
|
|
{ |
235
|
2 |
|
return $this->sslVerify; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @var string |
240
|
|
|
*/ |
241
|
1 |
|
public function setPrefixSeparator($separator) |
242
|
|
|
{ |
243
|
1 |
|
$this->prefixSeparator = $separator; |
244
|
1 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getPrefixSeparator() |
250
|
|
|
{ |
251
|
|
|
return $this->prefixSeparator; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return int |
256
|
|
|
*/ |
257
|
|
|
public function getFilenameMaxSize() |
258
|
|
|
{ |
259
|
|
|
return $this->filenameMaxSize; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param int $filenameMaxSize |
264
|
|
|
*/ |
265
|
|
|
public function setFilenameMaxSize($filenameMaxSize) |
266
|
|
|
{ |
267
|
|
|
$this->filenameMaxSize = $filenameMaxSize; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|