| Total Complexity | 11 |
| Total Lines | 85 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | abstract class Driver |
||
| 18 | { |
||
| 19 | |||
| 20 | /** @var App */ |
||
| 21 | protected $app; |
||
| 22 | |||
| 23 | /** @var Filesystem */ |
||
| 24 | protected $filesystem; |
||
| 25 | |||
| 26 | protected $config = []; |
||
| 27 | |||
| 28 | 3 | public function __construct(App $app, $config) |
|
| 29 | { |
||
| 30 | 3 | $this->app = $app; |
|
| 31 | 3 | $this->config = array_merge($this->config, $config); |
|
| 32 | |||
| 33 | 3 | $adapter = $this->createAdapter(); |
|
| 34 | 3 | $this->filesystem = $this->createFilesystem($adapter); |
|
| 35 | 3 | } |
|
| 36 | |||
| 37 | 2 | protected function createCacheStore($config) |
|
| 47 | ); |
||
| 48 | } |
||
| 49 | |||
| 50 | abstract protected function createAdapter(): AdapterInterface; |
||
| 51 | |||
| 52 | 3 | protected function createFilesystem(AdapterInterface $adapter) |
|
| 53 | { |
||
| 54 | 3 | if (!empty($this->config['cache'])) { |
|
| 55 | 2 | $adapter = new CachedAdapter($adapter, $this->createCacheStore($this->config['cache'])); |
|
| 56 | } |
||
| 57 | |||
| 58 | 3 | $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url'])); |
|
| 59 | |||
| 60 | 3 | return new Filesystem($adapter, count($config) > 0 ? $config : null); |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * 保存文件 |
||
| 65 | * @param string $path |
||
|
1 ignored issue
–
show
|
|||
| 66 | * @param File $file |
||
|
1 ignored issue
–
show
|
|||
| 67 | * @param null|string|\Closure $rule |
||
|
1 ignored issue
–
show
|
|||
| 68 | * @param array $options |
||
|
1 ignored issue
–
show
|
|||
| 69 | * @return bool|string |
||
| 70 | */ |
||
| 71 | 1 | public function putFile($path, $file, $rule = null, $options = []) |
|
| 72 | { |
||
| 73 | 1 | return $this->putFileAs($path, $file, $file->hashName($rule), $options); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 指定文件名保存文件 |
||
| 78 | * @param string $path |
||
|
1 ignored issue
–
show
|
|||
| 79 | * @param File $file |
||
|
1 ignored issue
–
show
|
|||
| 80 | * @param string $name |
||
|
1 ignored issue
–
show
|
|||
| 81 | * @param array $options |
||
|
1 ignored issue
–
show
|
|||
| 82 | * @return bool|string |
||
| 83 | */ |
||
| 84 | 1 | public function putFileAs($path, $file, $name, $options = []) |
|
| 85 | { |
||
| 86 | 1 | $stream = fopen($file->getRealPath(), 'r'); |
|
| 87 | |||
| 88 | 1 | $result = $this->putStream( |
|
| 89 | 1 | $path = trim($path . '/' . $name, '/'), $stream, $options |
|
| 90 | ); |
||
| 91 | |||
| 92 | 1 | if (is_resource($stream)) { |
|
| 93 | 1 | fclose($stream); |
|
| 94 | } |
||
| 95 | |||
| 96 | 1 | return $result ? $path : false; |
|
| 97 | } |
||
| 98 | |||
| 99 | 2 | public function __call($method, $parameters) |
|
| 102 | } |
||
| 103 | } |
||
| 104 |