3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
13 use Zend\Stdlib\ArrayUtils
;
15 abstract class StorageFactory
18 * Plugin manager for loading adapters
20 * @var null|Storage\AdapterPluginManager
22 protected static $adapters = null;
25 * Plugin manager for loading plugins
27 * @var null|Storage\PluginManager
29 protected static $plugins = null;
33 * This can instantiate storage adapters and plugins.
35 * @param array|Traversable $cfg
36 * @return Storage\StorageInterface
37 * @throws Exception\InvalidArgumentException
39 public static function factory($cfg)
41 if ($cfg instanceof Traversable
) {
42 $cfg = ArrayUtils
::iteratorToArray($cfg);
45 if (!is_array($cfg)) {
46 throw new Exception\
InvalidArgumentException(
47 'The factory needs an associative array '
48 . 'or a Traversable object as an argument'
52 // instantiate the adapter
53 if (!isset($cfg['adapter'])) {
54 throw new Exception\
InvalidArgumentException('Missing "adapter"');
56 $adapterName = $cfg['adapter'];
57 $adapterOptions = array();
58 if (is_array($cfg['adapter'])) {
59 if (!isset($cfg['adapter']['name'])) {
60 throw new Exception\
InvalidArgumentException('Missing "adapter.name"');
63 $adapterName = $cfg['adapter']['name'];
64 $adapterOptions = isset($cfg['adapter']['options']) ?
$cfg['adapter']['options'] : array();
66 if (isset($cfg['options'])) {
67 $adapterOptions = array_merge($adapterOptions, $cfg['options']);
70 $adapter = static::adapterFactory((string) $adapterName, $adapterOptions);
73 if (isset($cfg['plugins'])) {
74 if (!is_array($cfg['plugins'])) {
75 throw new Exception\
InvalidArgumentException(
76 'Plugins needs to be an array'
80 foreach ($cfg['plugins'] as $k => $v) {
81 $pluginPrio = 1; // default priority
85 throw new Exception\
InvalidArgumentException(
86 "'plugins.{$k}' needs to be an array"
91 } elseif (is_array($v)) {
92 if (!isset($v['name'])) {
93 throw new Exception\
InvalidArgumentException("Invalid plugins[{$k}] or missing plugins[{$k}].name");
95 $pluginName = (string) $v['name'];
97 if (isset($v['options'])) {
98 $pluginOptions = $v['options'];
100 $pluginOptions = array();
103 if (isset($v['priority'])) {
104 $pluginPrio = $v['priority'];
108 $pluginOptions = array();
111 $plugin = static::pluginFactory($pluginName, $pluginOptions);
112 $adapter->addPlugin($plugin, $pluginPrio);
120 * Instantiate a storage adapter
122 * @param string|Storage\StorageInterface $adapterName
123 * @param array|Traversable|Storage\Adapter\AdapterOptions $options
124 * @return Storage\StorageInterface
125 * @throws Exception\RuntimeException
127 public static function adapterFactory($adapterName, $options = array())
129 if ($adapterName instanceof Storage\StorageInterface
) {
130 // $adapterName is already an adapter object
131 $adapter = $adapterName;
133 $adapter = static::getAdapterPluginManager()->get($adapterName);
137 $adapter->setOptions($options);
144 * Get the adapter plugin manager
146 * @return Storage\AdapterPluginManager
148 public static function getAdapterPluginManager()
150 if (static::$adapters === null) {
151 static::$adapters = new Storage\
AdapterPluginManager();
153 return static::$adapters;
157 * Change the adapter plugin manager
159 * @param Storage\AdapterPluginManager $adapters
162 public static function setAdapterPluginManager(Storage\AdapterPluginManager
$adapters)
164 static::$adapters = $adapters;
168 * Resets the internal adapter plugin manager
172 public static function resetAdapterPluginManager()
174 static::$adapters = null;
178 * Instantiate a storage plugin
180 * @param string|Storage\Plugin\PluginInterface $pluginName
181 * @param array|Traversable|Storage\Plugin\PluginOptions $options
182 * @return Storage\Plugin\PluginInterface
183 * @throws Exception\RuntimeException
185 public static function pluginFactory($pluginName, $options = array())
187 if ($pluginName instanceof Storage\Plugin\PluginInterface
) {
188 // $pluginName is already an plugin object
189 $plugin = $pluginName;
191 $plugin = static::getPluginManager()->get($pluginName);
194 if (!$options instanceof Storage\Plugin\PluginOptions
) {
195 $options = new Storage\Plugin\
PluginOptions($options);
199 $plugin->setOptions($options);
206 * Get the plugin manager
208 * @return Storage\PluginManager
210 public static function getPluginManager()
212 if (static::$plugins === null) {
213 static::$plugins = new Storage\
PluginManager();
215 return static::$plugins;
219 * Change the plugin manager
221 * @param Storage\PluginManager $plugins
224 public static function setPluginManager(Storage\PluginManager
$plugins)
226 static::$plugins = $plugins;
230 * Resets the internal plugin manager
234 public static function resetPluginManager()
236 static::$plugins = null;