Repository navigation
Expand file tree
/
Copy pathStorage.php
More file actions
791 lines (701 loc) · 20.8 KB
/
Copy pathStorage.php
File metadata and controls
791 lines (701 loc) · 20.8 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<?php
/**
* @author: Jens Giessmann <jg@handcode.de>
*
* @copyright: 2025, Jens Giessmann
*/
namespace handcode\flysystem\components;
use handcode\flysystem\adapters\DeleteOnlyEmptyDirAdapter;
use handcode\flysystem\adapters\ParentMustExistAdapter;
use handcode\flysystem\exceptions\InvalidStorageAdapterException;
use handcode\flysystem\exceptions\InvalidStorageConfigException;
use handcode\flysystem\exceptions\InvalidStorageOperatorException;
use handcode\flysystem\exceptions\StorageAlreadyMountedException;
use handcode\flysystem\exceptions\UnprocessableStorageException;
use handcode\flysystem\helper\StorageHelper;
use handcode\flysystem\interfaces\StorageComponentInterface;
use handcode\flysystem\traits\StorageUtils;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\DirectoryListing;
use League\Flysystem\FileAttributes;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\FilesystemReader;
use League\Flysystem\PathPrefixer;
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\UnableToWriteFile;
use yii\base\Component;
use yii\base\InvalidConfigException;
/**
* This is the default implementation of the StorageInterface
*/
class Storage extends Component implements StorageComponentInterface
{
use StorageUtils;
/**
* internal var for setListMapper
*
* @var null|callable
*/
public $_listMapper;
/**
* Flysystem FilesystemAdapter that is given as config option
* - DO NOT USE this directly!
* - Use self::getAdapter which ensure you get the final decorated
* adapter instance (prefixed, readOnly,...)
*
* @var FilesystemAdapter
*/
protected $_adapter;
/**
* The final decorated adapter instance (prefixed, readOnly,...)
* returned from getAdapter
*
* @var FilesystemAdapter
*/
protected $_finalAdapater;
/**
* Flysystem FilesystemOperator class that should be used while initializing fs
*
* @var string
*/
protected $_filesystemOperatorClass = Filesystem::class;
/**
* Flysystem filesystem instance of self::filesystemOperatorClass
* with self::adapter
*
* @var FilesystemOperator
*/
protected $_fs;
/**
* Flysystem PathPrefixer instance
*
* @var PathPrefixer
*/
protected $_prefixer;
/**
* class that should be used while initializing prefixer
*
* @var string
*/
protected $_pathPrefixerClass = PathPrefixer::class;
/**
* Mount-Option: optional prefix for the fs instance, if set adapter will be
* decorated with PathPrefixedAdapter
*
* @see https://flysystem.thephpleague.com/docs/adapter/path-prefixing/
*
* @var null|string
*/
protected $_scopePrefix;
/**
* Mount-Option: if true the adapter will be decorated with ReadOnlyFilesystemAdapter
*
* @see https://flysystem.thephpleague.com/docs/adapter/read-only/
*
* @var bool
*/
protected $_readOnly = false;
/**
* if true parent dirs will be created on demand if not exists (default flysystem behaviour)
* if false, adapter will be decorated with EnforceParentDirFilesystemAdapter
*
* @var bool
*/
protected $_createDirOnDemand = false;
/**
* if true dirs can be deleted even if they are not empty
* if false, dir must be empty, adapter will be decorated with EnforceParentDirFilesystemAdapter
*
* @var bool
*/
protected $_deleteDirRecursive = false;
/**
* id true the (prefixed) root dir will be auto created if not exists
*
* @var bool
*/
protected $_ensureRoot = true;
/**
* string that should be used as directory_separator, defaults to DIRECTORY_SEPARATOR
*
* @var string
*/
protected $_separator = DIRECTORY_SEPARATOR;
/**
* internal flag that is checked while setting properties which must be set BEFORE the
* real fs was initialized like readOnly and scopePrefix
*
* @var bool
*/
protected $_mounted = false;
/**
* internal var to hold the decoratorAdapterConfigs set via setDecoratorAdapter
*
* @var
*/
protected $_decoratorAdapter;
/**
* if true mimeType will be populated within listContents map callback
*
* @var bool
*/
protected $_populateMimeType = false;
/**
* @var null|callable
*/
protected $_metaDataCallback;
/**
* @var null|callable
*/
protected $_filterCallback;
public function init()
{
parent::init();
if (!$this->_adapter) {
throw new InvalidStorageAdapterException();
}
if (!$this->_filesystemOperatorClass) {
throw new InvalidStorageOperatorException();
}
}
/**
* {@inheritDoc}
*/
public function setAdapter(callable|FilesystemAdapter $adapter): void
{
if (!$this->canSetMountOption()) {
throw new StorageAlreadyMountedException();
}
if (is_callable($adapter)) {
$adapter = call_user_func($adapter);
}
if ($adapter instanceof FilesystemAdapter) {
$this->_adapter = $adapter;
return;
}
throw new InvalidStorageAdapterException();
}
/**
* {@inheritDoc}
*/
public function setFilesystemOperatorClass(string $className): void
{
if (!$this->canSetMountOption()) {
throw new StorageAlreadyMountedException();
}
if (is_a($className, FilesystemOperator::class, true)) {
$this->_filesystemOperatorClass = $className;
return;
}
throw new InvalidStorageOperatorException();
}
/**
* {@inheritDoc}
*/
public function setScope(callable|string $scopePrefix): void
{
if (!$this->canSetMountOption()) {
throw new StorageAlreadyMountedException();
}
if (is_callable($scopePrefix)) {
$scopePrefix = call_user_func($scopePrefix);
if (!is_string($scopePrefix)) {
throw new InvalidStorageConfigException('return value from callback must be a string');
}
}
$this->_scopePrefix = $scopePrefix;
}
/**
* {@inheritDoc}
*/
public function setReadOnly(bool|callable $flag = false): void
{
if (!$this->canSetMountOption()) {
throw new StorageAlreadyMountedException();
}
if (is_callable($flag)) {
$flag = call_user_func($flag);
if (!is_bool($flag)) {
throw new InvalidStorageConfigException('return value from callback must be a boolean');
}
}
$this->_readOnly = $flag;
}
/**
* {@inheritDoc}
*/
public function setCreateDirOnDemand(bool $flag = false): void
{
if (!$this->canSetMountOption()) {
throw new StorageAlreadyMountedException();
}
$this->_createDirOnDemand = $flag;
}
/**
* {@inheritDoc}
*/
public function setDeleteDirRecursive(bool $flag = false): void
{
$this->_deleteDirRecursive = $flag;
}
/**
* {@inheritDoc}
*/
public function getDeleteDirRecursive(): bool
{
return $this->_deleteDirRecursive;
}
/**
* {@inheritDoc}
*/
public function setEnsureRoot(bool $flag = true): void
{
$this->_ensureRoot = $flag;
}
/**
* {@inheritDoc}
*/
public function setDecoratorAdapter(?array $decorators): void
{
$this->_decoratorAdapter = $decorators;
}
/**
* {@inheritDoc}
*/
public function setPopulateMimeType(bool $flag = false): void
{
$this->_populateMimeType = $flag;
}
/**
* {@inheritDoc}
*/
public function setMetaDataCallback(?callable $callable): void
{
if (is_callable($callable)) {
$this->_metaDataCallback = $callable;
return;
}
$this->error(new InvalidConfigException('listMapper must be a callable'));
}
/**
* {@inheritDoc}
*/
public function setFilterCallback(callable $callable): void
{
$this->_filterCallback = $callable;
}
/**
* optional callback that is used to map StorageAttributes in listContents()
* prototype for the function is:
* `function (StorageAttributes $attributes)`
*
* @throws \Exception
*
* @var null|callable
*/
public function setListMapper(?callable $listMapper): void
{
if (is_callable($listMapper)) {
$this->_listMapper = $listMapper;
return;
}
$this->error(new InvalidConfigException('listMapper must be a callable'));
}
# ---- methods from FilesystemReader -----
/**
* {@inheritDoc}
*/
public function fileExists(string $location): bool
{
return $this->getFs()->fileExists($location);
}
/**
* {@inheritDoc}
*/
public function directoryExists(string $location): bool
{
return $this->getFs()->directoryExists($location);
}
/**
* {@inheritDoc}
*/
public function has(string $location): bool
{
return $this->getFs()->has($location);
}
/**
* {@inheritDoc}
*/
public function read(string $location): string
{
try {
return $this->getFs()->read($location);
} catch (UnableToReadFile $e) {
$this->error($e);
}
return false;
}
/**
* {@inheritDoc}
*/
public function readStream(string $location)
{
try {
return $this->getFs()->readStream($location);
} catch (UnableToReadFile $e) {
$this->error($e);
}
return false;
}
/**
* {@inheritDoc}
*/
public function listContents(string $location, bool $deep = FilesystemReader::LIST_SHALLOW): DirectoryListing
{
$list = $this->getFs()->listContents($location, $deep);
$list = $list->map([$this, 'defaultListMapper']);
if ($this->_listMapper) {
$list = $list->map($this->_listMapper);
}
// if defined set filter callback
if ($this->_filterCallback) {
$list = $list->filter($this->_filterCallback);
}
return new DirectoryListing($list);
}
/**
* default List mapper used as callback for DirectoryListing::map() in self::listContents()
* therefor it has to be public
*
* @param StorageAttributes $storageAttributes
*
* @return DirectoryAttributes|FileAttributes|StorageAttributes
*/
public function defaultListMapper(StorageAttributes $storageAttributes)
{
return $this->internalListMapper($storageAttributes);
}
/**
* {@inheritDoc}
*/
public function lastModified(string $path): int
{
try {
return $this->getFs()->lastModified($path);
} catch (UnableToRetrieveMetadata $e) {
$this->error($e);
}
return false;
}
/**
* {@inheritDoc}
*/
public function fileSize(string $path): int
{
try {
return $this->getFs()->fileSize($path);
} catch (UnableToRetrieveMetadata $e) {
$this->error($e);
}
return false;
}
/**
* {@inheritDoc}
*/
public function mimeType(string $path): string
{
try {
return $this->getFs()->mimeType($path);
} catch (UnableToRetrieveMetadata $e) {
# do not throw error here as the mimeType can be undefined for "unknown" file types
# $this->error($e);
}
return false;
}
/**
* {@inheritDoc}
*/
public function visibility(string $path): string
{
return $this->getFs()->visibility($path);
}
# ---- methods from FilesystemWriter ----
/**
* {@inheritDoc}
*/
public function write(string $location, string $contents, array $config = []): void
{
try {
$this->getFs()->write($location, $contents);
} catch (UnableToWriteFile $e) {
$this->error($e);
}
}
/**
* {@inheritDoc}
*/
public function writeStream(string $location, $contents, array $config = []): void
{
// TODO: allow further classes like StreamWrapper, interfaces,....?
if (is_resource($contents)) {
try {
$this->getFs()->writeStream($location, $contents);
return;
} catch (UnableToDeleteFile $e) {
$this->error($e);
}
}
$this->error(new UnprocessableStorageException('content is not a valid stream'));
}
/**
* {@inheritDoc}
*/
public function delete(string $location): void
{
try {
$this->getFs()->delete($location);
} catch (UnableToDeleteFile $e) {
$this->error($e);
}
}
/**
* {@inheritDoc}
*/
public function deleteDirectory(string $location): void
{
try {
$this->getFs()->deleteDirectory($location);
} catch (UnableToDeleteDirectory $e) {
$this->error($e);
}
}
/**
* {@inheritDoc}
*/
public function createDirectory(string $location, array $config = []): void
{
try {
$this->getFs()->createDirectory($location, $config);
} catch (UnableToCreateDirectory|UnableToSetVisibility $e) {
$this->error($e);
}
}
/**
* {@inheritDoc}
*/
public function move(string $source, string $destination, array $config = []): void
{
try {
$this->getFs()->move($source, $destination, $config);
} catch (UnableToMoveFile $e) {
$this->error($e);
}
}
/**
* {@inheritDoc}
*/
public function copy(string $source, string $destination, array $config = []): void
{
try {
$this->getFs()->copy($source, $destination, $config);
} catch (UnableToCopyFile $e) {
$this->error($e);
}
}
/**
* {@inheritDoc}
*/
public function setVisibility(string $path, string $visibility): void
{
$this->getFs()->setVisibility($path, $visibility);
}
/**
* {@inheritDoc}
*/
public function isReadOnly(): bool
{
return $this->_readOnly;
}
/**
* {@inheritDoc}
*/
public function getCreateDirOnDemand(): bool
{
return $this->_createDirOnDemand;
}
/**
* default implementation used in defaultListMapper
*
* this method set and/or modify the StorageAttributes
*
* @uses StorageHelper::storageAttributesMerge
*
* @param StorageAttributes $storageAttributes
*
* @return DirectoryAttributes|FileAttributes|StorageAttributes
*/
protected function internalListMapper(StorageAttributes $storageAttributes)
{
// init new/changed attributes
$attributes = [];
// add all file specific values to be able to provide them to metaDataCallback if defined
if (StorageAttributes::TYPE_FILE === $storageAttributes->type()) {
// if not set already (e.g. from other Decorator) and _populateMimeType is set, get mimeType from Storage
if (empty($storageAttributes->mimeType() && $this->_populateMimeType)) {
$attributes[StorageAttributes::ATTRIBUTE_MIME_TYPE] = $this->mimeType($storageAttributes->path());
}
}
// this is one of the weird parts, provide a "fresh" instance of the "typed"
// StorageAttributes to the callback and assign the return value from the callback to the new $attributes
// The "advantage" is: all StorageAttributes are readonly inside the callback
if ($this->_metaDataCallback) {
$attributes[StorageAttributes::ATTRIBUTE_EXTRA_METADATA] = call_user_func($this->_metaDataCallback, StorageHelper::storageAttributesMerge($storageAttributes, $attributes));
}
// finally return a new typed StorageAttributes instance
return StorageHelper::storageAttributesMerge($storageAttributes, $attributes);
}
/**
* get the final adapter for this component
* if not yet initialized the final adapter is build with build-in adapters
* according to component properties (readOnly, scope, etc.) and defined
* additional decorator adapters
*
* @return FilesystemAdapter
*
* @throws \Exception
*/
protected function getAdapter()
{
if (!$this->_finalAdapater) {
$this->_finalAdapater = $this->_adapter;
if ($this->_readOnly) {
$this->_finalAdapater = $this->getReadOnlyAdapter($this->_finalAdapater);
}
if ($this->_scopePrefix) {
$this->_finalAdapater = $this->getPathPrefixedAdapter($this->_finalAdapater, $this->_scopePrefix);
}
if (!$this->_createDirOnDemand) {
$this->_finalAdapater = $this->getParentMustExistAdapter($this->_finalAdapater);
}
if (!$this->_deleteDirRecursive) {
$this->_finalAdapater = $this->getDeleteOnlyEmptyDirAdapter($this->_finalAdapater);
}
if (is_array($this->_decoratorAdapter)) {
foreach ($this->_decoratorAdapter as $decorator) {
$this->_finalAdapater = $this->addDecoratorToAdapter($this->_finalAdapater, $decorator);
}
}
}
return $this->_finalAdapater;
}
/**
* @param FilesystemAdapter $adapter
* @param $decorator
*
* @return FilesystemAdapter
*
* @throws \Exception
*/
protected function addDecoratorToAdapter(FilesystemAdapter $adapter, $decorator)
{
if (is_string($decorator) && is_a($decorator, FilesystemAdapter::class, true)) {
return new $decorator($adapter);
}
if (is_callable($decorator)) {
return call_user_func($decorator, $adapter);
}
$this->error(new InvalidStorageAdapterException('decoratorAdapter definition is not valid'));
return $adapter;
}
/**
* Inheritable wrapper to get an adapter decorated with ReadOnlyFilesystemAdapter
*
* @param FilesystemAdapter $adapter
*
* @return ReadOnlyFilesystemAdapter
*/
protected function getReadOnlyAdapter(FilesystemAdapter $adapter): FilesystemAdapter
{
return new ReadOnlyFilesystemAdapter($adapter);
}
/**
* Inheritable wrapper to get an adapter decorated with PathPrefixedAdapter
*
* @param FilesystemAdapter $adapter
* @param string $prefix
*
* @return PathPrefixedAdapter
*/
protected function getPathPrefixedAdapter(FilesystemAdapter $adapter, string $prefix): FilesystemAdapter
{
return new PathPrefixedAdapter($adapter, $prefix);
}
/**
* Inheritable wrapper to get an adapter decorated with ParentMustExistAdapter
*
* @param FilesystemAdapter $adapter
*
* @return ParentMustExistAdapter
*/
protected function getParentMustExistAdapter(FilesystemAdapter $adapter): FilesystemAdapter
{
return new ParentMustExistAdapter($adapter);
}
/**
* Inheritable wrapper to get an adapter decorated with DeleteOnlyEmptyDirAdapter
*
* @param FilesystemAdapter $adapter
*
* @return DeleteOnlyEmptyDirAdapter
*/
protected function getDeleteOnlyEmptyDirAdapter(FilesystemAdapter $adapter): FilesystemAdapter
{
return new DeleteOnlyEmptyDirAdapter($adapter);
}
/**
* @return FilesystemOperator
*/
protected function getFs(): FilesystemOperator
{
if (!$this->_fs) {
$this->_fs = new $this->_filesystemOperatorClass($this->getAdapter());
$this->_mounted = true;
}
return $this->_fs;
}
/**
* @return null|string
*/
protected function getScope(): ?string
{
return $this->_scopePrefix;
}
/**
* @return bool
*/
protected function getEnsureRoot(): bool
{
return $this->_ensureRoot;
}
/**
* check if mount option can be set or if fs is already mounted
*
* @return bool
*/
protected function canSetMountOption()
{
return !$this->_mounted;
}
}