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