The sfGaufrettePlugin integrates Gaufrette in a symfony project.
First, it uses a cool project. Then, it's cool because it allows you to define
"gaufrettes" directly from the config/gaufrettes.yml
configuration file and use them easily
everywhere in your applications.
It also provides a way to map filesystem resources (files) to their URL.
# config/gaufrettes.yml
all: # the environment
my_filesystem:
adapter: # create a ftp filesystem
class: \Gaufrette\Adapter\Ftp
param:
path: /home/joe
host: foo.com
username: joe
password: joe
cache: # cache it locally
class: \Gaufrette\Adapter\Local
param:
destination: %SF_CACHE_DIR%/gaufrette/cache
ttl: 3600
url_resolver: # and define the URL resolver
class: sfPrefixUrlResolver
param:
prefix: /uploads/
my_s3_filesystem:
adapter: # create a ftp filesystem
class: \AmazonS3Adapter
param:
amazon:
class: \AmazonS3
param:
options:
key: <AMAZON-ACCESS-KEY>
secret: <AMAZON-ACCESS-SECRET>
bucket: <BUCKET-NAME>
options:
region: s3-eu-west-1.amazonaws.com
cache: # cache it locally
class: \Gaufrette\Adapter\Local
param:
destination: %SF_CACHE_DIR%/gaufrette/cache
ttl: 3600
url_resolver: # and define the URL resolver
class: sfPrefixUrlResolver
param:
prefix: http://<bucket-name>.s3-website-eu-west-1.amazonaws.com/
The previous configuration creates a gaufrette named my_filesystem and exposes it through the context:
<?php
// myAction.class.php
$myFilesystem = $this->getContext()->getGaufrette('my_filesystem');
$url = $myFilesystem->getUrl('my_resource_key');
$content = $myFilesystem->read('my/file.ext');
$myFilesystem->write('my/file.ext.bck', $content);
Once you have retrieved the gaufrette, you can use it as described in the Gaufrette README.
The URL resolvers are the entities that map files to URL. There are currently two of them included in the plugin:
sfPrefixUrlResolver
: the url is the concatenation of a prefix and a filename- ̀
sfAmazonS3UrlResolver
: turns informations of a bucket and a filename into a public url
We provide a validator to use instead of the sfValidatorFile
to ease the
integration in forms. See the
sfGaufretteFileValidator
class for more information.
Install the behavior in your plugins directory:
git submodule add git://github.com/Carpe-Hora/sfGaufrettePlugin.git
git submodule update --init --recursive
As Gaufrette is bundled with the plugin, you have to initialize the submodules, at least for the sfGaufrettePlugin.
And now, enable the plugin in your project's configuration.
<?php
// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(array(
// ...
'sfGaufrettePlugin',
// ...
));
}
}
sfGaufrettePlugin is released under the MIT License. See the bundled LICENSE file for details.