Driver.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\filesystem;
  13. use League\Flysystem\Filesystem;
  14. use League\Flysystem\FilesystemAdapter;
  15. use League\Flysystem\UnableToSetVisibility;
  16. use League\Flysystem\UnableToWriteFile;
  17. use RuntimeException;
  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. abstract protected function createAdapter(): FilesystemAdapter;
  44. protected function createFilesystem(FilesystemAdapter $adapter): Filesystem
  45. {
  46. $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url']));
  47. return new Filesystem($adapter, $config);
  48. }
  49. /**
  50. * 获取文件完整路径
  51. * @param string $path
  52. * @return string
  53. */
  54. public function path(string $path): string
  55. {
  56. return $path;
  57. }
  58. protected function concatPathToUrl($url, $path)
  59. {
  60. return rtrim($url, '/') . '/' . ltrim($path, '/');
  61. }
  62. public function url(string $path): string
  63. {
  64. throw new RuntimeException('This driver does not support retrieving URLs.');
  65. }
  66. /**
  67. * 保存文件
  68. * @param string $path 路径
  69. * @param File $file 文件
  70. * @param null|string|\Closure $rule 文件名规则
  71. * @param array $options 参数
  72. * @return bool|string
  73. */
  74. public function putFile(string $path, File $file, $rule = null, array $options = [])
  75. {
  76. return $this->putFileAs($path, $file, $file->hashName($rule), $options);
  77. }
  78. /**
  79. * 指定文件名保存文件
  80. * @param string $path 路径
  81. * @param File $file 文件
  82. * @param string $name 文件名
  83. * @param array $options 参数
  84. * @return bool|string
  85. */
  86. public function putFileAs(string $path, File $file, string $name, array $options = [])
  87. {
  88. $stream = fopen($file->getRealPath(), 'r');
  89. $path = trim($path . '/' . $name, '/');
  90. $result = $this->put($path, $stream, $options);
  91. if (is_resource($stream)) {
  92. fclose($stream);
  93. }
  94. return $result ? $path : false;
  95. }
  96. protected function put(string $path, $contents, array $options = [])
  97. {
  98. try {
  99. $this->writeStream($path, $contents, $options);
  100. } catch (UnableToWriteFile|UnableToSetVisibility $e) {
  101. return false;
  102. }
  103. return true;
  104. }
  105. public function __call($method, $parameters)
  106. {
  107. return $this->filesystem->$method(...$parameters);
  108. }
  109. }