Driver.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\filesystem;
  13. use League\Flysystem\AdapterInterface;
  14. use League\Flysystem\Adapter\AbstractAdapter;
  15. use League\Flysystem\Cached\CachedAdapter;
  16. use League\Flysystem\Cached\Storage\Memory as MemoryStore;
  17. use League\Flysystem\Filesystem;
  18. use think\Cache;
  19. use think\File;
  20. /**
  21. * Class Driver
  22. * @package think\filesystem
  23. * @mixin Filesystem
  24. */
  25. abstract class Driver
  26. {
  27. /** @var Cache */
  28. protected $cache;
  29. /** @var Filesystem */
  30. protected $filesystem;
  31. /**
  32. * 配置参数
  33. * @var array
  34. */
  35. protected $config = [];
  36. public function __construct(Cache $cache, array $config)
  37. {
  38. $this->cache = $cache;
  39. $this->config = array_merge($this->config, $config);
  40. $adapter = $this->createAdapter();
  41. $this->filesystem = $this->createFilesystem($adapter);
  42. }
  43. protected function createCacheStore($config)
  44. {
  45. if (true === $config) {
  46. return new MemoryStore;
  47. }
  48. return new CacheStore(
  49. $this->cache->store($config['store']),
  50. $config['prefix'] ?? 'flysystem',
  51. $config['expire'] ?? null
  52. );
  53. }
  54. abstract protected function createAdapter(): AdapterInterface;
  55. protected function createFilesystem(AdapterInterface $adapter): Filesystem
  56. {
  57. if (!empty($this->config['cache'])) {
  58. $adapter = new CachedAdapter($adapter, $this->createCacheStore($this->config['cache']));
  59. }
  60. $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url']));
  61. return new Filesystem($adapter, count($config) > 0 ? $config : null);
  62. }
  63. /**
  64. * 获取文件完整路径
  65. * @param string $path
  66. * @return string
  67. */
  68. public function path(string $path): string
  69. {
  70. $adapter = $this->filesystem->getAdapter();
  71. if ($adapter instanceof AbstractAdapter) {
  72. return $adapter->applyPathPrefix($path);
  73. }
  74. return $path;
  75. }
  76. /**
  77. * 保存文件
  78. * @param string $path 路径
  79. * @param File $file 文件
  80. * @param null|string|\Closure $rule 文件名规则
  81. * @param array $options 参数
  82. * @return bool|string
  83. */
  84. public function putFile(string $path, File $file, $rule = null, array $options = [])
  85. {
  86. return $this->putFileAs($path, $file, $file->hashName($rule), $options);
  87. }
  88. /**
  89. * 指定文件名保存文件
  90. * @param string $path 路径
  91. * @param File $file 文件
  92. * @param string $name 文件名
  93. * @param array $options 参数
  94. * @return bool|string
  95. */
  96. public function putFileAs(string $path, File $file, string $name, array $options = [])
  97. {
  98. $stream = fopen($file->getRealPath(), 'r');
  99. $path = trim($path . '/' . $name, '/');
  100. $result = $this->putStream($path, $stream, $options);
  101. if (is_resource($stream)) {
  102. fclose($stream);
  103. }
  104. return $result ? $path : false;
  105. }
  106. public function __call($method, $parameters)
  107. {
  108. return $this->filesystem->$method(...$parameters);
  109. }
  110. }