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

Commit

Permalink
Added support for cache chains with multiple cache providers
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanvdbos committed Mar 30, 2017
1 parent ab97e2e commit 104015a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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);
},
$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 104015a

Please sign in to comment.