Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #19 from arjanvdbos/add-cache-chain-support
Browse files Browse the repository at this point in the history
Added support for cache chains with multiple cache providers
  • Loading branch information
asgrim authored Apr 3, 2017
2 parents ab97e2e + 682bc9d commit be47616
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example/full-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
'class' => \Doctrine\Common\Cache\ZendDataCache::class,
'namespace' => 'container-interop-doctrine',
],
'chain' => [
'class' => \Doctrine\Common\Cache\ChainCache::class,
'providers' => ['array', 'redis'], // you can use any provider listed above
'namespace' => 'container-interop-doctrine', // will be applied to all providers in the chain
],
],
'types' => [],
],
Expand Down
18 changes: 18 additions & 0 deletions src/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\ChainCache;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\Common\Cache\MemcacheCache;
use Doctrine\Common\Cache\MemcachedCache;
Expand Down Expand Up @@ -55,6 +56,16 @@ protected function createWithConfig(ContainerInterface $container, $configKey)
$cache = new $config['class']($instance);
break;

case ChainCache::class:
$providers = array_map(
function ($provider) use ($container) {
return $this->createWithConfig($container, $provider);
},
is_array($config['providers']) ? $config['providers'] : []
);
$cache = new $config['class']($providers);
break;

default:
$cache = new $config['class']();
}
Expand Down Expand Up @@ -144,6 +155,13 @@ protected function getDefaultConfig($configKey)
'class' => ZendDataCache::class,
'namespace' => 'container-interop-doctrine',
];

case 'chain':
return [
'class' => ChainCache::class,
'namespace' => 'container-interop-doctrine',
'providers' => [],
];
}

return [];
Expand Down
25 changes: 25 additions & 0 deletions test/CacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use ContainerInteropDoctrine\AbstractFactory;
use ContainerInteropDoctrine\CacheFactory;
use Doctrine\Common\Cache\ChainCache;
use Doctrine\Common\Cache\FilesystemCache;
use Interop\Container\ContainerInterface;
use PHPUnit_Framework_TestCase;
Expand Down Expand Up @@ -56,4 +57,28 @@ public function testFileSystemCacheConstructor()

$this->assertInstanceOf(FilesystemCache::class, $cacheInstance);
}

public function testCacheChainContainsInitializedProviders()
{
$config = [
'doctrine' => [
'cache' => [
'chain' => [
'class' => ChainCache::class,
'providers' => ['array', 'array'],
],
],
],
];

$container = $this->prophesize(ContainerInterface::class);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn($config);

$factory = new CacheFactory('chain');
$cacheInstance = $factory($container->reveal());

$this->assertInstanceOf(ChainCache::class, $cacheInstance);
$this->assertAttributeCount(2, 'cacheProviders', $cacheInstance);
}
}

0 comments on commit be47616

Please sign in to comment.