tsslabs/knp-gaufrette-bundle

允许在Symfony项目中轻松使用Gaufrette库,并支持Dropbox适配器

安装: 105

依赖项: 1

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 157

类型:symfony-bundle

v0.1.4 2013-01-30 11:41 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:36:01 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部分添加以下内容来安装此包

    "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->addClassMap(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包的配置分为两部分:adaptersfilesystems

配置适配器

# 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

每个定义的文件系统都必须有一个adapter,其值设置为适配器的键。上面定义的文件系统将生成一个id为gaufrette.bar_filesystem的服务。alias参数允许我们为它定义一个别名(在这种情况下为foo_filesystem)。

文件系统映射

您可以通过映射服务访问所有声明的文件系统。在上面的例子中,我们声明了一个bar文件系统

$container->get('knp_gaufrette.filesystem_map')->get('bar');

返回bar的实例Gaufrette\Filesystem

适配器参考

本地适配器(local)

一个简单的基于本地文件系统的适配器。

参数

  • 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》是一个文件数组,其中每个文件都是一个子数组,包含可选的contentchecksummtime键。

示例

# app/config/config.yml
knp_gaufrette:
    adapters:
        foo:
            in_memory:
                files:
                    'file1.txt':    ~
                    'file2.txt':
                        content:    Some content
                        checksum:   abc1efg2hij3
                        mtime:      123456890123

Azure Blob Storage(azure_blob_storage)

Microsoft Azure Blob Storage服务的适配器。要使用此适配器,您需要将Azure SDK for php安装到您的项目中。

此外,您需要一个有效的连接字符串,并且必须使用它定义一个Blob Proxy工厂服务。您可以使用默认的\Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactory,如下所示

# app/config/config.yml
services:
    azure_blob_proxy_factory:
        class: Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactory
        arguments: [%azure_blob_storage_connection_string%]

必须将参数azure_blob_storage_connection_string设置为包含您的Windows Azure Blob Storage连接字符串。您可以在Windows Azure管理控制台中检索您的连接字符串。

