7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Serializer
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** @see Zend_Loader_PluginLoader */
23 require_once 'Zend/Loader/PluginLoader.php';
27 * @package Zend_Serializer
28 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
29 * @license http://framework.zend.com/license/new-bsd New BSD License
34 * Plugin loader to load adapter.
36 * @var null|Zend_Loader_PluginLoader
38 private static $_adapterLoader = null;
41 * The default adapter.
43 * @var string|Zend_Serializer_AdapterInterface
45 protected static $_defaultAdapter = 'PhpSerialize';
48 * Create a serializer adapter instance.
50 * @param string|Zend_Serializer_Adapter_AdapterInterface $adapterName Name of the adapter class
51 * @param array |Zend_Config $opts Serializer options
52 * @return Zend_Serializer_Adapter_AdapterInterface
54 public static function factory($adapterName, $opts = array())
56 if ($adapterName instanceof Zend_Serializer_Adapter_AdapterInterface
) {
57 return $adapterName; // $adapterName is already an adapter object
60 $adapterLoader = self
::getAdapterLoader();
62 $adapterClass = $adapterLoader->load($adapterName);
63 } catch (Exception
$e) {
64 require_once 'Zend/Serializer/Exception.php';
65 throw new Zend_Serializer_Exception('Can\'t load serializer adapter "'.$adapterName.'"', 0, $e);
69 // check that the loaded class implements Zend_Serializer_Adapter_AdapterInterface without execute code
70 if (!in_array('Zend_Serializer_Adapter_AdapterInterface', class_implements($adapterClass))) {
71 require_once 'Zend/Serializer/Exception.php';
72 throw new Zend_Serializer_Exception('The serializer adapter class "'.$adapterClass.'" must implement Zend_Serializer_Adapter_AdapterInterface');
75 return new $adapterClass($opts);
79 * Get the adapter plugin loader.
81 * @return Zend_Loader_PluginLoader
83 public static function getAdapterLoader()
85 if (self
::$_adapterLoader === null) {
86 self
::$_adapterLoader = self
::_getDefaultAdapterLoader();
88 return self
::$_adapterLoader;
92 * Change the adapter plugin load.
94 * @param Zend_Loader_PluginLoader $pluginLoader
97 public static function setAdapterLoader(Zend_Loader_PluginLoader
$pluginLoader)
99 self
::$_adapterLoader = $pluginLoader;
103 * Resets the internal adapter plugin loader
105 * @return Zend_Loader_PluginLoader
107 public static function resetAdapterLoader()
109 self
::$_adapterLoader = self
::_getDefaultAdapterLoader();
110 return self
::$_adapterLoader;
114 * Returns a default adapter plugin loader
116 * @return Zend_Loader_PluginLoader
118 protected static function _getDefaultAdapterLoader()
120 $loader = new Zend_Loader_PluginLoader();
121 $loader->addPrefixPath('Zend_Serializer_Adapter', dirname(__FILE__
).'/Serializer/Adapter');
126 * Change the default adapter.
128 * @param string|Zend_Serializer_Adapter_AdapterInterface $adapter
129 * @param array|Zend_Config $options
131 public static function setDefaultAdapter($adapter, $options = array())
133 self
::$_defaultAdapter = self
::factory($adapter, $options);
137 * Get the default adapter.
139 * @return Zend_Serializer_Adapter_AdapterInterface
141 public static function getDefaultAdapter()
143 if (!self
::$_defaultAdapter instanceof Zend_Serializer_Adapter_AdapterInterface
) {
144 self
::setDefaultAdapter(self
::$_defaultAdapter);
146 return self
::$_defaultAdapter;
150 * Generates a storable representation of a value using the default adapter.
152 * @param mixed $value
153 * @param array $options
155 * @throws Zend_Serializer_Exception
157 public static function serialize($value, array $options = array())
159 if (isset($options['adapter'])) {
160 $adapter = self
::factory($options['adapter']);
161 unset($options['adapter']);
163 $adapter = self
::getDefaultAdapter();
166 return $adapter->serialize($value, $options);
170 * Creates a PHP value from a stored representation using the default adapter.
172 * @param string $serialized
173 * @param array $options
175 * @throws Zend_Serializer_Exception
177 public static function unserialize($serialized, array $options = array())
179 if (isset($options['adapter'])) {
180 $adapter = self
::factory($options['adapter']);
181 unset($options['adapter']);
183 $adapter = self
::getDefaultAdapter();
186 return $adapter->unserialize($serialized, $options);