Serializer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  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
  19. * @version $Id$
  20. */
  21. /** @see Zend_Loader_PluginLoader */
  22. require_once 'Zend/Loader/PluginLoader.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Serializer
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Serializer
  30. {
  31. /**
  32. * Plugin loader to load adapter.
  33. *
  34. * @var null|Zend_Loader_PluginLoader
  35. */
  36. private static $_adapterLoader = null;
  37. /**
  38. * The default adapter.
  39. *
  40. * @var string|Zend_Serializer_AdapterInterface
  41. */
  42. protected static $_defaultAdapter = 'PhpSerialize';
  43. /**
  44. * Create a serializer adapter instance.
  45. *
  46. * @param string|Zend_Serializer_Adapter_AdapterInterface $adapterName Name of the adapter class
  47. * @param array |Zend_Config $opts Serializer options
  48. * @return Zend_Serializer_Adapter_AdapterInterface
  49. */
  50. public static function factory($adapterName, $opts = array())
  51. {
  52. if ($adapterName instanceof Zend_Serializer_Adapter_AdapterInterface) {
  53. return $adapterName; // $adapterName is already an adapter object
  54. }
  55. $adapterLoader = self::getAdapterLoader();
  56. try {
  57. $adapterClass = $adapterLoader->load($adapterName);
  58. } catch (Exception $e) {
  59. require_once 'Zend/Serializer/Exception.php';
  60. throw new Zend_Serializer_Exception('Can\'t load serializer adapter "'.$adapterName.'"', 0, $e);
  61. }
  62. $adapterObj = new $adapterClass($opts);
  63. if (!$adapterObj instanceof Zend_Serializer_Adapter_AdapterInterface) {
  64. require_once 'Zend/Serializer/Exception.php';
  65. throw new Zend_Serializer_Exception('The serializer adapter class "'.$adapterClass.'" must implement Zend_Serializer_Adapter_AdapterInterface');
  66. }
  67. return $adapterObj;
  68. }
  69. /**
  70. * Get the adapter plugin loader.
  71. *
  72. * @return Zend_Loader_PluginLoader
  73. */
  74. public static function getAdapterLoader()
  75. {
  76. if (self::$_adapterLoader === null) {
  77. self::$_adapterLoader = self::_getDefaultAdapterLoader();
  78. }
  79. return self::$_adapterLoader;
  80. }
  81. /**
  82. * Change the adapter plugin load.
  83. *
  84. * @param Zend_Loader_PluginLoader $pluginLoader
  85. * @return void
  86. */
  87. public static function setAdapterLoader(Zend_Loader_PluginLoader $pluginLoader)
  88. {
  89. self::$_adapterLoader = $pluginLoader;
  90. }
  91. /**
  92. * Resets the internal adapter plugin loader
  93. *
  94. * @return Zend_Loader_PluginLoader
  95. */
  96. public static function resetAdapterLoader()
  97. {
  98. self::$_adapterLoader = self::_getDefaultAdapterLoader();
  99. return self::$_adapterLoader;
  100. }
  101. /**
  102. * Returns a default adapter plugin loader
  103. *
  104. * @return Zend_Loader_PluginLoader
  105. */
  106. protected static function _getDefaultAdapterLoader()
  107. {
  108. $loader = new Zend_Loader_PluginLoader();
  109. $loader->addPrefixPath('Zend_Serializer_Adapter', dirname(__FILE__).'/Serializer/Adapter');
  110. return $loader;
  111. }
  112. /**
  113. * Change the default adapter.
  114. *
  115. * @param string|Zend_Serializer_Adapter_AdapterInterface $adapter
  116. * @param array|Zend_Config $options
  117. */
  118. public static function setDefaultAdapter($adapter, $options = array())
  119. {
  120. self::$_defaultAdapter = self::factory($adapter, $options);
  121. }
  122. /**
  123. * Get the default adapter.
  124. *
  125. * @return Zend_Serializer_Adapter_AdapterInterface
  126. */
  127. public static function getDefaultAdapter()
  128. {
  129. if (!self::$_defaultAdapter instanceof Zend_Serializer_Adapter_AdapterInterface) {
  130. self::setDefaultAdapter(self::$_defaultAdapter);
  131. }
  132. return self::$_defaultAdapter;
  133. }
  134. /**
  135. * Generates a storable representation of a value using the default adapter.
  136. *
  137. * @param mixed $value
  138. * @param array $options
  139. * @return string
  140. * @throws Zend_Serializer_Exception
  141. */
  142. public static function serialize($value, array $options = array())
  143. {
  144. if (isset($options['adapter'])) {
  145. $adapter = self::factory($options['adapter']);
  146. unset($options['adapter']);
  147. } else {
  148. $adapter = self::getDefaultAdapter();
  149. }
  150. return $adapter->serialize($value, $options);
  151. }
  152. /**
  153. * Creates a PHP value from a stored representation using the default adapter.
  154. *
  155. * @param string $serialized
  156. * @param array $options
  157. * @return mixed
  158. * @throws Zend_Serializer_Exception
  159. */
  160. public static function unserialize($serialized, array $options = array())
  161. {
  162. if (isset($options['adapter'])) {
  163. $adapter = self::factory($options['adapter']);
  164. unset($options['adapter']);
  165. } else {
  166. $adapter = self::getDefaultAdapter();
  167. }
  168. return $adapter->unserialize($serialized, $options);
  169. }
  170. }