blazon / psr11-flysystem
PSR-11 的 Flysystem 工厂
Requires
- php: ^8.0
- league/flysystem: ^2.1
- league/flysystem-memory: ^2.0
- psr/container: ^1.0.0
Requires (Dev)
- async-aws/simple-s3: ^1.1
- league/flysystem-async-aws-s3: ^2.0
- league/flysystem-aws-s3-v3: ^2.1
- league/flysystem-ftp: ^2.0
- league/flysystem-google-cloud-storage: ^2.0
- league/flysystem-sftp: ^2.1
- league/flysystem-ziparchive: ^2.0
- phpmd/phpmd: ^2.10
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
- symfony/dependency-injection: ^5.3
README
PSR-11 FlySystem
FlySystem 版本 2+ 的 PSR-11 工厂。如果您需要版本 1 的工厂,请参阅:https://github.com/wshafer/psr11-flysystem
目录
安装
composer require blazon/psr11-flysystem
使用
<?php
// Get the FlySystem FileSystem
$fileSystem = $container->get('myFileSystemService');
// Write to file
$fileSystem->write('test.txt', 'this is test');
更多信息请参阅文档
容器
任何 PSR-11 容器都可以使用。为了做到这一点,您需要添加配置并注册一个指向 Blazon\PSR11FlySystem\FlySystemFactory
的新服务
以下是一些具体的容器示例,以帮助您开始
Pimple 示例
// Create Container
$container = new \Xtreamwayz\Pimple\Container([
// FlySystem using the default keys.
'fileSystem' => new \Blazon\PSR11FlySystem\FlySystemFactory(),
// FlySystem using a different filesystem configuration
'other' => function($c) {
return \Blazon\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->write('test1.txt', 'this is a test');
print $fileSystem->read('test1.txt');
Laminas 服务管理器
// Create the container and define the services you'd like to use
$container = new \Zend\ServiceManager\ServiceManager([
'factories' => [
// FlySystem using the default keys.
'fileSystem' => \Blazon\PSR11FlySystem\FlySystemFactory::class,
// FlySystem using a different filesystem configuration
'other' => [\Blazon\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
'other' => [
'type' => 'local',
'options' => [
'root' => '/tmp/zend'
],
],
],
],
]);
/** @var \League\Flysystem\FilesystemInterface $fileSystem */
$fileSystem = $container->get('someOtherAdaptor');
$fileSystem->write('test1.txt', 'this is a test');
print $fileSystem->read('test1.txt');
框架
任何使用 PSR-11 的框架都应该运行良好。以下是一些具体的框架示例,以帮助您开始
Mezzio
您需要添加配置并注册您希望使用的服务。有几种方法可以实现这一点,但推荐的方法是创建一个新的配置文件 config/autoload/flySystem.global.php
配置
config/autoload/flySystem.global.php
<?php
return [
'dependencies' => [
'factories' => [
// FlySystem using the default keys.
'fileSystem' => \Blazon\PSR11FlySystem\FlySystemFactory::class,
// FlySystem using a different filesystem configuration
'someOtherAdaptor' => [\Blazon\PSR11FlySystem\FlySystemFactory::class, 'someOtherAdaptor'],
],
],
'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'
],
],
],
],
];
Laminas
您需要添加配置并注册您希望使用的服务。有几种方法可以实现这一点,但推荐的方法是创建一个新的配置文件 config/autoload/flySystem.global.php
配置
config/autoload/flySystem.global.php
<?php
return [
'service_manager' => [
'factories' => [
// FlySystem using the default keys.
'fileSystem' => \Blazon\PSR11FlySystem\FlySystemFactory::class,
// FlySystem using a different filesystem configuration
'someOtherAdaptor' => [\Blazon\PSR11FlySystem\FlySystemFactory::class, 'someOtherAdaptor'],
],
],
'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'
],
],
],
],
];
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
容器服务配置
app/config/services.yml
services:
# FlySystem using the default keys.
fileSystem:
factory: 'Blazon\PSR11FlySystem\FlySystemFactory:__invoke'
class: 'League\Flysystem\FilesystemInterface'
arguments: ['@service_container']
public: true
# FlySystem using a different filesystem configuration
someOtherAdaptor:
factory: ['Blazon\PSR11FlySystem\FlySystemFactory', __callStatic]
class: 'League\Flysystem\FilesystemInterface'
arguments: ['someOtherAdaptor', ['@service_container']]
public: true
Blazon\PSR11FlySystem\FlySystemFactory:
class: 'Blazon\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('someOtherAdaptor');
$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'
],
],
],
],
],
];
$app = new \Slim\App($config);
// Wire up the factory
$container = $app->getContainer();
// FlySystem using the default keys.
$container['fileSystem'] = new \Blazon\PSR11FlySystem\FlySystemFactory();
// FlySystem using a different filesystem configuration
$container['someOtherAdaptor'] = function ($c) {
return \Blazon\PSR11FlySystem\FlySystemFactory::someOtherAdaptor($c);
};
// Example usage
$app->get('/example', function (Request $request, Response $response) {
/** @var \League\Flysystem\FilesystemInterface $fileSystem */
$fileSystem = $this->get('fileSystem');
$fileSystem->write('default.txt', 'Hi there');
/** @var \League\Flysystem\FilesystemInterface $fileSystem */
$fileSystem = $this->get('someOtherAdaptor');
$fileSystem->write('other.txt', 'Hi there');
});
$app->run();
配置
最小配置
最小配置至少需要定义一个服务和 "default" 适配器。
最小示例(使用 Zend Expressive 作为示例)
<?php
return [
'dependencies' => [
'factories' => [
// FlySystem using the default keys.
'MyServiceName' => \Blazon\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
],
],
],
],
];
使用此设置,您将使用 "default" 文件系统和 "default" 适配器。在此示例中,我们将使用本地文件适配器作为默认适配器。
完整配置
注意:需要一个 "default" 适配器。
完整示例
<?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
],
// Mount Manager Config
'manager' => [
'type' => 'manager', // Adaptor name or pre-configured service from the container
'options' => [
'fileSystems' => [
'default' => 'default', // Adaptor name from adaptor configuration
'adaptorTwo' => 'adaptorTwo', // Adaptor name from adaptor configuration
],
],
],
],
],
];
适配器
支持适配器的示例配置
本地
<?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\Local\LocalFilesystemAdapter::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 文档:[本地适配器](https://flysystem.thephpleague.com/v2/docs/adapter/local/)
FTP
<?php
return [
'flysystem' => [
'adaptors' => [
'default' => [
'type' => 'ftp',
'options' => [
'host' => 'ftp.example.com', // Required : Host
'username' => 'username', // Required : Username
'password' => 'password', // Required : Password
'root' => '/root/path/', // required
// optional config settings
'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
],
],
],
],
];
FlySystem 文档:[FTP](https://flysystem.thephpleague.com/v2/docs/adapter/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', // Required : Username
'password' => 'password', // Optional : Password
'privateKey' => 'path/to/or/contents/of/privatekey', // Optional : Private SSH Key
'passphrase' => 'passphrase', // Optional : SSH Key Passphrase
'root' => '/path/to/root', // Required : Root Path
'timeout' => 10, // Optional : Timeout
'useAgent' => false, // Optional : Use Agent (default: false)
'hostFingerprint' => 'fingerprint', // Optional : Host Fingerprint
'maxTries' => 4, // Optional : Max tries
// Optional: Optional set of permissions to set for files
'permissions' => [
'file' => [
'public' => 0644,
'private' => 0600,
],
'dir' => [
'public' => 0755,
'private' => 0700,
],
],
],
],
],
],
];
FlySystem 文档:[SFTP](https://flysystem.thephpleague.com/v2/docs/adapter/sftp/)
内存
安装
composer require league/flysystem-memory
配置
<?php
return [
'flysystem' => [
'adaptors' => [
'default' => [
'type' => 'memory',
'options' => [], // No options available
],
],
],
];
FlySystem 文档:[内存](https://flysystem.thephpleague.com/adapter/memory/)
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 归档](https://flysystem.thephpleague.com/adapter/zip-archive/)
AWS S3
注意:此软件包不支持 AWS V2
安装
composer require league/flysystem-aws-s3-v3
配置
<?php
return [
'flysystem' => [
'adaptors' => [
'default' => [
'type' => 's3',
'options' => [
'client' => 'some-container-service', // Required if client options not provided : S3 client service name
'key' => 'aws-key', // Required if no client provided : Key
'secret' => 'aws-secret', // Required if no client provided : Secret
'region' => 'us-east-1', // Required if no client provided : Region
'bucket' => 'bucket-name', // Required : Bucket Name
'prefix' => 'some/prefix', // Optional : Prefix
'version' => 'latest', // Optional : Api Version. Default: 'latest'
'dirPermissions' => \League\Flysystem\Visibility::PUBLIC, // or ::PRIVATE (Optional)
],
],
],
],
];
FlySystem 文档:[Aws S3 适配器 - SDK V3](https://flysystem.thephpleague.com/v2/docs/adapter/aws-s3-v3/)
Async Aws S3 适配器
安装
composer require async-aws/simple-s3
composer require league/flysystem-async-aws-s3
配置
<?php
return [
'flysystem' => [
'adaptors' => [
'default' => [
'type' => 'AsyncAwsS3',
'options' => [
'client' => 'some-container-service', // Required if client options not provided : S3 client service name
'key' => 'aws-key', // Required if no client provided : Key
'secret' => 'aws-secret', // Required if no client provided : Secret
'region' => 'us-east-1', // Required if no client provided : Region
'bucket' => 'bucket-name', // Required : Bucket Name
'prefix' => 'some/prefix', // Optional : Prefix
'dirPermissions' => \League\Flysystem\Visibility::PUBLIC, // or ::PRIVATE (Optional)
],
],
],
],
];
FlySystem 文档:[AsyncAws S3 适配器](https://flysystem.thephpleague.com/v2/docs/adapter/async-aws-s3/)
Google Cloud Storage
安装
composer require league/flysystem-google-cloud-storage
配置
<?php
return [
'flysystem' => [
'adaptors' => [
'default' => [
'type' => 'GoogleCloudStorage',
'options' => [
'bucket' => 'bucket name or service', // Required
'client' => 'service name', // Required if no clientOptions are provided
// Required if no client is provided
'clientOptions' => [
'keyFile' => 'path-to-key-file.json', // Required : Auth key file
'projectId' => 'myProject', // Optional
],
'prefix' => 'some/prefix', // Optional : Prefix
'defaultVisibility' => \League\Flysystem\Visibility::PUBLIC, // or ::PRIVATE (Optional)
// Optional permissions/acl
'permissions' => [
'entity' => 'allUsers',
'publicAcl' => \League\Flysystem\GoogleCloudStorage\PortableVisibilityHandler::ACL_PUBLIC_READ,
'privateAcl' => \League\Flysystem\GoogleCloudStorage\PortableVisibilityHandler::ACL_PRIVATE,
],
],
],
],
],
];
飞系统文档:Google Cloud Storage 适配器
升级
版本 2 到版本 3
版本 3 将 FlySystem 升级到版本 2。FlySystem 版本 2 是对伟大 FlySystem 的一次全新尝试。库已经变得精简和简化。这也导致我们采取了新的方法,引入了许多破坏性变更。
向后兼容性中断
已将所有命名空间从 WShafer 更改为 Blazon,因为贡献者名单已经扩展到不仅仅是我自己。我相信这次变动将有助于确保向前兼容性,并允许这些库在未来得到良好的维护。
文件管理器已被移除。这是版本 1 的遗留问题,并在版本 2 中已弃用。如果您仍在代码库中使用它,则需要更新您的代码。
文件缓存已从上游移除,因此也从本库中移除。
FlySystem 插件已从上游移除,并且不再受支持。
随着缓存和插件的移除,文件系统的配置已简化。"fileSystems" 键已被移除,现在只保留 "adaptors" 键。
"挂载管理器" 现在可以像其他任何适配器一样进行配置。
上游移除的适配器
- 空
- Azure
- Dropbox