|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use diecoding\flysystem\adapter\LocalFilesystemAdapter; |
|
6
|
|
|
use diecoding\flysystem\traits\UrlGeneratorComponentTrait; |
|
7
|
|
|
use League\Flysystem\PathPrefixing\PathPrefixedAdapter; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\InvalidConfigException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Interacting with the Local filesystem |
|
13
|
|
|
* @see https://flysystem.thephpleague.com/docs/adapter/local/ |
|
14
|
|
|
* |
|
15
|
|
|
* ```php |
|
16
|
|
|
* 'components' => [ |
|
17
|
|
|
* 'fs' => [ |
|
18
|
|
|
* 'class' => \diecoding\flysystem\LocalComponent::class, |
|
19
|
|
|
* 'path' => dirname(dirname(__DIR__)) . '/storage', // or you can use @alias |
|
20
|
|
|
* 'secret' => 'my-secret', // for secure route url |
|
21
|
|
|
* // 'action' => '/site/file', // action route |
|
22
|
|
|
* // 'prefix' => '', |
|
23
|
|
|
* ], |
|
24
|
|
|
* ], |
|
25
|
|
|
* ``` |
|
26
|
|
|
* |
|
27
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
28
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
29
|
|
|
* @copyright Copyright (c) 2023 |
|
30
|
|
|
*/ |
|
31
|
|
|
class LocalComponent extends AbstractComponent |
|
32
|
|
|
{ |
|
33
|
|
|
use UrlGeneratorComponentTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $path; |
|
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
|
|
|
'path', |
|
53
|
|
|
'secret', |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$this->initEncrypter($this->secret); |
|
57
|
|
|
|
|
58
|
|
|
parent::init(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return LocalFilesystemAdapter|PathPrefixedAdapter |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function initAdapter() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->path = (string) Yii::getAlias($this->path); |
|
67
|
|
|
|
|
68
|
|
|
$adapter = new LocalFilesystemAdapter($this->path); |
|
69
|
|
|
// for UrlGeneratorAdapterTrait |
|
70
|
|
|
$adapter->component = $this; |
|
71
|
|
|
|
|
72
|
|
|
if ($this->prefix) { |
|
73
|
|
|
$adapter = new PathPrefixedAdapter($adapter, $this->prefix); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $adapter; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|