参数

  • blob_proxy_factory_id Blob代理工厂服务的引用
  • container_name 容器的名称
  • create_container 布尔值,指示如果容器不存在是否创建它(可选:默认false
  • detect_content_type 布尔值,指示是否自动确定并设置新blob的内容类型(可选:默认true

示例

# app/config/config.yml
knp_gaufrette:
    adapters:
        foo:
            azure_blob_storage:
                blob_proxy_factory_id: azure_blob_proxy_factory
                container_name: my_container
                create_container: true

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)

FTP的适配器。

参数

  • directory 远程目录(必需
  • host FTP主机(必需
  • username FTP用户名(默认null
  • password FTP密码(默认null
  • port FTP端口(默认21
  • passive FTP被动模式(默认false
  • create 如果不存在是否创建目录(默认false
  • mode FTP传输模式(defaut 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)

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)

APC的适配器。

一个非持久性适配器,用于开发环境、演示网站等。

参数

  • prefix 此文件系统的前缀(APC 'namespace',建议以点'.'结尾)(必需
  • ttl 存活时间(默认0

示例

# app/config/config.yml
knp_gaufrette:
    adapters:
        foo:
            apc:
                prefix: APC 'namespace' prefix
                ttl: 0

Amazon S3(amazon_s3)

连接到Amazon S3实例的适配器。

此适配器需要使用amazonwebservices/aws-sdk-for-php,可以通过将以下行添加到您的composer.json中来进行安装

    "require": {
        ...
        "amazonwebservices/aws-sdk-for-php": "1.6.2"
    },

参数

  • amazon_s3_id:用于底层连接的AmazonS3服务的id
  • bucket_name:要使用的存储桶名称
  • options:附加(可选)设置
    • directory:在指定存储桶中使用的目录
    • region
    • create

定义服务

要使用Amazon S3适配器,您需要提供一个有效的AmazonS3实例(如Amazon SDK中定义)。这可以很容易地通过使用Symfony的服务配置来完成

# app/config/config.yml
services:
    amazonS3:
        class: AmazonS3
        arguments:
            options:
                key:      '%aws_key%'
                secret:   '%aws_secret_key%'

示例

一旦设置了服务,就使用其密钥作为gaufrette配置中的amazon_s3_id

# app/config/config.yml
knp_gaufrette:
    adapters:
        foo:
            amazon_s3:
                amazon_s3_id:   amazonS3
                bucket_name:    foo_bucket
                options:
                    directory:  foo_directory

请注意,SDK似乎与包含点的存储桶名称有一些问题,例如,“com.mycompany.bucket”似乎有问题,但“com-mycompany-bucket”可以工作。

AwsS3

Amazon S3 SDK v2的适配器。

参数

  • service_id 要使用的 Aws\S3\S3Client 的服务 ID。 (必填)
  • bucket_name 要使用的 S3 桶的名称。 (必填)
  • options 传递给适配器的额外选项列表。
    • create 如果不存在,是否创建桶。 (默认 false)
    • directory 要操作的目录。 (默认 '') 此目录将在桶的根目录下创建,所有文件都将在此处读取和写入。

示例

# app/config/config.yml
knp_gaufrette:
    adapters:
        profile_photos:
            aws_s3:
                service_id: 'acme.aws_s3.client'
                bucket_name: 'images'
                options:
                    directory: 'profile_photos'

开放云 (opencloud)

OpenCloud (Rackspace) 适配器

参数

  • object_store_id: 对象存储服务的 ID
  • container_name: 要使用的容器名称
  • create_container: 如果为 true,则如果不存在将创建容器 (默认 false)
  • detect_content_type: 如果为 true,将为每个文件检测内容类型 (默认 true)

定义服务

要使用 OpenCloud 适配器,您应提供一个有效的 ObjectStore 实例。您可以通过 OpenCloud\OpenStackOpenCloud\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

缓存 (cache)

允许您缓存其他适配器的适配器

参数

  • 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

Stream Wrapper

stream_wrapper 设置允许您将文件系统注册到指定的域,然后在代码的任何地方使用流包装器,如:gaufrette://domain/file.txt

参数

  • protocol 协议名称,如 gaufrette://… (默认 gaufrette)
  • filesystem 包含您想要注册到此流包装器的文件系统的数组。如果您设置数组键,这些键将用作文件系统的别名(请参阅下面的示例) (默认所有没有别名的文件系统)

示例 1

使用默认设置,协议是 "gaufrette",所有文件系统都将提供服务

# app/config/config.yml
knp_gaufrette:
    adapters:
        backup: #...
        amazon: #...

    filesystems:
        backup1:
            adapter: backup
        amazonS3:
            adapter: amazon

    stream_wrapper: ~
gaufrette://backup1/...
gaufrette://amazonS3/...

示例 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
data://backup1/... (works since it is defined above)
data://amazonS3/... (will not be available)

示例 4

我们将协议定义为 data,并使用数组键定义哪些文件系统将可用,以设置域别名

# app/config/config.yml
knp_gaufrette:
    filesystems:
        #...

    stream_wrapper:
        protocol: data
        filesystems:
            backup: backup1
            pictures: amazonS3
data://backup/...
data://pictures/...

Doctrine DBAL (doctrine_dbal)

允许您将数据存储到数据库中的适配器。

参数

  • connection_name doctrine dbal 连接名称,如 default
  • table 表名称,如 media_data
  • key:表中的主键
  • content 文件内容的字段名称
  • mtime 时间戳的字段名称
  • checksum 校验和的字段名称

示例

# app/config/config.yml
knp_gaufrette:
    adapters:
        database:
            doctrine_dbal:
                connection_name: default
                table: data
                columns:
                    key: id
                    content: text
                    mtime: date
                    checksum: checksum