|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use diecoding\flysystem\adapter\ZipArchiveAdapter; |
|
6
|
|
|
use diecoding\flysystem\traits\UrlGeneratorComponentTrait; |
|
7
|
|
|
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\InvalidConfigException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Interacting with an ZipArchive filesystem |
|
13
|
|
|
* @see https://flysystem.thephpleague.com/docs/adapter/zip-archive/ |
|
14
|
|
|
* |
|
15
|
|
|
* ```php |
|
16
|
|
|
* 'components' => [ |
|
17
|
|
|
* 'fs' => [ |
|
18
|
|
|
* 'class' => \diecoding\flysystem\ZipArchiveComponent::class, |
|
19
|
|
|
* 'pathToZip' => dirname(dirname(__DIR__)) . '/storage.zip', // or you can use @alias |
|
20
|
|
|
* 'secret' => 'my-secret', // for secure route url |
|
21
|
|
|
* // 'action' => '/site/file', // action route |
|
22
|
|
|
* // 'prefix' => '', // root directory inside zip file |
|
23
|
|
|
* ], |
|
24
|
|
|
* ], |
|
25
|
|
|
* ``` |
|
26
|
|
|
* |
|
27
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
28
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
29
|
|
|
* @copyright Copyright (c) 2023 |
|
30
|
|
|
*/ |
|
31
|
|
|
class ZipArchiveComponent extends AbstractComponent |
|
32
|
|
|
{ |
|
33
|
|
|
use UrlGeneratorComponentTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $pathToZip; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $secret; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
* @throws InvalidConfigException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function init() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->validateProperties([ |
|
52
|
|
|
'pathToZip', |
|
53
|
|
|
'secret', |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$this->initEncrypter($this->secret); |
|
57
|
|
|
|
|
58
|
|
|
parent::init(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return ZipArchiveAdapter |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function initAdapter() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->pathToZip = (string) Yii::getAlias($this->pathToZip); |
|
67
|
|
|
|
|
68
|
|
|
$adapter = new ZipArchiveAdapter( |
|
69
|
|
|
new FilesystemZipArchiveProvider($this->pathToZip), |
|
70
|
|
|
(string) $this->prefix |
|
71
|
|
|
); |
|
72
|
|
|
// for UrlGeneratorAdapterTrait |
|
73
|
|
|
$adapter->component = $this; |
|
74
|
|
|
|
|
75
|
|
|
return $adapter; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|