Файл: concrete5.7.5.6/concrete/vendor/zendframework/zend-cache/src/StorageFactory.php
Строк: 305
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendCache;
use Traversable;
use ZendStdlibArrayUtils;
abstract class StorageFactory
{
/**
* Plugin manager for loading adapters
*
* @var null|StorageAdapterPluginManager
*/
protected static $adapters = null;
/**
* Plugin manager for loading plugins
*
* @var null|StoragePluginManager
*/
protected static $plugins = null;
/**
* The storage factory
* This can instantiate storage adapters and plugins.
*
* @param array|Traversable $cfg
* @return StorageStorageInterface
* @throws ExceptionInvalidArgumentException
*/
public static function factory($cfg)
{
if ($cfg instanceof Traversable) {
$cfg = ArrayUtils::iteratorToArray($cfg);
}
if (!is_array($cfg)) {
throw new ExceptionInvalidArgumentException(
'The factory needs an associative array '
. 'or a Traversable object as an argument'
);
}
// instantiate the adapter
if (!isset($cfg['adapter'])) {
throw new ExceptionInvalidArgumentException('Missing "adapter"');
}
$adapterName = $cfg['adapter'];
$adapterOptions = array();
if (is_array($cfg['adapter'])) {
if (!isset($cfg['adapter']['name'])) {
throw new ExceptionInvalidArgumentException('Missing "adapter.name"');
}
$adapterName = $cfg['adapter']['name'];
$adapterOptions = isset($cfg['adapter']['options']) ? $cfg['adapter']['options'] : array();
}
if (isset($cfg['options'])) {
$adapterOptions = array_merge($adapterOptions, $cfg['options']);
}
$adapter = static::adapterFactory((string) $adapterName, $adapterOptions);
// add plugins
if (isset($cfg['plugins'])) {
if (!is_array($cfg['plugins'])) {
throw new ExceptionInvalidArgumentException(
'Plugins needs to be an array'
);
}
foreach ($cfg['plugins'] as $k => $v) {
$pluginPrio = 1; // default priority
if (is_string($k)) {
if (!is_array($v)) {
throw new ExceptionInvalidArgumentException(
"'plugins.{$k}' needs to be an array"
);
}
$pluginName = $k;
$pluginOptions = $v;
} elseif (is_array($v)) {
if (!isset($v['name'])) {
throw new ExceptionInvalidArgumentException("Invalid plugins[{$k}] or missing plugins[{$k}].name");
}
$pluginName = (string) $v['name'];
if (isset($v['options'])) {
$pluginOptions = $v['options'];
} else {
$pluginOptions = array();
}
if (isset($v['priority'])) {
$pluginPrio = $v['priority'];
}
} else {
$pluginName = $v;
$pluginOptions = array();
}
$plugin = static::pluginFactory($pluginName, $pluginOptions);
$adapter->addPlugin($plugin, $pluginPrio);
}
}
return $adapter;
}
/**
* Instantiate a storage adapter
*
* @param string|StorageStorageInterface $adapterName
* @param array|Traversable|StorageAdapterAdapterOptions $options
* @return StorageStorageInterface
* @throws ExceptionRuntimeException
*/
public static function adapterFactory($adapterName, $options = array())
{
if ($adapterName instanceof StorageStorageInterface) {
// $adapterName is already an adapter object
$adapter = $adapterName;
} else {
$adapter = static::getAdapterPluginManager()->get($adapterName);
}
if ($options) {
$adapter->setOptions($options);
}
return $adapter;
}
/**
* Get the adapter plugin manager
*
* @return StorageAdapterPluginManager
*/
public static function getAdapterPluginManager()
{
if (static::$adapters === null) {
static::$adapters = new StorageAdapterPluginManager();
}
return static::$adapters;
}
/**
* Change the adapter plugin manager
*
* @param StorageAdapterPluginManager $adapters
* @return void
*/
public static function setAdapterPluginManager(StorageAdapterPluginManager $adapters)
{
static::$adapters = $adapters;
}
/**
* Resets the internal adapter plugin manager
*
* @return void
*/
public static function resetAdapterPluginManager()
{
static::$adapters = null;
}
/**
* Instantiate a storage plugin
*
* @param string|StoragePluginPluginInterface $pluginName
* @param array|Traversable|StoragePluginPluginOptions $options
* @return StoragePluginPluginInterface
* @throws ExceptionRuntimeException
*/
public static function pluginFactory($pluginName, $options = array())
{
if ($pluginName instanceof StoragePluginPluginInterface) {
// $pluginName is already an plugin object
$plugin = $pluginName;
} else {
$plugin = static::getPluginManager()->get($pluginName);
}
if (!$options instanceof StoragePluginPluginOptions) {
$options = new StoragePluginPluginOptions($options);
}
if ($options) {
$plugin->setOptions($options);
}
return $plugin;
}
/**
* Get the plugin manager
*
* @return StoragePluginManager
*/
public static function getPluginManager()
{
if (static::$plugins === null) {
static::$plugins = new StoragePluginManager();
}
return static::$plugins;
}
/**
* Change the plugin manager
*
* @param StoragePluginManager $plugins
* @return void
*/
public static function setPluginManager(StoragePluginManager $plugins)
{
static::$plugins = $plugins;
}
/**
* Resets the internal plugin manager
*
* @return void
*/
public static function resetPluginManager()
{
static::$plugins = null;
}
}