123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- /**
- * FileSystem.php UTF-8
- *
- *
- * @date : 2019/8/9 9:34
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : luowei <lw@huosdk.com>
- * @version : HuoMp 1.0
- */
- namespace FileSystem;
- use Xxtime\Flysystem\Aliyun\OssAdapter;
- use League\Flysystem\Filesystem as LeagueFilesystem;
- /**
- * 文件系统工厂类
- * Class FileSystem
- * Usage
- *
- * // Write Files
- * $filesystem->write('path/to/file.txt', 'contents');
- * // get RAW data from aliYun OSS
- * $raw = $aliyun->supports->getFlashData();
- *
- * // Write Use writeStream
- * $stream = fopen('local/path/to/file.txt', 'r+');
- * $result = $filesystem->writeStream('path/to/file.txt', $stream);
- * if (is_resource($stream)) {
- * fclose($stream);
- * }
- *
- * // Update Files
- * $filesystem->update('path/to/file.txt', 'new contents');
- *
- * // Check if a file exists
- * $exists = $filesystem->has('path/to/file.txt');
- *
- * // Read Files
- * $contents = $filesystem->read('path/to/file.txt');
- *
- * // Delete Files
- * $filesystem->delete('path/to/file.txt');
- *
- * // Rename Files
- * $filesystem->rename('filename.txt', 'newname.txt');
- *
- * // Copy Files
- * $filesystem->copy('filename.txt', 'duplicate.txt');
- *
- *
- * @package FileSystem
- */
- class FileSystem {
- private static $adapter;
- /**
- * @param array $config
- *
- * @return LeagueFilesystem
- * @throws \Exception
- */
- public static function create($config = []) {
- if (empty($config)) {
- $config = config('filesystem');
- }
- $_default = get_val($config, 'default', 'oss');
- try {
- switch ($_default) {
- case 'oss':
- $_driver_config = get_val($config, 'oss', []);
- $_adapter = new OssAdapter($_driver_config);
- break;
- default:
- $_driver_config = get_val($config, 'oss', []);
- $_adapter = new OssAdapter($_driver_config);
- }
- self::setAdapter($_adapter);
- $filesystem = new LeagueFilesystem($_adapter);
- } catch (\Exception $exception) {
- throw new $exception;
- }
- return $filesystem;
- }
- /**
- * @return mixed
- */
- public static function getAdapter() {
- return self::$adapter;
- }
- /**
- * @param mixed $adapter
- */
- public static function setAdapter($adapter) {
- self::$adapter = $adapter;
- }
- }
|