Completed
Push — master ( 66334b...800dec )
by Xu
05:45
created

Adapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 3 1
A getRootUrl() 0 6 2
A init() 0 14 3
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;
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
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
     * @var string|null The adapter URL
48
     */
49
    public $url;
50
51
    /**
52
     * 初始化适配器
53
     * @throws InvalidConfigException
54
     */
55
    public function init()
56
    {
57
        parent::init();
58
        $adapter = $this->prepareAdapter();
59
        if ($this->cache !== null) {
60
            /* @var Cache $cache */
61
            $cache = Instance::ensure($this->cache, Cache::class);
62
            if (!$cache instanceof Cache) {
0 ignored issues
show
introduced by
The condition ! $cache instanceof yii\caching\Cache can never be true.
Loading history...
63
                throw new InvalidConfigException('The "cache" property must be an instance of \yii\caching\Cache subclasses.');
64
            }
65
            $adapter = new CachedAdapter($adapter, new YiiCache($cache, $this->cacheKey, $this->cacheDuration));
66
        }
67
        // And use that to create the file system
68
        $this->adapter = new \League\Flysystem\Filesystem($adapter);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    abstract public static function displayName();
75
76
    /**
77
     * 准备适配器
78
     * @return AdapterInterface
79
     */
80
    abstract protected function prepareAdapter();
81
82
    /**
83
     * Returns the URL to the source, if it’s accessible via HTTP traffic.
84
     *
85
     * @return string|false The root URL, or `false` if there isn’t one
86
     */
87
    public function getRootUrl()
88
    {
89
        if ($this->url !== null) {
90
            return rtrim(Yii::getAlias($this->url), '/') . '/';
91
        }
92
        return false;
93
    }
94
95
    /**
96
     * 魔术方法,执行适配器方法
97
     * @param string $method
98
     * @param array $parameters
99
     * @return mixed
100
     */
101
    public function __call($method, $parameters)
102
    {
103
        return call_user_func_array([$this->adapter, $method], $parameters);
104
    }
105
}