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

Added support for cache chains with multiple cache providers #19

Merged
merged 4 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}