Merge pull request #20 from l3l0/cache-adapter

Cache adapter
This commit is contained in:
Leszek Prabucki 2012-09-05 12:26:33 -07:00
commit 805c4342a2
9 changed files with 157 additions and 21 deletions

View File

@ -9,4 +9,4 @@ before_script:
- php composer.phar install --dev - php composer.phar install --dev
script: script:
- phpunit - phpunit --coverage-text

View File

@ -0,0 +1,53 @@
<?php
namespace Knp\Bundle\GaufretteBundle\DependencyInjection\Factory;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
/**
* Cache adapter factory
*
* @author Robin van der Vleuten <robinvdvleuten@gmail.com>
*/
class CacheAdapterFactory implements AdapterFactoryInterface
{
/**
* {@inheritDoc}
*/
public function create(ContainerBuilder $container, $id, array $config)
{
$container
->setDefinition($id, new DefinitionDecorator('knp_gaufrette.adapter.cache'))
->addArgument(new Reference('gaufrette.' . $config['source'] . '_adapter'))
->addArgument(new Reference('gaufrette.' . $config['cache'] . '_adapter'))
->addArgument($config['ttl'])
->addArgument($config['serialize'] ? new Reference('gaufrette.' . $config['serialize'] . '_adapter') : null)
;
}
/**
* {@inheritDoc}
*/
public function getKey()
{
return 'cache';
}
/**
* {@inheritDoc}
*/
public function addConfiguration(NodeDefinition $node)
{
$node
->children()
->scalarNode('source')->isRequired()->cannotBeEmpty()->end()
->scalarNode('cache')->isRequired()->cannotBeEmpty()->end()
->scalarNode('ttl')->defaultValue(0)->end()
->scalarNode('serialize')->defaultNull()->end()
->end()
;
}
}

View File

@ -34,6 +34,10 @@ you must add the following lines to it:
git=http://github.com/KnpLabs/KnpGaufretteBundle.git git=http://github.com/KnpLabs/KnpGaufretteBundle.git
target=/bundles/Knp/Bundle/GaufretteBundle target=/bundles/Knp/Bundle/GaufretteBundle
### Composer Style
Bundle can be installed using composer by add to require `composer.json` part `"knplabs/knp-gaufrette-bundle": "dev-master"` line.
### Git Submodule Style ### Git Submodule Style
If you are versioning your project with git, you had better to embed it If you are versioning your project with git, you had better to embed it
@ -41,9 +45,10 @@ as a submodule:
$ git submodule add https://github.com/KnpLabs/KnpGaufretteBundle.git vendor/bundles/Knp/Bundle/GaufretteBundle $ git submodule add https://github.com/KnpLabs/KnpGaufretteBundle.git vendor/bundles/Knp/Bundle/GaufretteBundle
## Add the namespace in the autoloader ## Add the namespace in the autoloader
You must register both Gaufrette and the KnpGaufretteBundle in your autoloader: You must register both Gaufrette and the KnpGaufretteBundle in your autoloader:
(You do not have to do that if you are using composer autoload system.)
``` php ``` php
<?php <?php
@ -338,3 +343,42 @@ knp_gaufrette:
prefix: APC 'namespace' prefix prefix: APC 'namespace' prefix
ttl: 0 ttl: 0
``` ```
## Cache
Adapter which allow to cache other adapters
### Parameters
* `source` The source adapter that must be cached *(required)*
* `cache` The adapter used to cache the source *(required)*
* `ttl` Time to live *(default 0)*
* `serializer` The adapter used to cache serializations *(default null)*
### Example
``` yaml
# 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
```

View File

@ -35,6 +35,9 @@
<service id="knp_gaufrette.adapter.factory.apc" class="Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\ApcAdapterFactory"> <service id="knp_gaufrette.adapter.factory.apc" class="Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\ApcAdapterFactory">
<tag name="gaufrette.adapter.factory" /> <tag name="gaufrette.adapter.factory" />
</service> </service>
<service id="knp_gaufrette.adapter.factory.cache" class="Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\CacheAdapterFactory">
<tag name="gaufrette.adapter.factory" />
</service>
</services> </services>

View File

@ -34,6 +34,7 @@
<argument /><!-- prefix --> <argument /><!-- prefix -->
<argument /><!-- ttl --> <argument /><!-- ttl -->
</service> </service>
<service id="knp_gaufrette.adapter.cache" class="Gaufrette\Adapter\Cache" abstract="true" public="false" />
<service id="knp_gaufrette.filesystem_map" class="%knp_gaufrette.filesystem_map.class%"> <service id="knp_gaufrette.filesystem_map" class="%knp_gaufrette.filesystem_map.class%">
<argument /> <!-- map of filesystems --> <argument /> <!-- map of filesystems -->
</service> </service>

