-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemManager.php
More file actions
364 lines (302 loc) · 9.74 KB
/
FilesystemManager.php
File metadata and controls
364 lines (302 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace BlitzPHP\Filesystem;
use Aws\S3\S3Client;
use BlitzPHP\Contracts\Filesystem\FilesystemInterface;
use BlitzPHP\Filesystem\Adapters\AwsS3V3Adapter;
use BlitzPHP\Filesystem\Adapters\FilesystemAdapter;
use BlitzPHP\Utilities\Helpers;
use BlitzPHP\Utilities\Iterable\Arr;
use Closure;
use InvalidArgumentException;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter as S3Adapter;
use League\Flysystem\AwsS3V3\PortableVisibilityConverter as AwsS3PortableVisibilityConverter;
use League\Flysystem\Filesystem as Flysystem;
use League\Flysystem\FilesystemAdapter as FlysystemAdapter;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\Visibility;
/**
* @credit <a href="http://laravel.com/">Laravel - Illuminate\Filesystem\FilesystemManager</a>
*/
class FilesystemManager
{
/**
* The array of resolved filesystem drivers.
*
* @var list<FilesystemInterface>
*/
protected array $disks = [];
/**
* The registered custom driver creators.
*/
protected array $customCreators = [];
/**
* Create a new filesystem manager instance.
*/
public function __construct(protected array $config)
{
}
/**
* Get a filesystem instance.
*/
public function drive(?string $name = null): FilesystemInterface
{
return $this->disk($name);
}
/**
* Get a filesystem instance.
*/
public function disk(?string $name = null): FilesystemInterface
{
$name = $name ?: $this->getDefaultDriver();
return $this->disks[$name] = $this->get($name);
}
/**
* Get a default cloud filesystem instance.
*/
public function cloud(): FilesystemInterface
{
$name = $this->getDefaultCloudDriver();
return $this->disks[$name] = $this->get($name);
}
/**
* Build an on-demand disk.
*/
public function build(array|string $config): FilesystemInterface
{
return $this->resolve('ondemand', is_array($config) ? $config : [
'driver' => 'local',
'root' => $config,
]);
}
/**
* Attempt to get the disk from the local cache.
*/
protected function get(string $name): FilesystemInterface
{
return $this->disks[$name] ?? $this->resolve($name);
}
/**
* Resolve the given disk.
*
* @throws InvalidArgumentException
*/
protected function resolve(string $name, ?array $config = null): FilesystemInterface
{
$config ??= $this->getConfig($name);
if (empty($config['driver'])) {
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver.");
}
$name = $config['driver'];
if (isset($this->customCreators[$name])) {
return $this->callCustomCreator($config);
}
$driverMethod = 'create' . ucfirst($name) . 'Driver';
if (! method_exists($this, $driverMethod)) {
throw new InvalidArgumentException("Driver [{$name}] is not supported.");
}
return $this->{$driverMethod}($config);
}
/**
* Call a custom driver creator.
*/
protected function callCustomCreator(array $config): FilesystemInterface
{
return $this->customCreators[$config['driver']]($this->config, $config);
}
/**
* Create an instance of the local driver.
*/
public function createLocalDriver(array $config): FilesystemInterface
{
$visibility = PortableVisibilityConverter::fromArray(
$config['permissions'] ?? [],
$config['directory_visibility'] ?? $config['visibility'] ?? Visibility::PRIVATE
);
$links = ($config['links'] ?? null) === 'skip'
? LocalAdapter::SKIP_LINKS
: LocalAdapter::DISALLOW_LINKS;
$adapter = new LocalAdapter(
$config['root'],
$visibility,
$config['lock'] ?? LOCK_EX,
$links
);
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
/**
* Create an instance of the ftp driver.
*/
public function createFtpDriver(array $config): FilesystemInterface
{
if (! isset($config['root'])) {
$config['root'] = '';
}
$adapter = new FtpAdapter(FtpConnectionOptions::fromArray($config));
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
/**
* Create an instance of the sftp driver.
*/
public function createSftpDriver(array $config): FilesystemInterface
{
$provider = SftpConnectionProvider::fromArray($config);
$root = $config['root'] ?? '/';
$visibility = PortableVisibilityConverter::fromArray(
$config['permissions'] ?? []
);
$adapter = new SftpAdapter($provider, $root, $visibility);
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
/**
* Create an instance of the Amazon S3 driver.
*/
public function createS3Driver(array $config)
{
$s3Config = $this->formatS3Config($config);
$root = (string) ($s3Config['root'] ?? '');
$visibility = new AwsS3PortableVisibilityConverter(
$config['visibility'] ?? Visibility::PUBLIC
);
$streamReads = $s3Config['stream_reads'] ?? false;
$client = new S3Client($s3Config);
$adapter = new S3Adapter($client, $s3Config['bucket'], $root, $visibility, null, $config['options'] ?? [], $streamReads);
return new AwsS3V3Adapter(
$this->createFlysystem($adapter, $config),
$adapter,
$s3Config,
$client
);
}
/**
* Format the given S3 configuration with the default options.
*/
protected function formatS3Config(array $config): array
{
$config += ['version' => 'latest'];
if (! empty($config['key']) && ! empty($config['secret'])) {
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
}
return Arr::except($config, ['token']);
}
/**
* Create a scoped driver.
*/
public function createScopedDriver(array $config): FilesystemInterface
{
if (empty($config['disk'])) {
throw new InvalidArgumentException('Scoped disk is missing "disk" configuration option.');
}
if (empty($config['prefix'])) {
throw new InvalidArgumentException('Scoped disk is missing "prefix" configuration option.');
}
return $this->build(Helpers::tap(
$this->getConfig($config['disk']),
static fn (&$parent) => $parent['prefix'] = $config['prefix']
));
}
/**
* Create a Flysystem instance with the given adapter.
*/
protected function createFlysystem(FlysystemAdapter $adapter, array $config): FilesystemOperator
{
if ($config['read-only'] ?? false === true) {
$adapter = new ReadOnlyFilesystemAdapter($adapter);
}
if (! empty($config['prefix'])) {
$adapter = new PathPrefixedAdapter($adapter, $config['prefix']);
}
return new Flysystem($adapter, Arr::only($config, [
'directory_visibility',
'disable_asserts',
'temporary_url',
'url',
'visibility',
]));
}
/**
* Set the given disk instance.
*/
public function set(string $name, FilesystemInterface $disk): self
{
$this->disks[$name] = $disk;
return $this;
}
/**
* Get the filesystem connection configuration.
*/
protected function getConfig(string $name): array
{
return $this->config['disks'][$name] ?: [];
}
/**
* Get the default driver name.
*/
public function getDefaultDriver(): string
{
return $this->config['default'];
}
/**
* Get the default cloud driver name.
*/
public function getDefaultCloudDriver(): string
{
return $this->config['cloud'] ?? 's3';
}
/**
* Unset the given disk instances.
*/
public function forgetDisk(array|string $disk): self
{
foreach ((array) $disk as $diskName) {
unset($this->disks[$diskName]);
}
return $this;
}
/**
* Disconnect the given disk and remove from local cache.
*/
public function purge(?string $name = null): void
{
$name ??= $this->getDefaultDriver();
unset($this->disks[$name]);
}
/**
* Register a custom driver creator Closure.
*/
public function extend(string $driver, Closure $callback): self
{
$this->customCreators[$driver] = $callback;
return $this;
}
/**
* Set the configurations used by the manager.
*/
public function setConfig(array $config): self
{
$this->config = $config;
return $this;
}
/**
* Dynamically call the default driver instance.
*/
public function __call(string $method, array $parameters = []): mixed
{
return $this->disk()->{$method}(...$parameters);
}
}