Completed
Push — master ( 7388ca...992326 )
by Xu
05:55
created

Adapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 3
A __call() 0 3 1
A getUrl() 0 3 1
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\filesystem;
9
10
use yii\base\Component;
11
use yii\base\InvalidConfigException;
12
use yii\base\NotSupportedException;
13
use yii\caching\Cache;
14
use yii\di\Instance;
15
use League\Flysystem\AdapterInterface;
16
use League\Flysystem\Cached\CachedAdapter;
17
18
/**
19
 * Class Filesystem
20
 *
21
 * @author Tongle Xu <[email protected]>
22
 * @since 3.0
23
 */
24
abstract class Adapter extends Component
25
{
26
    /**
27
     * @var \League\Flysystem\FilesystemInterface
28
     */
29
    protected $adapter;
30
31
    /**
32
     * @var string|null
33
     */
34
    public $cache;
35
36
    /**
37
     * @var string
38
     */
39
    public $cacheKey = 'flysystem';
40
41
    /**
42
     * @var integer
43
     */
44
    public $cacheDuration = 3600;
45
46
    /**
47
     * 初始化适配器
48
     * @throws InvalidConfigException
49
     */
50
    public function init()
51
    {
52
        $adapter = $this->prepareAdapter();
53
        if ($this->cache !== null) {
54
            /* @var Cache $cache */
55
            $cache = Instance::ensure($this->cache, Cache::class);
56
            if (!$cache instanceof Cache) {
0 ignored issues
show
introduced by
The condition ! $cache instanceof yii\caching\Cache can never be true.
Loading history...
57
                throw new InvalidConfigException('The "cache" property must be an instance of \yii\caching\Cache subclasses.');
58
            }
59
            $adapter = new CachedAdapter($adapter, new YiiCache($cache, $this->cacheKey, $this->cacheDuration));
60
        }
61
        // And use that to create the file system
62
        $this->adapter = new \League\Flysystem\Filesystem($adapter);
63
    }
64
65
    /**
66
     * 准备适配器
67
     * @return AdapterInterface
68
     */
69
    abstract protected function prepareAdapter();
70
71
    /**
72
     * 获取文件的Url访问路径
73
     * @param string $path
74
     * @throws NotSupportedException
75
     */
76
    public function getUrl($path)
77
    {
78
        throw new NotSupportedException('"getUrl" is not implemented.');
79
    }
80
81
    /**
82
     * 魔术方法,执行适配器方法
83
     * @param string $method
84
     * @param array $parameters
85
     * @return mixed
86
     */
87
    public function __call($method, $parameters)
88
    {
89
        return call_user_func_array([$this->adapter, $method], $parameters);
90
    }
91
}