Maslosoft Cache API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php
/**
* This software package is licensed under New BSD license.
*
* @package maslosoft/cache
* @licence New BSD
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
*/
namespace Maslosoft\Cache\Helpers;
use Maslosoft\Cache\Interfaces\CacheInterface;
use Maslosoft\Cache\Interfaces\CacheAdapterInterface;
use Maslosoft\Cache\Interfaces\ChildCacheInterface;
use Maslosoft\EmbeDi\EmbeDi;
/**
* AdapterFactory
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class AdapterFactory
{
/**
* Create first available adapter based on configuration
* @param mixed[] $adapters
* @return CacheAdapterInterface
*/
public static function create($adapters, CacheInterface $parent = null)
{
foreach (self::createOne($adapters, $parent) as $adapter)
{
return $adapter;
}
}
/**
* Create all available adapters based on configuration
* @param mixed[] $adapters
* @return CacheAdapterInterface[] Key is class name
*/
public static function createAll($adapters, CacheInterface $parent = null)
{
$result = [];
foreach (self::createOne($adapters, $parent) as $adapter)
{
$key = get_class($adapter);
$result[$key] = $adapter;
}
return $result;
}
private static function createOne($adapters, CacheInterface $parent = null)
{
foreach ($adapters as $className => $config)
{
if (!$config)
{
continue;
}
$adapter = new $className;
/* @var $adapter CacheAdapterInterface */
if ($adapter->isAvailable())
{
if (is_array($config))
{
(new EmbeDi())->apply($config, $adapter);
}
// Parent cache config
if ($parent && $adapter instanceof ChildCacheInterface)
{
$adapter->setParent($parent);
}
yield $adapter;
}
}
}
}
API documentation generated by ApiGen