Changeset 757

Show
Ignore:
Timestamp:
07/10/07 14:34:21 (1 year ago)
Author:
mikey
Message:

added template methods to hook into serializing/unserializing
made stubSerializableObject::sleep() and stubSerializableObject::wakeup() final

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAbstractAnnotation.php

    r735 r757  
    7878 
    7979    /** 
    80      * store only the names of the property and the reflected class names, 
    81      * not the reflection class itself because it can not be serialized 
     80     * template method to hook into __sleep() 
    8281     * 
    83      * @return  array<string>  list of properties to serialize 
     82     * @return  array<string>  list of property names that should not be serialized 
    8483     */ 
    85     public function __sleep() 
     84    protected function __doSleep() 
    8685    { 
    87         $propertiesToSerialize       = array(); 
    88         $this->reflectedClasses      = array(); 
    89         $this->_serializedProperties = array(); 
    90         foreach (get_object_vars($this) as $name => $value) { 
    91             if ($value instanceof stubReflectionClass) { 
    92                 $this->reflectedClasses[$name] = $value->getFullQualifiedClassName(); 
    93             } elseif ($value instanceof stubSerializable) { 
    94                 $this->_serializedProperties[$name] = $value->getSerialized(); 
    95             } else { 
    96                 $propertiesToSerialize[] = $name; 
    97             } 
    98         } 
    99          
    100         return $propertiesToSerialize; 
     86        $this->reflectedClasses = array(); 
     87        return array(); 
    10188    } 
    10289 
    10390    /** 
    104      * restore all properties containing an instance of a reflection class 
     91     * takes care of serializing the value 
     92     * 
     93     * @param  array   $propertiesToSerialize  list of properties to serialize 
     94     * @param  string  $name                   name of the property to serialize 
     95     * @param  mixed   $value                  value to serialize 
    10596     */ 
    106     public function __wakeup(
     97    protected function __doSerialize(&$propertiesToSerialize, $name, $value
    10798    { 
    108         parent::__wakeup(); 
     99        if ($value instanceof stubReflectionClass) { 
     100            $this->reflectedClasses[$name] = $value->getFullQualifiedClassName(); 
     101            return; 
     102        } 
     103         
     104        parent::__doSerialize($propertiesToSerialize, $name, $value); 
     105    } 
     106 
     107    /** 
     108     * template method to hook into __wakeup() 
     109     */ 
     110    protected function __doWakeUp() 
     111    { 
    109112        foreach ($this->reflectedClasses as $propertyName => $reflectedClasses) { 
    110113            $this->$propertyName = new stubReflectionClass($reflectedClasses); 
  • trunk/src/main/php/net/stubbles/stubSerializableObject.php

    r555 r757  
    4343     * @XMLIgnore 
    4444     */ 
    45     public function __sleep() 
     45    public final function __sleep() 
    4646    { 
    4747        $this->_serializedProperties = array(); 
    48         $propertiesToSerialize      = array(); 
     48        $nonAllowedProperties        = $this->__doSleep(); 
     49        $propertiesToSerialize       = array(); 
    4950        foreach (get_object_vars($this) as $name => $value) { 
    50             if ($value instanceof stubSerializable) { 
    51                 $this->_serializedProperties[$name] = $value->getSerialized(); 
    52             } else { 
    53                 $propertiesToSerialize[] = $name; 
     51            if (in_array($name, $nonAllowedProperties) == true) { 
     52                continue; 
    5453            } 
     54             
     55            $this->__doSerialize($propertiesToSerialize, $name, $value); 
    5556        } 
    5657         
     
    5960 
    6061    /** 
     62     * template method to hook into __sleep() 
     63     * 
     64     * @return  array<string>  list of property names that should not be serialized 
     65     */ 
     66    protected function __doSleep() 
     67    { 
     68        return array(); 
     69    } 
     70 
     71    /** 
     72     * takes care of serializing the value 
     73     * 
     74     * @param  array   $propertiesToSerialize  list of properties to serialize 
     75     * @param  string  $name                   name of the property to serialize 
     76     * @param  mixed   $value                  value to serialize 
     77     */ 
     78    protected function __doSerialize(&$propertiesToSerialize, $name, $value) 
     79    { 
     80        if ($value instanceof stubSerializable) { 
     81            $this->_serializedProperties[$name] = $value->getSerialized(); 
     82        } else { 
     83            $propertiesToSerialize[] = $name; 
     84        } 
     85    } 
     86     
     87    /** 
    6188     * restore all instances that are of type stubSerializable 
    6289     *  
    6390     * @XMLIgnore 
    6491     */ 
    65     public function __wakeup() 
     92    public final function __wakeup() 
    6693    { 
     94        $this->__doWakeUp(); 
    6795        foreach ($this->_serializedProperties as $name => $serializedValue) { 
    68             $this->$name = $serializedValue->getUnserialized(); 
     96            $this->__doUnserialize($name, $serializedValue); 
    6997        } 
    7098         
    7199        $this->_serializedProperties = array(); 
    72100    } 
     101 
     102    /** 
     103     * template method to hook into __wakeup() 
     104     */ 
     105    protected function __doWakeUp() 
     106    { 
     107        // intentionally empty 
     108    } 
     109 
     110    /** 
     111     * takes care of unserializing the value 
     112     * 
     113     * @param  string  $name             name of the property 
     114     * @param  mixed   $serializedValue  value of the property 
     115     */ 
     116    protected function __doUnserialize($name, $serializedValue) 
     117    { 
     118        $this->$name = $serializedValue->getUnserialized(); 
     119    } 
    73120} 
    74121?> 
  • trunk/src/main/php/net/stubbles/websites/processors/stubAbstractProcessorResolver.php

    r737 r757  
    8989 
    9090    /** 
    91      * ensure that all instances of stubSerializable are correctly serialized 
     91     * template method to hook into __sleep() 
    9292     * 
    93      * @return  array<string>  list of properties to serialize 
     93     * @return  array<string>  list of property names that should not be serialized 
    9494     */ 
    95     public function __sleep() 
     95    protected function __doSleep() 
    9696    { 
    97         $propertiesToSerialize = parent::__sleep(); 
    9897        $this->_serializedProperties['pageFactory'] = $this->pageFactory->getClassName(); 
    99         $return = array(); 
    100         foreach ($propertiesToSerialize as $propertyToSerialize) { 
    101             if ('pageFactory' == $propertyToSerialize) { 
    102                 continue; 
    103             } 
    104              
    105             $return[] = $propertyToSerialize; 
    106         } 
    107          
    108         return $return; 
     98        return array('pageFactory'); 
    10999    } 
    110100 
    111101    /** 
    112      * restore all instances that are of type stubSerializable 
     102     * template method to hook into __wakeup() 
    113103     */ 
    114     public function __wakeup() 
     104    protected function __doWakeUp() 
    115105    { 
    116106        $nqClassName = stubClassLoader::getNonQualifiedClassName($this->_serializedProperties['pageFactory']); 
     
    121111        $this->pageFactory = new $nqClassName(); 
    122112        unset($this->_serializedProperties['pageFactory']); 
    123         parent::__wakeup(); 
    124113    } 
    125114} 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubAbstractVariant.php

    r672 r757  
    307307 
    308308    /** 
    309      * ensure that all children are correctly serialized 
    310      * 
    311      * @return  array<string>  list of properties to serialize 
    312      */ 
    313     public function __sleep() 
    314     { 
    315         $this->_serializedProperties = array(); 
     309     * template method to hook into __sleep() 
     310     * 
     311     * @return  array<string>  list of property names that should not be serialized 
     312     */ 
     313    protected function __doSleep() 
     314    { 
    316315        foreach ($this->children as $name => $child) { 
    317316            $this->_serializedProperties[$name] = $child->getSerialized(); 
    318317        } 
    319318         
    320         $nonAllowedProperties  = array('children', 'parent'); 
    321         $propertiesToSerialize = array(); 
    322         foreach ($this->getClass()->getProperties() as $property) { 
    323             if (in_array($property->getName(), $nonAllowedProperties) == true  || $property->isStatic() == true) { 
    324                 continue; 
    325             } 
    326              
    327             $propertiesToSerialize[] = $property->getName(); 
    328         } 
    329          
    330         return $propertiesToSerialize; 
    331     } 
    332  
    333     /** 
    334      * restore all children 
    335      */ 
    336     public function __wakeup() 
     319        return array('children', 'parent'); 
     320    } 
     321 
     322    /** 
     323     * template method to hook into __wakeup() 
     324     */ 
     325    protected function __doWakeUp() 
    337326    { 
    338327        foreach ($this->_serializedProperties as $name => $serializedChild) {