Serializer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $adapterReflection = new ReflectionClass($adapterClass);
  63. if (!$adapterReflection->implementsInterface('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 new $adapterClass($opts);
  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. $loader = new Zend_Loader_PluginLoader();
  78. $loader->addPrefixPath('Zend_Serializer_Adapter', dirname(__FILE__).'/Serializer/Adapter');
  79. self::$_adapterLoader = $loader;
  80. }
  81. return self::$_adapterLoader;
  82. }
  83. /**
  84. * Change the adapter plugin load.
  85. *
  86. * @param Zend_Loader_PluginLoader $pluginLoader
  87. * @return void
  88. */
  89. public static function setAdapterLoader(Zend_Loader_PluginLoader $pluginLoader)
  90. {
  91. self::$_adapterLoader = $pluginLoader;
  92. }
  93. /**
  94. * Change the default adapter.
  95. *
  96. * @param string|Zend_Serializer_Adapter_AdapterInterface $adapter
  97. * @param array|Zend_Config $options
  98. */
  99. public static function setDefaultAdapter($adapter, $options = array())
  100. {
  101. self::$_defaultAdapter = self::factory($adapter, $options);
  102. }
  103. /**
  104. * Get the default adapter.
  105. *
  106. * @return Zend_Serializer_Adapter_AdapterInterface
  107. */
  108. public static function getDefaultAdapter()
  109. {
  110. if (!self::$_defaultAdapter instanceof Zend_Serializer_Adapter_AdapterInterface) {
  111. self::setDefaultAdapter(self::$_defaultAdapter);
  112. }
  113. return self::$_defaultAdapter;
  114. }
  115. /**
  116. * Generates a storable representation of a value using the default adapter.
  117. *
  118. * @param mixed $value
  119. * @param array $options
  120. * @return string
  121. * @throws Zend_Serializer_Exception
  122. */
  123. public static function serialize($value, array $options = array())
  124. {
  125. if (isset($options['adapter'])) {
  126. $adapter = self::factory($options['adapter']);
  127. unset($options['adapter']);
  128. } else {
  129. $adapter = self::getDefaultAdapter();
  130. }
  131. return $adapter->serialize($value, $options);
  132. }
  133. /**
  134. * Creates a PHP value from a stored representation using the default adapter.
  135. *
  136. * @param string $serialized
  137. * @param array $options
  138. * @return mixed
  139. * @throws Zend_Serializer_Exception
  140. */
  141. public static function unserialize($serialized, array $options = array())
  142. {
  143. if (isset($options['adapter'])) {
  144. $adapter = self::factory($options['adapter']);
  145. unset($options['adapter']);
  146. } else {
  147. $adapter = self::getDefaultAdapter();
  148. }
  149. return $adapter->unserialize($serialized, $options);
  150. }
  151. }