FileSystem.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * FileSystem.php UTF-8
  4. *
  5. *
  6. * @date : 2019/8/9 9:34
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace FileSystem;
  13. use Xxtime\Flysystem\Aliyun\OssAdapter;
  14. use League\Flysystem\Filesystem as LeagueFilesystem;
  15. /**
  16. * 文件系统工厂类
  17. * Class FileSystem
  18. * Usage
  19. *
  20. * // Write Files
  21. * $filesystem->write('path/to/file.txt', 'contents');
  22. * // get RAW data from aliYun OSS
  23. * $raw = $aliyun->supports->getFlashData();
  24. *
  25. * // Write Use writeStream
  26. * $stream = fopen('local/path/to/file.txt', 'r+');
  27. * $result = $filesystem->writeStream('path/to/file.txt', $stream);
  28. * if (is_resource($stream)) {
  29. * fclose($stream);
  30. * }
  31. *
  32. * // Update Files
  33. * $filesystem->update('path/to/file.txt', 'new contents');
  34. *
  35. * // Check if a file exists
  36. * $exists = $filesystem->has('path/to/file.txt');
  37. *
  38. * // Read Files
  39. * $contents = $filesystem->read('path/to/file.txt');
  40. *
  41. * // Delete Files
  42. * $filesystem->delete('path/to/file.txt');
  43. *
  44. * // Rename Files
  45. * $filesystem->rename('filename.txt', 'newname.txt');
  46. *
  47. * // Copy Files
  48. * $filesystem->copy('filename.txt', 'duplicate.txt');
  49. *
  50. *
  51. * @package FileSystem
  52. */
  53. class FileSystem {
  54. private static $adapter;
  55. /**
  56. * @param array $config
  57. *
  58. * @return LeagueFilesystem
  59. * @throws \Exception
  60. */
  61. public static function create($config = []) {
  62. if (empty($config)) {
  63. $config = config('filesystem');
  64. }
  65. $_default = get_val($config, 'default', 'oss');
  66. try {
  67. switch ($_default) {
  68. case 'oss':
  69. $_driver_config = get_val($config, 'oss', []);
  70. $_adapter = new OssAdapter($_driver_config);
  71. break;
  72. default:
  73. $_driver_config = get_val($config, 'oss', []);
  74. $_adapter = new OssAdapter($_driver_config);
  75. }
  76. self::setAdapter($_adapter);
  77. $filesystem = new LeagueFilesystem($_adapter);
  78. } catch (\Exception $exception) {
  79. throw new $exception;
  80. }
  81. return $filesystem;
  82. }
  83. /**
  84. * @return mixed
  85. */
  86. public static function getAdapter() {
  87. return self::$adapter;
  88. }
  89. /**
  90. * @param mixed $adapter
  91. */
  92. public static function setAdapter($adapter) {
  93. self::$adapter = $adapter;
  94. }
  95. }