Passed
Push — 6.0 ( 03c236...d3acc3 )
by yun
13:52
created

Driver::createFilesystem()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 3
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\filesystem;
14
15
use League\Flysystem\AdapterInterface;
16
use League\Flysystem\Adapter\AbstractAdapter;
17
use League\Flysystem\Cached\CachedAdapter;
18
use League\Flysystem\Cached\Storage\Memory as MemoryStore;
19
use League\Flysystem\Filesystem;
20
use RuntimeException;
21
use think\Cache;
22
use think\File;
23
24
/**
25
 * Class Driver
26
 * @package think\filesystem
27
 * @mixin Filesystem
28
 */
29
abstract class Driver
30
{
31
32
    /** @var Cache */
33
    protected $cache;
34
35
    /** @var Filesystem */
36
    protected $filesystem;
37
38
    /**
39
     * 配置参数
40
     * @var array
41
     */
42
    protected $config = [];
43 9
44
    public function __construct(Cache $cache, array $config)
45 9
    {
46 9
        $this->cache  = $cache;
47
        $this->config = array_merge($this->config, $config);
48 9
49 9
        $adapter          = $this->createAdapter();
50 9
        $this->filesystem = $this->createFilesystem($adapter);
51
    }
52 6
53
    protected function createCacheStore($config)
54 6
    {
55 6
        if (true === $config) {
56
            return new MemoryStore;
57
        }
58 3
59 3
        return new CacheStore(
60 3
            $this->cache->store($config['store']),
61 3
            $config['prefix'] ?? 'flysystem',
62
            $config['expire'] ?? null
63
        );
64
    }
65
66
    abstract protected function createAdapter(): AdapterInterface;
67 9
68
    protected function createFilesystem(AdapterInterface $adapter): Filesystem
69 9
    {
70 6
        if (!empty($this->config['cache'])) {
71
            $adapter = new CachedAdapter($adapter, $this->createCacheStore($this->config['cache']));
72
        }
73 9
74
        $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url']));
75 9
76
        return new Filesystem($adapter, count($config) > 0 ? $config : null);
77
    }
78
79
    /**
80
     * 获取文件完整路径
81
     * @param string $path
82
     * @return string
83
     */
84
    public function path(string $path): string
85
    {
86
        $adapter = $this->filesystem->getAdapter();
87
88
        if ($adapter instanceof AbstractAdapter) {
89
            return $adapter->applyPathPrefix($path);
90
        }
91
92
        return $path;
93
    }
94
95
    protected function concatPathToUrl($url, $path)
96
    {
97
        return rtrim($url, '/') . '/' . ltrim($path, '/');
98
    }
99
100
    public function url(string $path): string
101
    {
102 3
        throw new RuntimeException('This driver does not support retrieving URLs.');
103
    }
104 3
105
    /**
106
     * 保存文件
107
     * @param string               $path    路径
108
     * @param File                 $file    文件
109
     * @param null|string|\Closure $rule    文件名规则
110
     * @param array                $options 参数
111
     * @return bool|string
112
     */
113
    public function putFile(string $path, File $file, $rule = null, array $options = [])
114
    {
115 3
        return $this->putFileAs($path, $file, $file->hashName($rule), $options);
116
    }
117 3
118 3
    /**
119
     * 指定文件名保存文件
120 3
     * @param string $path    路径
121
     * @param File   $file    文件
122 3
     * @param string $name    文件名
123 3
     * @param array  $options 参数
124
     * @return bool|string
125
     */
126 3
    public function putFileAs(string $path, File $file, string $name, array $options = [])
127
    {
128
        $stream = fopen($file->getRealPath(), 'r');
129 6
        $path   = trim($path . '/' . $name, '/');
130
131 6
        $result = $this->putStream($path, $stream, $options);
0 ignored issues
show
Bug introduced by
The method putStream() does not exist on think\filesystem\Driver. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        /** @scrutinizer ignore-call */ 
132
        $result = $this->putStream($path, $stream, $options);
Loading history...
132
133
        if (is_resource($stream)) {
134
            fclose($stream);
135
        }
136
137
        return $result ? $path : false;
138
    }
139
140
    public function __call($method, $parameters)
141
    {
142
        return $this->filesystem->$method(...$parameters);
143
    }
144
}
145