wshafer / psr11-flysystem
Requires
- php: ^7.0
- league/flysystem: ^1.0.40
- league/flysystem-cached-adapter: ^1.0
- psr/container: ^1.0.0
Requires (Dev)
- league/flysystem-aws-s3-v3: ^1.0
- league/flysystem-azure: ^1.0
- league/flysystem-memory: ^1.0
- league/flysystem-sftp: ^1.0
- league/flysystem-ziparchive: ^1.0
- phpmd/phpmd: @stable
- phpunit/phpunit: ^6.0.8
- predis/predis: ^1.1
- satooshi/php-coveralls: ^1.0
- spatie/flysystem-dropbox: ^1.0
- squizlabs/php_codesniffer: ^2.8.1
- symfony/dependency-injection: ^3.3
README
PSR-11 FlySystem
FlySystem 版本 1 为 PSR-11 提供工厂
已弃用
此库已被弃用并迁移。请查看新模块 blazon/psr11-flysystem 的最新版本。
目录
安装
composer require wshafer/psr11-flysystem
使用
<?php // Get the FlySystem FileSystem $fileSystem = $container->get('myFileSystemService'); // Write to file $fileSystem->put('test.txt', 'this is test');
更多信息可以在 文档 中找到
容器
任何 PSR-11 容器都适用。为了实现这一点,您需要添加配置并注册一个指向 WShafer\PSR11FlySystem\FlySystemFactory
的新服务
以下是一些具体的容器示例以供您开始
Pimple 示例
// Create Container $container = new \Xtreamwayz\Pimple\Container([ // FlySystem using the default keys. 'fileSystem' => new \WShafer\PSR11FlySystem\FlySystemFactory(), // FlySystem using a different filesystem configuration 'other' => function($c) { return \WShafer\PSR11FlySystem\FlySystemFactory::other($c); }, // Config 'config' => [ 'flysystem' => [ 'adaptors' => [ // At the bare minimum you must include a default adaptor. 'default' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/pimple' ], ], // Some other Adaptor. Keys are the names for each adaptor 'someOtherAdaptor' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/pimple' ], ], ], 'fileSystems' => [ 'other' => [ 'adaptor' => 'someOtherAdaptor' ], ], ], ] ]); /** @var \League\Flysystem\FilesystemInterface $fileSystem */ $fileSystem = $container->get('other'); $fileSystem->put('test1.txt', 'this is a test'); print $fileSystem->get('test1.txt')->read();
Zend 服务管理器
// Create the container and define the services you'd like to use $container = new \Zend\ServiceManager\ServiceManager([ 'factories' => [ // FlySystem using the default keys. 'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class, // FlySystem using a different filesystem configuration 'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'], ], ]); // Config $container->setService('config', [ 'flysystem' => [ 'adaptors' => [ // At the bare minimum you must include a default adaptor. 'default' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/zend' ], ], // Some other Adaptor. Keys are the names for each adaptor 'someOtherAdaptor' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/zend' ], ], ], 'fileSystems' => [ 'other' => [ 'adaptor' => 'someOtherAdaptor' ], ], ], ]); /** @var \League\Flysystem\FilesystemInterface $fileSystem */ $fileSystem = $container->get('other'); $fileSystem->put('test1.txt', 'this is a test'); print $fileSystem->get('test1.txt')->read();
框架
任何使用 PSR-11 的框架都应正常工作。以下是一些具体的框架示例以供您开始
Zend Expressive
您需要添加配置并注册您想要使用的服务。有多种方法可以实现这一点,但推荐的方法是创建一个新的配置文件 config/autoload/flySystem.global.php
配置
config/autoload/flySystem.global.php
<?php return [ 'dependencies' => [ 'factories' => [ // FlySystem using the default keys. 'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class, // FlySystem using a different filesystem configuration 'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'], ], ], 'flysystem' => [ 'adaptors' => [ // At the bare minimum you must include a default adaptor. 'default' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/zend' ], ], // Some other Adaptor. Keys are the names for each adaptor 'someOtherAdaptor' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/zend' ], ], ], 'fileSystems' => [ 'other' => [ 'adaptor' => 'someOtherAdaptor' ], ], ], ];
Zend Framework 3
您需要添加配置并注册您想要使用的服务。有多种方法可以实现这一点,但推荐的方法是创建一个新的配置文件 config/autoload/flySystem.global.php
配置
config/autoload/flySystem.global.php
<?php return [ 'service_manager' => [ 'factories' => [ // FlySystem using the default keys. 'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class, // FlySystem using a different filesystem configuration 'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'], ], ], 'flysystem' => [ 'adaptors' => [ // At the bare minimum you must include a default adaptor. 'default' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/zend' ], ], // Some other Adaptor. Keys are the names for each adaptor 'someOtherAdaptor' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/zend' ], ], ], 'fileSystems' => [ 'other' => [ 'adaptor' => 'someOtherAdaptor' ], ], ], ];
模块配置
如果您没有使用Zend 组件安装器,您还需要注册该模块。
config/modules.config.php (ZF 3 框架)
<?php return [ // ... Previously registered modules here 'WShafer\\PSR11FlySystem', ];
config/application.config.php (ZF 2 框架)
<?php return [ 'modules' => [ // ... Previously registered modules here 'WShafer\\PSR11FlySystem', ] ];
Symfony
尽管有其他 Symfony 扩展包,但截至 Symfony 3.3,服务容器现在是 PSR-11 兼容容器。下面的配置将使这些工厂在 Symfony 中注册并运行。
配置
app/config/config.yml (或等效)
parameters: flysystem: adaptors: # At the bare minimum you must include a default adaptor. default: type: local options: root: /tmp/symfony # Some other Adaptor. Keys are the names for each adaptor someOtherAdaptor: type: local options: root: /tmp/symfony fileSystems: other: adaptor: someOtherAdaptor
容器服务配置
app/config/services.yml
services: # FlySystem using the default keys. fileSystem: factory: 'WShafer\PSR11FlySystem\FlySystemFactory:__invoke' class: 'League\Flysystem\FilesystemInterface' arguments: ['@service_container'] public: true # FlySystem using a different filesystem configuration other: factory: ['WShafer\PSR11FlySystem\FlySystemFactory', __callStatic] class: 'League\Flysystem\FilesystemInterface' arguments: ['other', ['@service_container']] public: true WShafer\PSR11FlySystem\FlySystemFactory: class: 'WShafer\PSR11FlySystem\FlySystemFactory' public: true
示例用法
src/AppBundle/Controller/DefaultController.php
<?php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller { /** * @Route("/", name="homepage") */ public function indexAction(Request $request) { $fileSystem = $this->container->get('fileSystem'); $fileSystem->write('default.txt', 'Hi there'); $fileSystem = $this->container->get('other'); $fileSystem->write('other.txt', 'Hi there'); } }
Slim
public/index.php
<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require '../vendor/autoload.php'; // Add Configuration $config = [ 'settings' => [ 'flysystem' => [ 'adaptors' => [ // At the bare minimum you must include a default adaptor. 'default' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/slim' ], ], // Some other Adaptor. Keys are the names for each adaptor 'someOtherAdaptor' => [ 'type' => 'local', 'options' => [ 'root' => '/tmp/slim' ], ], ], 'fileSystems' => [ 'other' => [ 'adaptor' => 'someOtherAdaptor' ], ], ], ], ]; $app = new \Slim\App($config); // Wire up the factory $container = $app->getContainer(); // FlySystem using the default keys. $container['fileSystem'] = new \WShafer\PSR11FlySystem\FlySystemFactory(); // FlySystem using a different filesystem configuration $container['other'] = function ($c) { return \WShafer\PSR11FlySystem\FlySystemFactory::other($c); }; // Example usage $app->get('/example', function (Request $request, Response $response) { /** @var \League\Flysystem\FilesystemInterface $fileSystem */ $fileSystem = $this->get('fileSystem'); $fileSystem->put('default.txt', 'Hi there'); /** @var \League\Flysystem\FilesystemInterface $fileSystem */ $fileSystem = $this->get('other'); $fileSystem->put('other.txt', 'Hi there'); }); $app->run();
配置
Fly System 使用三种类型的服务,每种服务都需要为您的应用程序进行配置。此外,您还需要创建一个名为服务,该服务映射到基于容器的 \WShafer\PSR11FlySystem\FlySystemFactory
。
-
命名服务:这些是与工厂相连的服务名称。配置将根据使用的容器/框架类型而有所不同。
-
适配器:这是连接到实际文件系统的适配器。这可以是 Azure 容器、S3、本地、内存等。
-
缓存:(可选)优化性能的缓存层。虽然这是可选的,但如果未提供,此包将默认使用内存缓存。
-
文件系统:这将配置您实际使用的最终 FlySystem 文件系统。它使用前面的两个配置来为您提供可以与之一起工作的完整功能的文件系统。此外,您还可以配置一个 挂载管理器,以连接需要相互交互的多个文件系统。
最小配置
最小配置至少需要定义一个服务和“默认”适配器。
最小示例(使用 Zend Expressive 作为示例)
<?php return [ 'dependencies' => [ 'factories' => [ // FlySystem using the default keys. 'MyServiceName' => \WShafer\PSR11FlySystem\FlySystemFactory::class, ], ], 'flysystem' => [ 'adaptors' => [ // Array Keys are the names used for the adaptor 'default' => [ 'type' => 'local', # Adaptor name or pre-configured service from the container // Adaptor specific options. See adaptors below 'options' => [ 'root' => '/path/to/root', // Path on local filesystem ], ], ], ], ];
使用此设置,您将使用“默认”文件系统和“默认”适配器。在这个例子中,我们将使用本地文件适配器作为默认。
完整配置
注意:“默认”适配器是必需的。
完整示例
<?php return [ 'flysystem' => [ 'adaptors' => [ // Array Keys are the names used for the adaptor. Default entry required for adaptors 'default' => [ 'type' => 'local', // Adaptor name or pre-configured service from the container // Adaptor specific options. See adaptors below 'options' => [ 'root' => '/path/to/root', // Path on local filesystem ], ], 'adaptorTwo' => [ 'type' => 'null', // Adaptor name or pre-configured service from the container 'options' => [], // Adaptor specific options. See adaptors below ], ], 'caches' => [ // Array Keys are the names used for the cache // // Note: You can specify "default" here to overwrite the default settings for the // default cache. Memory is used if not specified 'default' => [ 'type' => 'psr6', // Cache specific options. See caches below 'options' => [ 'service' => 'my_psr6_service_from_container', 'key' => 'my_key_', 'ttl' => 3000 ], ], 'cacheTwo' => [ 'type' => 'psr6', // Cache specific options. See caches below 'options' => [ 'service' => 'my_psr6_service_from_container', 'key' => 'my_key_', 'ttl' => 3000 ], ], ], 'fileSystems' => [ // Array Keys are the file systems identifiers. // // Note: You can specify "default" here to overwrite the default settings for the // default file system 'default' => [ 'adaptor' => 'default', // Adaptor name from adaptor configuration 'cache' => 'default', // Cache name from adaptor configuration 'plugins' => [] // User defined plugins to be injected into the file system ], // Mount Manager Config 'manager' => [ 'adaptor' => 'manager', 'fileSystems' => [ 'local' => [ 'adaptor' => 'default', // Adaptor name from adaptor configuration 'cache' => 'default', // PSR-6 pre-configured service 'plugins' => [] // User defined plugins to be injected into the file system ], 'anotherFileSystem' => [ 'adaptor' => 'adaptorTwo', // Adaptor name from adaptor configuration 'cache' => 'cacheTwo', // PSR-6 pre-configured service 'plugins' => [] // User defined plugins to be injected into the file system ], ], ], ], ], ];
文件系统
<?php return [ 'flysystem' => [ 'fileSystems' => [ // Array Keys are the file systems identifiers 'myFileSystemName' => [ 'adaptor' => 'default', // Required : Adaptor name from adaptor configuration 'cache' => 'default', // Optional : Cache name from adaptor configuration 'plugins' => [] // Optional : User defined plugins to be injected into the file system ], ], ], ];
适配器
支持适配器的示例配置
Null/Test
<?php return [ 'flysystem' => [ 'adaptors' => [ 'myAdaptorName' => [ 'type' => 'null', 'options' => [], #No options available ], ], ], ];
FlySystem 文档:Null 适配器
本地
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'local', 'options' => [ 'root' => '/path/to/root', // Required : Path on local filesystem 'writeFlags' => LOCK_EX, // Optional : PHP flags. See: file_get_contents for more info 'linkBehavior' => League\Flysystem\Adapter\Local::DISALLOW_LINKS, // Optional : Link behavior // Optional: Optional set of permissions to set for files 'permissions' => [ 'file' => [ 'public' => 0644, 'private' => 0600, ], 'dir' => [ 'public' => 0755, 'private' => 0700, ] ] ], ], ], ], ];
FlySystem 文档:本地适配器
FTP
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'ftp', 'options' => [ 'host' => 'ftp.example.com', // Required : Host 'username' => 'username', // Required : Username 'password' => 'password', // Required : Password // optional config settings 'port' => 21, 'root' => '/path/to/root', 'passive' => true, 'ssl' => true, 'timeout' => 30, ], ], ], ], ];
FlySystem 文档:FTP
SFTP
安装
composer require league/flysystem-sftp
配置
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'sftp', 'options' => [ 'host' => 'example.com', // Required : Host 'port' => 21, // Optional : Port 'username' => 'username', // Optional : Username 'password' => 'password', // Optional : Password 'privateKey' => 'path/to/or/contents/of/privatekey', // Optional : Host 'root' => '/path/to/root', // Required : Root Path 'timeout' => 10, // Optional : Timeout ], ], ], ], ];
FlySystem 文档:SFTP
内存
安装
composer require league/flysystem-memory
配置
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'memory', 'options' => [], // No options available ], ], ], ];
FlySystem 文档:内存
ZIP 存档
安装
composer require league/flysystem-ziparchive
配置
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'zip', 'options' => [ 'path' => '/some/path/to/file.zip' // Required : File name and path to use for zip file ], ], ], ], ];
FlySystem 文档:ZIP 存档
Azure
安装
composer require league/flysystem-azure
配置
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'azure', 'options' => [ 'accountName' => 'account-name', // Required : Account Name 'apiKey' => 'api-key', // Required : API Key 'container' => 'container-name', // Required : Container name 'prefix' => 'prefix_', // Optional ], ], ], ], ];
FlySystem 文档:Azure 适配器
AWS S3
注意:此包不支持 AWS V2
安装
composer require league/flysystem-aws-s3-v3
配置
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 's3', 'options' => [ 'key' => 'aws-key', // Required : Key 'secret' => 'aws-secret', // Required : Secret 'region' => 'us-east-1', // Required : Region 'bucket' => 'bucket-name', // Required : Bucket Name 'prefix' => 'some/prefix', // Optional : Prefix 'version' => 'latest' // Optional : Api Version. Default: 'latest' ], ], ], ], ];
FlySystem 文档:Aws S3 适配器 - SDK V3
DropBox
安装
composer require spatie/flysystem-dropbox
配置
<?php return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ 'type' => 'dropbox', 'options' => [ 'token' => 'my-token', // Required : API Token 'prefix' => 'prefix', // Optional : Prefix ], ], ], ], ];
FlySystem 文档:DropBox
缓存
支持的缓存示例配置
内存/测试
<?php return [ 'flysystem' => [ 'caches' => [ 'default' => [ 'type' => 'memory', 'options' => [], // No options available ], ], ], ];
FlySystem 文档:缓存
适配器
此缓存适配器将使用另一个适配器将缓存存储到文件中。它将从现有的管理器中拉取此文件系统。
<?php return [ 'flysystem' => [ 'caches' => [ 'default' => [ 'type' => 'adaptor', 'options' => [ 'adaptor' => 'MyAdaptorName', // Required : Adaptor name found in the adaptor config 'fileName' => 'my_cache_file', // Required : File name for cache file 'ttl' => 300 // Optional : Time to live ], ], ], ], ];
FlySystem 文档:缓存
PSR-6
<?php return [ 'flysystem' => [ 'caches' => [ 'default' => [ 'type' => 'psr6', 'options' => [ 'service' => 'my_psr6_service_from_container', // Required : Service to be used from the container 'key' => 'my_key_', // Required : Cache Key 'ttl' => 3000 // Optional : Time to live ], ], ], ], ];
FlySystem 文档:未知
Predis
<?php return [ 'flysystem' => [ 'caches' => [ 'default' => [ 'type' => 'predis', 'options' => [ 'service' => 'my_predis_client_from_container', // Required : Configured Predis Client Service to pull from container 'key' => 'my_key_', // Required : Cache Key 'ttl' => 3000 // Optional : Time to live ], ], ], ], ];
FlySystem 文档:缓存
Memcached
<?php return [ 'flysystem' => [ 'caches' => [ 'default' => [ 'type' => 'memcached', 'options' => [ 'service' => 'my_memcached_client_from_container', // Required : Configured Memcached Client Service to pull from container 'key' => 'my_key_', // Required : Cache Key 'ttl' => 3000 // Optional : Time to live ], ], ], ], ];
FlySystem 文档:缓存
Stash
参见:PSR6
注意:虽然 "The League" 提供了原生缓存客户端,但 Stash 本身已经实现了 PSR 6 接口。建议使用它。
升级
版本 1 到版本 2
在从版本 1 升级到版本 2 时,不需要进行任何更改。请注意,不再建议直接使用 FlySystemManager。应使用命名服务。见上文了解更多信息。
更改
-
已自动将“默认”文件系统条目添加到配置中。此默认文件系统需要您指定配置的文件系统适配器,或者必须有配置的默认适配器才能使用它。大多数版本 1 用户应该不会遇到此更改的问题,并且工厂仍然可以正常工作。
-
现在可以覆盖和重新配置默认缓存。以前,当文件系统没有指定要使用的缓存时,始终使用“内存”缓存。