pluggit / storage
虚拟存储抽象层
Requires
- aws/aws-sdk-php: 3.19.3
- psr/log: ^1.0
- ralouphie/mimey: ^1.0
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: 2.*
- phpspec/phpspec: ^2.5
- phpunit/phpunit: 4.8.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2024-09-23 14:34:39 UTC
README
Storage是一个文件系统抽象层,允许您轻松地将本地文件系统与远程文件系统进行交换。
TLDR;
//one adapter (save data to S3) $s3Adapter = new \Cmp\Storage\Adapter\S3AWSAdapter(); $s3Adapter->put('/tmp/test.txt',"this is a test"); //two adapters with a fallback strategy and decorated with a logger $s3Adapter = new \Cmp\Storage\Adapter\S3AWSAdapter(); $fallBackAdapter = (new StorageBuilder())->addAdapter($s3Adapter) ->addAdapter($s3Adapter) //the order matters with FallBackChainStrategy ->addAdapter($fileSystemAdapter) ->setLogger(new Logger()) ->build(new \Cmp\Storage\Strategy\FallBackChainStrategy()); //it saves data to S3 and if fails save the data to FS $fallBackAdapter->put('/tmp/test.txt',"this is a test"); //one step more fs adapter bind to one folder and strategy to another folder $vfs = new \Cmp\Storage\MountableVirtualStorage($fileSystemStorage); //bind to any path that non match with mountpoint folders $localMountPoint = new \Cmp\Storage\MountPoint('/tmp', $fileSystemAdapter); $publicMountPoint = new \Cmp\Storage\MountPoint('/var/www/app/public', $fallBackAdapter); $vfs->registerMountPoint($localMountPoint); $vfs->registerMountPoint($publicMountPoint); /* //move file from /tmp (FS) to /var/www/app/public (S3) and if fails try to move from /tmp (FS) to /var/www/app/public (FS) */ $vfs->move('/tmp/testfile.jpg','/var/www/app/public/avatar.jpg' );
安装
将此仓库添加到您的composer.json中
"repositories": { "pluggit/storage": { "type": "vcs", "url": "git@github.com:CMProductions/storage.git" } }
然后按常规要求它
composer require "pluggit/storage"
##适配器
它提供了一个通用的API,用于处理多个文件存储引擎中的常见任务。如果您想添加一个新适配器,您必须实现\Cmp\Storage\AdapterInterface
。
适配器接口包含以下方法
exists
get
getStream
rename
copy
delete
put
putStream
getName
内置
此库附带两个内置适配器,可直接使用。
- FileSystem:此适配器直接与宿主文件系统交互。
- S3AWS:此适配器与Amazon S3服务交互。(您必须添加一些环境参数才能使用它)
策略
它允许您在注册了多个适配器时指定不同的调用策略。如果您想创建自定义调用策略,您必须扩展\Cmp\Storage\Strategy\AbstractStorageCallStrategies
。
注意:默认情况下,该库使用CallAllStrategy。
挂载点
有时您可能希望根据您正在工作的路径使用不同的适配器或策略。我们使用MountableVirtualStorage来解决这个问题。MountableVirtualStorage需要使用一个VirtualStorage实现(适配器或策略)来构建,并将其绑定到'/'。
之后,您可以注册新的挂载点。
示例
$s3Adapter = new \Cmp\Storage\Adapter\S3AWSAdapter(); $fileSystemAdapter = new \Cmp\Storage\Adapter\FileSystemAdapter(); $localMountPoint = new \Cmp\Storage\MountPoint('/tmp', $fileSystemAdapter); $publicMountPoint = new \Cmp\Storage\MountPoint('/var/www/app/public', $s3Adapter); $vfs = new \Cmp\Storage\MountableVirtualStorage($fileSystemStorage); //bind to / $vfs->registerMountPoint($localMountPoint); $vfs->registerMountPoint($publicMountPoint); $vfs->delete('/tmp/testfile'); //running over filesystem adapter $vfs->put('/var/www/app/public/testfile', '..some content..')); //running over AWS S3 adapter
- 挂载点之间的移动也是允许的。
- 您可以注册适配器、策略或任何实现VirtualStorage的类。
内置
有两个可用的策略。
\Cmp\Storage\StrategyFallBackChainStrategy
:此策略按插入顺序调用每个适配器,直到其中一个返回一些结果。(在发生错误的情况下,它将被静默记录)\Cmp\Storage\Strategy\CallAllStrategy
:此策略调用所有提供者以应用相同的操作。(非常适合迁移的第一步)
日志记录
该库提供了一个默认的stdout记录器,但您可以在任何时候将其更改为任何PSR-3兼容的记录器。
StorageBuilder
存储构建器负责为您创建一个抽象存储。
可用的函数有
流畅调用
setStrategy(AbstractStorageCallStrategy $strategy)
:设置自定义策略setLogger(LoggerInterface $logger)
:设置自定义记录器addAdapter($adapter)
:添加新的适配器build(AbstractStorageCallStrategy $callStrategy = null, LoggerInterface $logger = null)
:构建虚拟存储
非流畅调用
getStrategy()
:获取当前策略getLogger()
:获取当前记录器hasLoadedAdapters()
:检查是否已加载一个或多个适配器
存储中可用的函数
存在
检查文件是否存在。
获取
读取文件并返回内容。
获取流
检索文件的读取流。
重命名
重命名文件。
复制
复制文件。
删除
删除文件或目录(即使不为空)。
放入
创建文件或更新(如果存在)。它将创建缺失的文件夹。
放入流
创建文件或更新(如果存在)。它将创建缺失的文件夹。
注意:对于大文件,请使用流函数。
要求
- php 5.6
贡献
- 分支仓库。
- 修复一个问题或添加新的适配器或改进某些内容或您想做的任何事情。
- 遵循PSR-2样式。
- 运行
make tests
。 (如有需要,请添加更多测试)。 - 提交一个拉取请求。