socloz / knp-gaufrette-bundle
允许在 Symfony 项目中轻松使用 Gaufrette 库
Requires
- knplabs/gaufrette: 0.1.4
- symfony/framework-bundle: 2.*
Requires (Dev)
- symfony/console: 2.*
- symfony/yaml: 2.*
This package is not auto-updated.
Last update: 2020-01-20 05:37:33 UTC
README
为您的 Symfony 项目提供 Gaufrette 集成。
关于 Gaufrette
Gaufrette 是一个 PHP 5.3+ 库,提供文件系统抽象层。这个抽象层允许您开发应用程序,而无需知道所有媒体将存储在哪里以及如何存储。
文档可在 Gaufrette 的官方页面 上找到。
安装
先决条件
由于此包是 Gaufrette 库的 Symfony 集成,因此您需要首先在 Symfony 项目中安装 Gaufrette。
下载包
您可以从包中下载归档并将其解压到应用程序的 vendor/bundles/Knp/Bundle/GaufretteBundle
目录中。
标准版样式
如果您正在使用 deps
文件来管理项目依赖项,您必须向其中添加以下行
[gaufrette]
git=http://github.com/KnpLabs/Gaufrette.git
[KnpGaufretteBundle]
git=http://github.com/KnpLabs/KnpGaufretteBundle.git
target=/bundles/Knp/Bundle/GaufretteBundle
Composer 样式
可以使用 composer 安装此包,通过向 composer.json
文件中的 require 部分添加 "knplabs/knp-gaufrette-bundle": "dev-master"
行。
Git Submodule 样式
如果您使用 git 对项目进行版本控制,最好将其作为子模块嵌入
$ git submodule add https://github.com/KnpLabs/KnpGaufretteBundle.git vendor/bundles/Knp/Bundle/GaufretteBundle
在自动加载器中添加命名空间
您必须在自动加载器中注册 Gaufrette 和 KnpGaufretteBundle: (如果您正在使用 composer 自动加载系统,则不需要这样做。)
<?php // app/autoload.php $loader->registerNamespaces(array( 'Knp\Bundle' => __DIR__.'/../vendor/bundles', 'Gaufrette' => __DIR__.'/../vendor/gaufrette/src', // ... ));
注册包
您必须在内核中注册此包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Knp\Bundle\GaufretteBundle\KnpGaufretteBundle() ); // ... }
配置
Gaufrette 包允许您声明文件系统作为服务,而无需触及著名的“服务容器”。实际上,您可以使用配置来完成此操作!
Gaufrette 包的配置分为两部分:适配器(adapters)和文件系统(filesystems)。
配置适配器
# app/config/config.yml knp_gaufrette: adapters: foo: local: directory: /path/to/my/filesystem
定义的适配器可用于创建文件系统。
配置文件系统
# app/config/config.yml knp_gaufrette: adapters: # ... filesystems: bar: adapter: foo alias: foo_filesystem
每个定义的文件系统必须有一个具有适配器键作为值的适配器
。上述定义的文件系统将导致具有id gaufrette.bar_filesystem
的服务。alias
参数还允许为它定义别名。
文件系统映射
您可以通过映射服务访问所有声明的文件系统。在之前的示例中,我们声明了一个bar
文件系统
$container->get('knp_gaufrette.filesystem_map')->get('bar');
返回bar
的Gaufrette\Filesystem
实例。
适配器参考
本地适配器
一个简单的基于本地文件系统的适配器。
参数
directory
文件系统的目录 (必需)create
是否在不存在时创建目录 (默认为 true)
示例
# app/config/config.yml knp_gaufrette: adapters: foo: local: directory: /path/to/my/filesystem create: true
安全本地适配器(safe_local)
与local
适配器几乎一样简单,但它将键进行编码,以避免处理目录结构。
参数
directory
文件系统的目录 (必需)create
是否在不存在时创建目录 (默认为 true)
示例
# app/config/config.yml knp_gaufrette: adapters: foo: safe_local: directory: /path/to/my/filesystem create: true
服务(service)
允许您使用用户定义的适配器服务。
参数
id
服务的id (必需)
示例
# app/config/config.yml knp_gaufrette: adapters: foo: service: id: my.adapter.service
内存(in_memory)
用于测试目的的适配器,它将文件存储在内部数组中。
参数
files
文件数组 (可选)
files
是一个文件数组,其中每个文件都是一个具有content
、checksum
和mtime
(可选)键的子数组。
示例
# app/config/config.yml knp_gaufrette: adapters: foo: in_memory: files: 'file1.txt': ~ 'file2.txt': content: Some content checksum: abc1efg2hij3 mtime: 123456890123
GridFS(gridfs)
允许您使用MongoDB GridFS存储文件的适配器。
参数
mongogridfs_id
为适配器提供MongoGridFS对象实例的服务id (必需)
示例
# app/config/config.yml knp_gaufrette: adapters: foo: gridfs: mongogridfs_id: acme_test.gridfs
在您的AcmeTestBundle中,添加以下服务定义
# src/Acme/TestBundle/Resources/config/services.yml parameters: acme_test.mongo.server: "mongodb://:27017" acme_test.mongo.options: connect: true acme_test.mongodb.name: "test_database" acme_test.gridfs.prefix: "fs" #Default services: acme_test.mongo: class: Mongo arguments: [%acme_test.mongo.server%, %acme_test.mongo.options%] acme_test.mongodb: class: MongoDB arguments: [@acme_test.mongo, %acme_test.mongodb.name%] acme_test.gridfs: class: MongoGridFS arguments: [@acme_test.mongodb, %acme_test.gridfs.prefix%]
请注意,您可以通过任何方式准备MongoGridFS服务。这只是其中一种方法。
MogileFS(mogilefs)
允许您使用MogileFS存储文件的适配器。
参数
domain
MogileFS域hosts
可用的跟踪器
示例
# app/config/config.yml knp_gaufrette: adapters: foo: mogilefs: domain: foobar hosts: ["192.168.0.1:7001", "192.168.0.2:7001"]
FTP
FTP适配器。
参数
directory
文件系统的目录 (必需)host
FTP主机 (必需)username
FTP用户名 (默认null)password
FTP密码 (默认null)port
FTP端口 (默认21)passive
FTP被动模式 (默认false)create
如果不存在是否创建目录 (默认false)mode
FTP传输模式 (默认FTP_ASCII)
示例
# app/config/config.yml knp_gaufrette: adapters: foo: ftp: host: example.com username: user password: pass directory: /example/ftp create: true mode: FTP_BINARY
Sftp
SFTP (SSH-FTP)的适配器。
参数
sftp_id
提供SFTP访问的服务ID。- `directory* 远程目录 (默认null).
create
如果不存在是否创建目录 (默认false).
示例
# app/config/config.yml knp_gaufrette: adapters: foo: sftp: sftp_id: acme_test.sftp directory: /example/sftp create: true
在您的AcmeTestBundle中,添加以下服务定义
# src/Acme/TestBundle/Resources/config/services.yml parameters: acme_test.ssh.host: my_host_name acme_test.ssh.username: user_name acme_test.ssh.password: some_secret services: acme_test.ssh.configuration: class: Ssh\Configuration arguments: [%acme_test.ssh.host%] acme_test.ssh.authentication: class: Ssh\Authentication\Password arguments: [%acme_test.ssh.username%, %acme_test.ssh.password%] acme_test.ssh.session: class: Ssh\Session arguments: [@acme_test.ssh.configuration, @acme_test.ssh.authentication] acme_test.sftp: class: Ssh\Sftp arguments: [@acme_test.ssh.session]
Apc
APC的适配器。
非持久适配器,在开发环境、演示网站等中使用。
参数
prefix
该文件系统的前缀(APC 'namespace',建议以点'.'结尾) (必需)ttl
存活时间 (默认0)
示例
# app/config/config.yml knp_gaufrette: adapters: foo: apc: prefix: APC 'namespace' prefix ttl: 0
Open Cloud (opencloud)
OpenCloud (Rackspace)的适配器
参数
object_store_id
: 对象存储服务的IDcontainer_name
: 要使用的容器名称create_container
: 如果为true
,将在需要时创建容器 (默认false
)detect_content_type
: 如果为true
,将为每个文件检测内容类型 (默认true
)
定义服务
要使用OpenCloud适配器,您应提供一个有效的ObjectStore
实例。您可以通过OpenCloud\OpenStack
或OpenCloud\Rackspace
实例检索实例。我们可以通过Symfony DiC配置提供全面的配置。
定义OpenStack/Rackspace服务
通用OpenStack
# app/config/config.yml services: opencloud.connection: class: OpenCloud\OpenStack arguments: - %openstack_identity_url% - {username: %openstack_username%, password: %openstack_password%, tenantName: %openstack_tenant_name%}
HPCloud
# app/config/config.yml services: opencloud.connection.hpcloud: class: OpenCloud\OpenStack arguments: - 'https://region-a.geo-1.identity.hpcloudsvc.com:123456/v2.0/' // check https://account.hpcloud.com/account/api_keys for identities urls - {username: %hpcloud_username%, password: %hpcloud_password%, tenantName: %hpcloud_tenant_name%}
用户名和密码是您的登录凭据,不是API密钥。您的tenantName是API密钥页面上的项目名称。
Rackspace
# app/config/config.yml services: opencloud.connection.rackspace: class: OpenCloud\Rackspace arguments: - 'https://identity.api.rackspacecloud.com/v2.0/' - {username: %rackspace_username%, apiKey: %rackspace_apikey%}
定义ObjectStore服务
HPCloud
# app/config/config.yml services: opencloud.object_store: class: OpenCloud\ObjectStoreBase factory_service: opencloud.connection.hpcloud factory_method: ObjectStore arguments: - 'Object Storage' # Object storage type - 'region-a.geo-1' # Object storage region - 'publicURL' # url type
Rackspace
# app/config/config.yml services: opencloud.object_store: class: OpenCloud\ObjectStoreBase factory_service: opencloud.connection factory_method: ObjectStore arguments: - 'cloudFiles' # Object storage type - 'DFW' # Object storage region - 'publicURL' # url type
示例
最后,您可以在配置中定义您的适配器
# app/config/config.yml knp_gaufrette: adapters: foo: opencloud: object_store_id: opencloud.object_store container_name: foo
缓存
允许缓存其他适配器的适配器
参数
source
必须缓存的源适配器 (必需)cache
用于缓存源的适配器 (必需)ttl
存活时间 (默认0)serializer
用于缓存序列化的适配器 (默认null)
示例
# app/config/config.yml knp_gaufrette: adapters: media_ftp: ftp: host: example.com username: user password: pass directory: /example/ftp create: true mode: FTP_BINARY media_apc: apc: prefix: APC 'namespace' prefix ttl: 0 media_cache: cache: source: media_ftp cache: media_apc ttl: 7200 filesystems: media: adapter: media_cache
流包装器
您可以注册指定域的文件系统,并在代码的任何位置使用流包装器,例如:gaufrette://domain/file.txt
参数
protocol
协议名称,例如gaufrette://…
(默认 gaufrette)filesystem
一个数组,包含您想要注册的文件系统,可以设置数组键作为域,例如gaufrette://mydomain/…
(默认所有文件系统)
示例 1
协议为 gaufrette,所有文件系统将被保存
# app/config/config.yml knp_gaufrette: adapters: backup: #... amazon: #... filesystems: backup1: adapter: backup amazonS3: adapter: amazon stream_wrapper: ~
gaufrette://backup1/file
gaufrette://amazonS3/file
示例 2
我们将协议定义为 data,所有文件系统将被保存
# app/config/config.yml knp_gaufrette: filesystems: #... stream_wrapper: protocol: data
data://backup1/...
data://amazonS3/...
示例 3
我们将协议定义为 data,并定义将使用哪个文件系统
# app/config/config.yml knp_gaufrette: filesystems: #... stream_wrapper: protocol: data filesystems: - backup1 - amazonS3
data://backup1/...
data://amazonS3/...
示例 4
我们将协议定义为 data,并定义使用域名别名将使用哪个文件系统
# app/config/config.yml knp_gaufrette: filesystems: #... stream_wrapper: protocol: data filesystems: backup: backup1 pictures: amazonS3
data://backup/...
data://pictures/...