View File

@ -7,6 +7,7 @@ use Symfony\Component\HttpKernel\Util\Filesystem;
class ConfigurationTest extends \PHPUnit_Framework_TestCase class ConfigurationTest extends \PHPUnit_Framework_TestCase
{ {
private $cacheDir; private $cacheDir;
private $kernel;
public function setUp() public function setUp()
{ {
@ -17,6 +18,9 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
} }
mkdir($this->cacheDir, 0777, true); mkdir($this->cacheDir, 0777, true);
$this->kernel = new TestKernel('test', false);
$this->kernel->boot();
} }
public function tearDown() public function tearDown()
@ -32,12 +36,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
*/ */
public function shouldAllowForFilesystemAlias() public function shouldAllowForFilesystemAlias()
{ {
$kernel = new TestKernel('test', false); $this->assertInstanceOf('Gaufrette\Filesystem', $this->kernel->getContainer()->get('foo_filesystem'));
$kernel->boot();
$container = $kernel->getContainer();
$this->assertInstanceOf('Gaufrette\Filesystem', $container->get('foo_filesystem'));
} }
/** /**
@ -57,12 +56,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
*/ */
public function shouldAllowAccessToAllPublicServices() public function shouldAllowAccessToAllPublicServices()
{ {
$kernel = new TestKernel('dev', false); $this->assertInstanceOf('Knp\Bundle\GaufretteBundle\FilesystemMap', $this->kernel->getContainer()->get('knp_gaufrette.filesystem_map'));
$kernel->boot();
$container = $kernel->getContainer();
$this->assertInstanceOf('Gaufrette\Filesystem', $container->get('foo_filesystem'));
$this->assertInstanceOf('Knp\Bundle\GaufretteBundle\FilesystemMap', $container->get('knp_gaufrette.filesystem_map'));
} }
/** /**
@ -70,10 +64,30 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
*/ */
public function shouldAllowAccessToFilesystemThoughFilesystemMap() public function shouldAllowAccessToFilesystemThoughFilesystemMap()
{ {
$kernel = new TestKernel('test', false); $this->assertInstanceOf('Gaufrette\Filesystem', $this->kernel->getContainer()->get('knp_gaufrette.filesystem_map')->get('foo'));
$kernel->boot(); }
$container = $kernel->getContainer(); /**
$this->assertInstanceOf('Gaufrette\Filesystem', $container->get('knp_gaufrette.filesystem_map')->get('foo')); * @test
*/
public function shouldAllowAccessToLocalFilesystem()
{
$this->assertInstanceOf('Gaufrette\Adapter\Local', $this->kernel->getContainer()->get('foo_filesystem')->getAdapter());
}
/**
* @test
*/
public function shouldAllowAccessToCacheFilesystem()
{
$this->assertInstanceOf('Gaufrette\Adapter\Cache', $this->kernel->getContainer()->get('cache_filesystem')->getAdapter());
}
/**
* @test
*/
public function shouldAllowAccessToFtpFilesystem()
{
$this->assertInstanceOf('Gaufrette\Adapter\Ftp', $this->kernel->getContainer()->get('ftp_filesystem')->getAdapter());
} }
} }

View File

@ -1,11 +1,30 @@
knp_gaufrette: knp_gaufrette:
adapters: adapters:
foo: foo_local:
local: local:
directory: %kernel.root_dir% directory: %kernel.root_dir%
create: true create: true
foo_ftp:
ftp:
host: example.com
username: user
password: pass
directory: /example/ftp
create: true
mode: FTP_BINARY
foo_cache:
cache:
source: foo_ftp
cache: foo_local
ttl: 7200
filesystems: filesystems:
foo: foo:
adapter: foo adapter: foo_local
alias: foo_filesystem alias: foo_filesystem
cache:
adapter: foo_cache
alias: cache_filesystem
ftp:
adapter: foo_ftp
alias: ftp_filesystem

View File

@ -20,7 +20,8 @@
"knplabs/gaufrette": "*" "knplabs/gaufrette": "*"
}, },
"require-dev": { "require-dev": {
"symfony/yaml": "2.*" "symfony/yaml": "2.*",
"symfony/console": "2.*"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {

View File

@ -23,6 +23,7 @@
<exclude> <exclude>
<directory>./Resources</directory> <directory>./Resources</directory>
<directory>./Tests</directory> <directory>./Tests</directory>
<directory>./vendor</directory>
</exclude> </exclude>
</whitelist> </whitelist>
</filter> </filter>