Source of file StorageFactory.php
Size: 7,344 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Cache/StorageFactory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 | <?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 Zend\Cache; use Traversable; use Zend\EventManager\EventsCapableInterface; use Zend\Stdlib\ArrayUtils; abstract class StorageFactory { /** * Plugin manager for loading adapters * * @var null|Storage\AdapterPluginManager */ protected static $adapters = null; /** * Plugin manager for loading plugins * * @var null|Storage\PluginManager */ protected static $plugins = null; /** * The storage factory * This can instantiate storage adapters and plugins. * * @param array|Traversable $cfg * @return Storage\StorageInterface * @throws Exception\InvalidArgumentException */ public static function factory($cfg) { if ($cfg instanceof Traversable) { $cfg = ArrayUtils::iteratorToArray($cfg); } if (!is_array($cfg)) { throw new Exception\InvalidArgumentException( 'The factory needs an associative array ' . 'or a Traversable object as an argument' ); } // instantiate the adapter if (!isset($cfg['adapter'])) { throw new Exception\InvalidArgumentException('Missing "adapter"'); } $adapterName = $cfg['adapter']; $adapterOptions = array(); if (is_array($cfg['adapter'])) { if (!isset($cfg['adapter']['name'])) { throw new Exception\InvalidArgumentException('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 (!$adapter instanceof EventsCapableInterface) { throw new Exception\RuntimeException(sprintf( "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", get_class($adapter), 'Zend\EventManager\EventsCapableInterface' )); } if (!is_array($cfg['plugins'])) { throw new Exception\InvalidArgumentException( '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 Exception\InvalidArgumentException( "'plugins.{$k}' needs to be an array" ); } $pluginName = $k; $pluginOptions = $v; } elseif (is_array($v)) { if (!isset($v['name'])) { throw new Exception\InvalidArgumentException("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|Storage\StorageInterface $adapterName * @param array|Traversable|Storage\Adapter\AdapterOptions $options * @return Storage\StorageInterface * @throws Exception\RuntimeException */ public static function adapterFactory($adapterName, $options = array()) { if ($adapterName instanceof Storage\StorageInterface) { // $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 Storage\AdapterPluginManager */ public static function getAdapterPluginManager() { if (static::$adapters === null) { static::$adapters = new Storage\AdapterPluginManager(); } return static::$adapters; } /** * Change the adapter plugin manager * * @param Storage\AdapterPluginManager $adapters * @return void */ public static function setAdapterPluginManager(Storage\AdapterPluginManager $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|Storage\Plugin\PluginInterface $pluginName * @param array|Traversable|Storage\Plugin\PluginOptions $options * @return Storage\Plugin\PluginInterface * @throws Exception\RuntimeException */ public static function pluginFactory($pluginName, $options = array()) { if ($pluginName instanceof Storage\Plugin\PluginInterface) { // $pluginName is already a plugin object $plugin = $pluginName; } else { $plugin = static::getPluginManager()->get($pluginName); } if (!$options instanceof Storage\Plugin\PluginOptions) { $options = new Storage\Plugin\PluginOptions($options); } if ($options) { $plugin->setOptions($options); } return $plugin; } /** * Get the plugin manager * * @return Storage\PluginManager */ public static function getPluginManager() { if (static::$plugins === null) { static::$plugins = new Storage\PluginManager(); } return static::$plugins; } /** * Change the plugin manager * * @param Storage\PluginManager $plugins * @return void */ public static function setPluginManager(Storage\PluginManager $plugins) { static::$plugins = $plugins; } /** * Resets the internal plugin manager * * @return void */ public static function resetPluginManager() { static::$plugins = null; } } |