File indexing completed on 2024-12-22 05:37:17

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Serializer
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** @see Zend_Loader_PluginLoader */
0023 // require_once 'Zend/Loader/PluginLoader.php';
0024 
0025 /**
0026  * @category   Zend
0027  * @package    Zend_Serializer
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_Serializer
0032 {
0033     /**
0034      * Plugin loader to load adapter.
0035      *
0036      * @var null|Zend_Loader_PluginLoader
0037      */
0038     private static $_adapterLoader = null;
0039 
0040     /**
0041      * The default adapter.
0042      *
0043      * @var string|Zend_Serializer_AdapterInterface
0044      */
0045     protected static $_defaultAdapter = 'PhpSerialize';
0046 
0047     /**
0048      * Create a serializer adapter instance.
0049      *
0050      * @param string|Zend_Serializer_Adapter_AdapterInterface $adapterName Name of the adapter class
0051      * @param array |Zend_Config $opts Serializer options
0052      * @return Zend_Serializer_Adapter_AdapterInterface
0053      */
0054     public static function factory($adapterName, $opts = array())
0055     {
0056         if ($adapterName instanceof Zend_Serializer_Adapter_AdapterInterface) {
0057             return $adapterName; // $adapterName is already an adapter object
0058         }
0059 
0060         $adapterLoader = self::getAdapterLoader();
0061         try {
0062             $adapterClass = $adapterLoader->load($adapterName);
0063         } catch (Exception $e) {
0064             // require_once 'Zend/Serializer/Exception.php';
0065             throw new Zend_Serializer_Exception('Can\'t load serializer adapter "'.$adapterName.'"', 0, $e);
0066         }
0067 
0068         // ZF-8842:
0069         // check that the loaded class implements Zend_Serializer_Adapter_AdapterInterface without execute code
0070         if (!in_array('Zend_Serializer_Adapter_AdapterInterface', class_implements($adapterClass))) {
0071             // require_once 'Zend/Serializer/Exception.php';
0072             throw new Zend_Serializer_Exception('The serializer adapter class "'.$adapterClass.'" must implement Zend_Serializer_Adapter_AdapterInterface');
0073         }
0074 
0075         return new $adapterClass($opts);
0076     }
0077 
0078     /**
0079      * Get the adapter plugin loader.
0080      *
0081      * @return Zend_Loader_PluginLoader
0082      */
0083     public static function getAdapterLoader()
0084     {
0085         if (self::$_adapterLoader === null) {
0086             self::$_adapterLoader = self::_getDefaultAdapterLoader();
0087         }
0088         return self::$_adapterLoader;
0089     }
0090 
0091     /**
0092      * Change the adapter plugin load.
0093      *
0094      * @param  Zend_Loader_PluginLoader $pluginLoader
0095      * @return void
0096      */
0097     public static function setAdapterLoader(Zend_Loader_PluginLoader $pluginLoader)
0098     {
0099         self::$_adapterLoader = $pluginLoader;
0100     }
0101 
0102     /**
0103      * Resets the internal adapter plugin loader
0104      *
0105      * @return Zend_Loader_PluginLoader
0106      */
0107     public static function resetAdapterLoader()
0108     {
0109         self::$_adapterLoader = self::_getDefaultAdapterLoader();
0110         return self::$_adapterLoader;
0111     }
0112 
0113     /**
0114      * Returns a default adapter plugin loader
0115      *
0116      * @return Zend_Loader_PluginLoader
0117      */
0118     protected static function _getDefaultAdapterLoader()
0119     {
0120         $loader = new Zend_Loader_PluginLoader();
0121         $loader->addPrefixPath('Zend_Serializer_Adapter', dirname(__FILE__).'/Serializer/Adapter');
0122         return $loader;
0123     }
0124 
0125     /**
0126      * Change the default adapter.
0127      *
0128      * @param string|Zend_Serializer_Adapter_AdapterInterface $adapter
0129      * @param array|Zend_Config $options
0130      */
0131     public static function setDefaultAdapter($adapter, $options = array())
0132     {
0133         self::$_defaultAdapter = self::factory($adapter, $options);
0134     }
0135 
0136     /**
0137      * Get the default adapter.
0138      *
0139      * @return Zend_Serializer_Adapter_AdapterInterface
0140      */
0141     public static function getDefaultAdapter()
0142     {
0143         if (!self::$_defaultAdapter instanceof Zend_Serializer_Adapter_AdapterInterface) {
0144             self::setDefaultAdapter(self::$_defaultAdapter);
0145         }
0146         return self::$_defaultAdapter;
0147     }
0148 
0149     /**
0150      * Generates a storable representation of a value using the default adapter.
0151      *
0152      * @param mixed $value
0153      * @param array $options
0154      * @return string
0155      * @throws Zend_Serializer_Exception
0156      */
0157     public static function serialize($value, array $options = array())
0158     {
0159         if (isset($options['adapter'])) {
0160             $adapter = self::factory($options['adapter']);
0161             unset($options['adapter']);
0162         } else {
0163             $adapter = self::getDefaultAdapter();
0164         }
0165 
0166         return $adapter->serialize($value, $options);
0167     }
0168 
0169     /**
0170      * Creates a PHP value from a stored representation using the default adapter.
0171      *
0172      * @param string $serialized
0173      * @param array $options
0174      * @return mixed
0175      * @throws Zend_Serializer_Exception
0176      */
0177     public static function unserialize($serialized, array $options = array())
0178     {
0179         if (isset($options['adapter'])) {
0180             $adapter = self::factory($options['adapter']);
0181             unset($options['adapter']);
0182         } else {
0183             $adapter = self::getDefaultAdapter();
0184         }
0185 
0186         return $adapter->unserialize($serialized, $options);
0187     }
0188 }