Improved CacheFactory so that adapter can be set in config

This commit is contained in:
Alejandro Celaya
2016-08-01 20:16:13 +02:00
parent 30988b10d1
commit 7b98527f2e
3 changed files with 49 additions and 3 deletions

View File

@@ -11,6 +11,11 @@ use Zend\ServiceManager\Factory\FactoryInterface;
class CacheFactory implements FactoryInterface
{
const VALID_CACHE_ADAPTERS = [
ApcuCache::class,
ArrayCache::class,
];
/**
* Create an object
*
@@ -25,6 +30,16 @@ class CacheFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return env('APP_ENV', 'dev') === 'pro' ? new ApcuCache() : new ArrayCache();
// Try to get the adapter from config
$config = $container->get('config');
if (isset($config['cache'])
&& isset($config['cache']['adapter'])
&& in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)
) {
return new $config['cache']['adapter']();
}
// If the adapter has not been set in config, create one based on environment
return env('APP_ENV', 'pro') === 'pro' ? new ApcuCache() : new ArrayCache();
}
}