Serializer.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-2015 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-2015 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. // ZF-8842:
  63. // check that the loaded class implements Zend_Serializer_Adapter_AdapterInterface without execute code
  64. if (!in_array('Zend_Serializer_Adapter_AdapterInterface', class_implements($adapterClass))) {
  65. require_once 'Zend/Serializer/Exception.php';
  66. throw new Zend_Serializer_Exception('The serializer adapter class "'.$adapterClass.'" must implement Zend_Serializer_Adapter_AdapterInterface');
  67. }
  68. return new $adapterClass($opts);
  69. }
  70. /**
  71. * Get the adapter plugin loader.
  72. *
  73. * @return Zend_Loader_PluginLoader
  74. */
  75. public static function getAdapterLoader()
  76. {
  77. if (self::$_adapterLoader === null) {
  78. self::$_adapterLoader = self::_getDefaultAdapterLoader();
  79. }
  80. return self::$_adapterLoader;
  81. }
  82. /**
  83. * Change the adapter plugin load.
  84. *
  85. * @param Zend_Loader_PluginLoader $pluginLoader
  86. * @return void
  87. */
  88. public static function setAdapterLoader(Zend_Loader_PluginLoader $pluginLoader)
  89. {
  90. self::$_adapterLoader = $pluginLoader;
  91. }
  92. /**
  93. * Resets the internal adapter plugin loader
  94. *
  95. * @return Zend_Loader_PluginLoader
  96. */
  97. public static function resetAdapterLoader()
  98. {
  99. self::$_adapterLoader = self::_getDefaultAdapterLoader();
  100. return self::$_adapterLoader;
  101. }
  102. /**
  103. * Returns a default adapter plugin loader
  104. *
  105. * @return Zend_Loader_PluginLoader
  106. */
  107. protected static function _getDefaultAdapterLoader()
  108. {
  109. $loader = new Zend_Loader_PluginLoader();
  110. $loader->addPrefixPath('Zend_Serializer_Adapter', dirname(__FILE__).'/Serializer/Adapter');
  111. return $loader;
  112. }
  113. /**
  114. * Change the default adapter.
  115. *
  116. * @param string|Zend_Serializer_Adapter_AdapterInterface $adapter
  117. * @param array|Zend_Config $options
  118. */
  119. public static function setDefaultAdapter($adapter, $options = array())
  120. {
  121. self::$_defaultAdapter = self::factory($adapter, $options);
  122. }
  123. /**
  124. * Get the default adapter.
  125. *
  126. * @return Zend_Serializer_Adapter_AdapterInterface
  127. */
  128. public static function getDefaultAdapter()
  129. {
  130. if (!self::$_defaultAdapter instanceof Zend_Serializer_Adapter_AdapterInterface) {
  131. self::setDefaultAdapter(self::$_defaultAdapter);
  132. }
  133. return self::$_defaultAdapter;
  134. }
  135. /**
  136. * Generates a storable representation of a value using the default adapter.
  137. *
  138. * @param mixed $value
  139. * @param array $options
  140. * @return string
  141. * @throws Zend_Serializer_Exception
  142. */
  143. public static function serialize($value, array $options = array())
  144. {
  145. if (isset($options['adapter'])) {
  146. $adapter = self::factory($options['adapter']);
  147. unset($options['adapter']);
  148. } else {
  149. $adapter = self::getDefaultAdapter();
  150. }
  151. return $adapter->serialize($value, $options);
  152. }
  153. /**
  154. * Creates a PHP value from a stored representation using the default adapter.
  155. *
  156. * @param string $serialized
  157. * @param array $options
  158. * @return mixed
  159. * @throws Zend_Serializer_Exception
  160. */
  161. public static function unserialize($serialized, array $options = array())
  162. {
  163. if (isset($options['adapter'])) {
  164. $adapter = self::factory($options['adapter']);
  165. unset($options['adapter']);
  166. } else {
  167. $adapter = self::getDefaultAdapter();
  168. }
  169. return $adapter->unserialize($serialized, $options);
  170. }
  171. }