TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file SerializeManager.php

Documentation is available at SerializeManager.php

  1. <?php
  2.   public function __construct($objectKey){
  3.       $this->objectKey $objectKey;
  4.   }
  5. }
  6.  
  7. class SerializeManager{  
  8.   private $_serializedObjects array()// avoid key = 0;
  9.   private $_autoKey 1;
  10.   private $_serializedKeys array();
  11.   private $_unserializedObjects array();
  12.   
  13.   /**
  14.    * OURD_SerializeManager
  15.    * @return SerializeManager 
  16.    */
  17.   public static function instance(){
  18.       static $intance;
  19.       if(!isset($intance)){
  20.           $intance new self();
  21.       }
  22.       return $intance;
  23.   }
  24.   
  25.   private function _isSerialized($obj){
  26.       foreach($this->_serializedObjects as $key=>$value){
  27.         if($obj === $value){
  28.           return $key;
  29.         }
  30.       }
  31.       return false;   
  32.   }
  33.   
  34.   private function _addSerializedObject($obj){
  35.       $this->_serializedObjects[$this->_autoKey++$obj;    
  36.       return $this->_autoKey 1;
  37.   }
  38.   
  39.   private function _registerSerializedObject($obj){
  40.     if(is_object($obj&& !($objectKey $this->_isSerialized($obj))){
  41.       return $this->_addSerializedObject($obj);
  42.     }else{
  43.       return $objectKey;
  44.     }
  45.   }
  46.   
  47.   private function _replaceSerializedObjects(array &$data){
  48.     foreach($data as $key=>&$value){
  49.       if(is_object($value)){
  50.         if($objectKey $this->_isSerialized($value)){
  51.           $data[$keynew ReferencedObjectSerializeMarker($objectKey)
  52.         }else{
  53.           $this->_addSerializedObject($value);
  54.         }       
  55.       }else if(is_array($value)){
  56.         $this->_replaceSerializedObjects($value);
  57.       }
  58.     }
  59.   }
  60.   
  61.   /**
  62.    * @return string 
  63.    */
  64.   public function serializeObject($object){
  65.       $this->init();
  66.       return serialize($object);
  67.     $this->init();
  68.   }  
  69.   
  70.   /**
  71.    * @return string 
  72.    */
  73.   public function serializeVars($objectarray $data array()){
  74.       $objectKey $this->_registerSerializedObject($object);
  75.       if(!isset($this->_serializedKeys[$objectKey])){
  76.           $this->_serializedKeys[$objectKeytrue;
  77.         $this->_replaceSerializedObjects($data);   
  78.         return "$objectKey:".serialize($data)
  79.       }else{
  80.           throw new Exception("Circle reference without serialize by manager");
  81.       }
  82.   }
  83.  
  84.   /******** Unserialze **********/
  85.   
  86.   public function replaceUnserializedObjects(array &$data){
  87.       foreach($data as $key=>&$value){
  88.       if($value instanceof ReferencedObjectSerializeMarker){
  89.         $data[$key$this->_unserializedObjects[$value->objectKey];
  90.       }elseif(is_array($value)){
  91.         $this->replaceUnserializedObjects($value);
  92.       }
  93.     }
  94.   }
  95.  
  96.   /**
  97.    * @return array 
  98.    */
  99.   public function unserializeVars($object$serialized){
  100.     if(preg_match("#^(.*?):a:#"$serialized$matches)){
  101.        $objectKey $matches[1];
  102.        $this->_unserializedObjects[$objectKey$object;
  103.        $serialized substr($serializedstrlen($matches[1]1);
  104.        return unserialize($serialized);
  105.     }else{
  106.       return array();
  107.     }
  108.   }
  109.   
  110.   private function init(){
  111.     // clearn-up
  112.     $this->_serializedObjects array("")// avoid key = 0;
  113.     $this->_serializedKeys array();
  114.     $this->_unserializedObjects array();     
  115.     $this->_autoKey 1;       
  116.   }
  117.   
  118.   public function postUnserialize(){
  119.     foreach($this->_unserializedObjects as $key=>$value){
  120.         if(is_object($value&& is_callable(array($value'postUnserializedByManager'))){
  121.          $value->postUnserializedByManager();
  122.         }
  123.     }
  124.     $this->init();
  125.   }
  126.   
  127.   public function unserializeObject($serialized){
  128.     $this->init();      
  129.     $object unserialize($serialized);
  130.     $this->postUnserialize();
  131.     return $object;
  132.   }
  133. }
  134. ?>

Documentation generated on Wed, 16 Jun 2010 12:07:36 +0200 by phpDocumentor 1.4.1