Filesystem.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace thans\filesystem;
  3. use think\App;
  4. use think\filesystem\Driver;
  5. use think\helper\Arr;
  6. class Filesystem
  7. {
  8. protected $disks;
  9. /** @var App */
  10. protected $app;
  11. public function __construct(App $app)
  12. {
  13. $this->app = $app;
  14. }
  15. /**
  16. * @param null|string $name
  17. *
  18. * @return Driver
  19. */
  20. public function disk(string $name = null): Driver
  21. {
  22. $name = $name ?: $this->app->config->get('filesystem.default');
  23. if (!isset($this->disks[$name])) {
  24. $config = $this->app->config->get("filesystem.disks.{$name}");
  25. $this->disks[$name] = App::factory($config['type'], '\\thans\\filesystem\\driver\\', $config);
  26. }
  27. return $this->disks[$name];
  28. }
  29. /**
  30. * 获取缓存配置.
  31. *
  32. * @param null|string $name 名称
  33. * @param mixed $default 默认值
  34. *
  35. * @return mixed
  36. */
  37. public function getConfig(string $name = null, $default = null)
  38. {
  39. if (!is_null($name)) {
  40. return $this->app->config->get('filesystem.'.$name, $default);
  41. }
  42. return $this->app->config->get('filesystem');
  43. }
  44. /**
  45. * 获取磁盘配置.
  46. *
  47. * @param string $disk
  48. * @param null $name
  49. * @param null $default
  50. *
  51. * @return array
  52. */
  53. public function getDiskConfig($disk, $name = null, $default = null)
  54. {
  55. if ($config = $this->getConfig("disks.{$disk}")) {
  56. return Arr::get($config, $name, $default);
  57. }
  58. throw new InvalidArgumentException("Disk [$disk] not found.");
  59. }
  60. public function __call($method, $parameters)
  61. {
  62. return $this->disk()->$method(...$parameters);
  63. }
  64. }