Completed
Push — master ( 5289b1...66334b )
by Xu
06:18
created

Adapter::getRootUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * @var string|null The adapter URL
48
     */
49
    public $url;
50
51
    /**
52
     * @var bool|null Whether the volume has a public URL
53
     */
54
    public $hasUrls;
55
56
    /**
57
     * 初始化适配器
58
     * @throws InvalidConfigException
59
     */
60
    public function init()
61
    {
62
        $adapter = $this->prepareAdapter();
63
        if ($this->cache !== null) {
64
            /* @var Cache $cache */
65
            $cache = Instance::ensure($this->cache, Cache::class);
66
            if (!$cache instanceof Cache) {
0 ignored issues
show
introduced by
The condition ! $cache instanceof yii\caching\Cache can never be true.
Loading history...
67
                throw new InvalidConfigException('The "cache" property must be an instance of \yii\caching\Cache subclasses.');
68
            }
69
            $adapter = new CachedAdapter($adapter, new YiiCache($cache, $this->cacheKey, $this->cacheDuration));
70
        }
71
        // And use that to create the file system
72
        $this->adapter = new \League\Flysystem\Filesystem($adapter);
73
    }
74
75
    /**
76
     * 准备适配器
77
     * @return AdapterInterface
78
     */
79
    abstract protected function prepareAdapter();
80
81
    /**
82
     * Returns the URL to the source, if it’s accessible via HTTP traffic.
83
     *
84
     * @return string|false The root URL, or `false` if there isn’t one
85
     */
86
    public function getRootUrl()
87
    {
88
        return false;
89
    }
90
91
    /**
92
     * 获取文件的Url访问路径
93
     * @param string $path
94
     * @return string
95
     * @throws NotSupportedException
96
     */
97
    public function getUrl($path)
98
    {
99
        if (is_null($this->url)) {
100
            throw new NotSupportedException('"getUrl" is not implemented.');
101
        } else {
102
            return $this->url . '/' . $path;
103
        }
104
    }
105
106
    /**
107
     * 魔术方法,执行适配器方法
108
     * @param string $method
109
     * @param array $parameters
110
     * @return mixed
111
     */
112
    public function __call($method, $parameters)
113
    {
114
        return call_user_func_array([$this->adapter, $method], $parameters);
115
    }
116
}