diff --git a/example/full-config.php b/example/full-config.php index a6fab39..0adbfee 100644 --- a/example/full-config.php +++ b/example/full-config.php @@ -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' => [], ], diff --git a/src/CacheFactory.php b/src/CacheFactory.php index 3a07bba..1859a6b 100644 --- a/src/CacheFactory.php +++ b/src/CacheFactory.php @@ -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; @@ -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'](); } @@ -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 []; diff --git a/test/CacheFactoryTest.php b/test/CacheFactoryTest.php index 79f886b..52f6185 100644 --- a/test/CacheFactoryTest.php +++ b/test/CacheFactoryTest.php @@ -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; @@ -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); + } }