pollen-solutions / filesystem
Pollen Solutions - 文件系统组件 - Flysystem 文件存储库的抽象层。
Requires
- php: ^7.4 || ^8.0
- league/flysystem: ^2.0
- pollen-solutions/http: ^1.0
- pollen-solutions/support: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- roave/security-advisories: dev-latest
Suggests
- league/flysystem-aws-s3-v3: Interacting with Aws S3 through Flysystem and Pollen Solutions Filesystem component.
- pollen-solutions/container: Pollen Solutions - Container Component - PSR-11 ready Dependencies Injection Container.
This package is auto-updated.
Last update: 2024-09-30 01:37:57 UTC
README
Pollen Solutions 文件系统 组件是 Flysystem 文件存储库的抽象层。Flysystem。
安装
composer require pollen-solutions/filesystem
基本用法
use Pollen\Filesystem\StorageManager; $storage = new StorageManager(); try { $listing = $storage->listContents('/'); /** @var \League\Flysystem\StorageAttributes $item */ foreach ($listing as $item) { $path = $item->path(); if ($item instanceof \League\Flysystem\FileAttributes) { var_dump($path); } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) { var_dump($path); } } } catch (\League\Flysystem\FilesystemException $e) { var_dump($e->getMessage()); }
API
Pollen Solutions 文件系统组件的 API 与 Flysystem 的 API 相同,它继承自 Flysystem。有关 Flysystem 在线官方文档的更多信息,请参阅Flysystem 在线官方文档。
存储管理器从其默认磁盘继承了文件系统实例的方法,并提供其他方法以访问其他声明的磁盘实例。
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ // Gets a filesystem instance provided by the storage manager from its name identifier. $storage->disk('my-own-disk'); // Writing files in default filesystem. try { $storage->write($path, $contents, $config); } catch (\League\Flysystem\FilesystemException | \League\Flysystem\UnableToWriteFile $exception) { // handle the error } // Reading files in default filesystem. try { $response = $storage->read($path); } catch (FilesystemException | UnableToReadFile $exception) { // handle the error } // And more ... // @see https://flysystem.thephpleague.com/v2/docs/usage/filesystem-api/
设置自定义默认文件系统
默认磁盘是从 Web 应用的公共目录创建的,可以通过原生的 PHP 函数 getcwd 访问。但风险是您可能会删除、覆盖 ... 对您应用程序功能至关重要的文件。
出于安全考虑,最好配置自己的默认回退磁盘,具有更好的文件权限。
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $defaultDisk = $storage->createLocalFilesystem('/my/secured/fallback/directory/absolute_path'); $storage->setDefaultDisk($defaultDisk);
注册本地磁盘
从本地目录路径
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $storage->registerLocalDisk('my-local-disk', '/my/local/directory/absolute_path');
使用自定义本地文件系统实例
use Pollen\Filesystem\LocalFilesystem; use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $filesystem = new LocalFilesystem($storage->createLocalAdapter('/my/local/directory/absolute_path')); $storage->addDisk('my-local-disk', $filesystem);
使用自定义本地文件系统实例和自定义适配器实例
有关文件系统架构的更多信息,请参阅Flysystem 在线官方文档。
use Pollen\Filesystem\LocalFilesystemAdapter; use Pollen\Filesystem\LocalFilesystem; use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $adapter = new LocalFilesystemAdapter('/my/local/directory/absolute_path'); $filesystem = new LocalFilesystem($adapter); $storage->addDisk('my-local-disk', $localDisk);
扩展本地文件系统 API。
使用 Pollen Solutions 文件系统而不是 Flysystem 的一大优点是,它与 HTTP 请求系统耦合。这允许扩展 Flystem 的 API 以添加一些实用的功能。
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $disk = $storage->registerLocalDisk('my-local-disk', '/my/local/directory/absolute_path'); // Returns the HTTP response to download a file. $disk->downloadResponse('/sample.txt'); // Returns the HTTP binary file response to download a file. $disk->binaryFileResponse('/sample.txt'); // Returns the url of a file or a directory. $disk->getUrl('/sample.txt') // Gets the absolute path of a file or a directory. $disk->getAbsolutePath('/sample.txt'); // Gets the SplFileInfo instance of a file or a directory. $disk->getSplFileInfo('/sample.txt'); // Gets the storage file attributes of a file or a directory. $disk->getStorageAttributes('/sample.txt');
本地图像文件系统
Pollen Solutions 文件系统为本地图像文件提供特定的文件系统和适配器。
注册本地图像文件系统
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $filesystem = $storage->registerLocalImageDisk('my-image-disk', '/my/image/directory/absolute_path'));
创建自定义本地图像文件系统实例
use Pollen\Filesystem\LocalImageFilesystem; use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $filesystem = new LocalImageFilesystem($storage->createLocalAdapter('/my/image/directory/absolute_path')); $storage->addLocalDisk('my-image-disk', $filesystem);
本地图像文件系统扩展 API
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $disk = $storage->registerLocalImageDisk('my-image-disk', '/my/image/directory/absolute_path')); // Gets the HTML render of an image file from its path. $disk->HtmlRender('/sample.jpg'); // Gets the image file url from its path. $disk->getImgSrc('/sample.jpg');
S3 文件系统
use Pollen\Filesystem\StorageManagerInterface; /** @var StorageManagerInterface $storage */ $disk = $storage->registerS3Disk( 's3-disk', [ 'version' => 'latest', 'region' => 'us-east-2', 'endpoint' => '{{ my-s3-endpoint }}', 'credentials' => [ 'key' => '{{ my-s3-user }}', 'secret' => '{{ my-s3-user-password }}', ] ], 'my-aws-s3-bucket-name' ); if ($disk = $storage->disk('s3-disk')) { try { $listing = $disk->listContents('/'; /** @var \League\Flysystem\StorageAttributes $item */ foreach ($listing as $item) { $path = $item->path(); if ($item instanceof FileAttributes) { var_dump($path); } elseif ($item instanceof DirectoryAttributes) { var_dump($path); } } } catch (FilesystemException $e) { var_dump($e->getMessage()); } }
高级用法
使用原生或第三方文件系统和适配器
Flysystem 有各种文件系统和适配器,有时由第三方库提供。
示例
您可能需要使用其中之一,并在 Pollen Solutions 文件系统组件中实现它。
此示例使用 Flysystem FTP 适配器。首先,Flysystem FTP 适配器需要安装新的依赖项。
composer require league/flysystem-ftp:^2.0
use Pollen\Filesystem\Filesystem; use Pollen\Filesystem\StorageManagerInterface; // The internal adapter $adapter = new \League\Flysystem\Ftp\FtpAdapter( // Connection options \League\Flysystem\Ftp\FtpConnectionOptions::fromArray([ 'host' => 'hostname', // required 'root' => '/root/path/', // required 'username' => 'username', // required 'password' => 'password', // required 'port' => 21, 'ssl' => false, 'timeout' => 90, 'utf8' => false, 'passive' => true, 'transferMode' => FTP_BINARY, 'systemType' => null, // 'windows' or 'unix' 'ignorePassiveAddress' => null, // true or false 'timestampsOnUnixListingsEnabled' => false, // true or false 'recurseManually' => true // true ]) ); $filesystem = new Filesystem($adapter); /** @var StorageManagerInterface $storage */ $storage->addDisk('ftp-disk', $filesystem); if ($disk = $storage->disk('ftp-disk')) { try { $listing = $disk->listContents('/'); /** @var \League\Flysystem\StorageAttributes $item */ foreach ($listing as $item) { $path = $item->path(); if ($item instanceof \League\Flysystem\FileAttributes) { var_dump($path); } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) { var_dump($path); } } } catch (\League\Flysystem\FilesystemException $e) { var_dump($e->getMessage()); } }
创建自己的文件系统驱动程序
use Pollen\Filesystem\StorageManagerInterface; use Pollen\Filesystem\FilesystemDriver; use League\Flysystem\Ftp\FtpAdapter; use League\Flysystem\Ftp\FtpConnectionOptions; $driver = new class extends FilesystemDriver { protected ?string $adapterDefinition = FtpAdapter::class /** * @inheritDoc * * {@internal Allows to pass and parse an array of connection options.} */ public function parseArgs(...$args) : array{ if (!isset($args[0]) || !is_array($args)) { throw new RuntimeException('FTP Filesystem first argument could be an array of connection options.'); } $config = $args[0]; $config['host'] = $config['host']?? '127.0.0.1'; $config['root'] = $config['root']?? '/'; $config['username'] = $config['username'] ?? 'root'; $config['password'] = $config['password'] ?? 'root'; $_args[] = FtpConnectionOptions::fromArray($config); return $_args; } }; /** @var StorageManagerInterface $storage */ $storage->registerDriver('ftp', $driver); $storage->registerDisk('ftp-disk', 'ftp', [ 'host' => 'hostname', // required 'root' => '/root/path/', // required 'username' => 'username', // required 'password' => 'password', // required ]); if ($disk = $storage->disk('ftp-disk')) { try { $listing = $disk->listContents('/', true); /** @var \League\Flysystem\StorageAttributes $item */ foreach ($listing as $item) { $path = $item->path(); if ($item instanceof \League\Flysystem\FileAttributes) { var_dump($path); } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) { var_dump($path); } } } catch (\League\Flysystem\FilesystemException $e) { var_dump($e->getMessage()); } }