web/lib/Zend/Serializer.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: Serializer.php 20574 2010-01-24 17:39:14Z mabe $
       
    20  */
       
    21 
       
    22 /** @see Zend_Loader_PluginLoader */
       
    23 require_once 'Zend/Loader/PluginLoader.php';
       
    24 
       
    25 /**
       
    26  * @category   Zend
       
    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
       
    30  */
       
    31 class Zend_Serializer
       
    32 {
       
    33     /**
       
    34      * Plugin loader to load adapter.
       
    35      *
       
    36      * @var null|Zend_Loader_PluginLoader
       
    37      */
       
    38     private static $_adapterLoader = null;
       
    39 
       
    40     /**
       
    41      * The default adapter.
       
    42      *
       
    43      * @var string|Zend_Serializer_AdapterInterface
       
    44      */
       
    45     protected static $_defaultAdapter = 'PhpSerialize';
       
    46 
       
    47     /**
       
    48      * Create a serializer adapter instance.
       
    49      *
       
    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
       
    53      */
       
    54     public static function factory($adapterName, $opts = array()) 
       
    55     {
       
    56         if ($adapterName instanceof Zend_Serializer_Adapter_AdapterInterface) {
       
    57             return $adapterName; // $adapterName is already an adapter object
       
    58         }
       
    59 
       
    60         $adapterLoader = self::getAdapterLoader();
       
    61         try {
       
    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);
       
    66         }
       
    67 
       
    68         // ZF-8842:
       
    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');
       
    73         }
       
    74 
       
    75         return new $adapterClass($opts);
       
    76     }
       
    77 
       
    78     /**
       
    79      * Get the adapter plugin loader.
       
    80      *
       
    81      * @return Zend_Loader_PluginLoader
       
    82      */
       
    83     public static function getAdapterLoader() 
       
    84     {
       
    85         if (self::$_adapterLoader === null) {
       
    86             self::$_adapterLoader = self::_getDefaultAdapterLoader();
       
    87         }
       
    88         return self::$_adapterLoader;
       
    89     }
       
    90 
       
    91     /**
       
    92      * Change the adapter plugin load.
       
    93      *
       
    94      * @param  Zend_Loader_PluginLoader $pluginLoader
       
    95      * @return void
       
    96      */
       
    97     public static function setAdapterLoader(Zend_Loader_PluginLoader $pluginLoader) 
       
    98     {
       
    99         self::$_adapterLoader = $pluginLoader;
       
   100     }
       
   101     
       
   102     /**
       
   103      * Resets the internal adapter plugin loader
       
   104      *
       
   105      * @return Zend_Loader_PluginLoader
       
   106      */
       
   107     public static function resetAdapterLoader()
       
   108     {
       
   109         self::$_adapterLoader = self::_getDefaultAdapterLoader();
       
   110         return self::$_adapterLoader;
       
   111     }
       
   112     
       
   113     /**
       
   114      * Returns a default adapter plugin loader
       
   115      *
       
   116      * @return Zend_Loader_PluginLoader
       
   117      */
       
   118     protected static function _getDefaultAdapterLoader()
       
   119     {
       
   120         $loader = new Zend_Loader_PluginLoader();
       
   121         $loader->addPrefixPath('Zend_Serializer_Adapter', dirname(__FILE__).'/Serializer/Adapter');
       
   122         return $loader;
       
   123     }
       
   124 
       
   125     /**
       
   126      * Change the default adapter.
       
   127      *
       
   128      * @param string|Zend_Serializer_Adapter_AdapterInterface $adapter
       
   129      * @param array|Zend_Config $options
       
   130      */
       
   131     public static function setDefaultAdapter($adapter, $options = array()) 
       
   132     {
       
   133         self::$_defaultAdapter = self::factory($adapter, $options);
       
   134     }
       
   135 
       
   136     /**
       
   137      * Get the default adapter.
       
   138      *
       
   139      * @return Zend_Serializer_Adapter_AdapterInterface
       
   140      */
       
   141     public static function getDefaultAdapter() 
       
   142     {
       
   143         if (!self::$_defaultAdapter instanceof Zend_Serializer_Adapter_AdapterInterface) {
       
   144             self::setDefaultAdapter(self::$_defaultAdapter);
       
   145         }
       
   146         return self::$_defaultAdapter;
       
   147     }
       
   148 
       
   149     /**
       
   150      * Generates a storable representation of a value using the default adapter.
       
   151      *
       
   152      * @param mixed $value
       
   153      * @param array $options
       
   154      * @return string
       
   155      * @throws Zend_Serializer_Exception
       
   156      */
       
   157     public static function serialize($value, array $options = array()) 
       
   158     {
       
   159         if (isset($options['adapter'])) {
       
   160             $adapter = self::factory($options['adapter']);
       
   161             unset($options['adapter']);
       
   162         } else {
       
   163             $adapter = self::getDefaultAdapter();
       
   164         }
       
   165 
       
   166         return $adapter->serialize($value, $options);
       
   167     }
       
   168 
       
   169     /**
       
   170      * Creates a PHP value from a stored representation using the default adapter.
       
   171      *
       
   172      * @param string $serialized
       
   173      * @param array $options
       
   174      * @return mixed
       
   175      * @throws Zend_Serializer_Exception
       
   176      */
       
   177     public static function unserialize($serialized, array $options = array()) 
       
   178     {
       
   179         if (isset($options['adapter'])) {
       
   180             $adapter = self::factory($options['adapter']);
       
   181             unset($options['adapter']);
       
   182         } else {
       
   183             $adapter = self::getDefaultAdapter();
       
   184         }
       
   185 
       
   186         return $adapter->unserialize($serialized, $options);
       
   187     }
       
   188 }