Changeset 500

Show
Ignore:
Timestamp:
04/13/07 16:57:46 (2 years ago)
Author:
mikey
Message:

allow deep serializing with lazy class loading

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/stubBaseObject.php

    r387 r500  
    1616{ 
    1717    /** 
     18     * a list of serialized properties 
     19     *  
     20     * Do not ever use this property in extended classes. It is only protected  
     21     * and not private because of some PHP-$§&%&§%&! 
     22     * 
     23     * @var  array<string,stubSerializedObject> 
     24     */ 
     25    protected $_serializedProperties = array(); 
     26     
     27    /** 
    1828     * returns class informations 
    1929     * 
     
    7484        $serialized = new stubSerializedObject($this); 
    7585        return $serialized; 
     86    } 
     87     
     88    /** 
     89     * ensure that all instances of stubObject are correctly serialized 
     90     * 
     91     * @return  array<string>  list of properties to serialize 
     92     */ 
     93    public function __sleep() 
     94    { 
     95        $this->_serializedProperties = array(); 
     96        $propertiesToSerialize      = array(); 
     97        foreach (get_object_vars($this) as $name => $value) { 
     98            if ($value instanceof stubObject) { 
     99                $this->_serializedProperties[$name] = $value->getSerialized(); 
     100            } else { 
     101                $propertiesToSerialize[] = $name; 
     102            } 
     103        } 
     104         
     105        return $propertiesToSerialize; 
     106    } 
     107     
     108    /** 
     109     * restore all instances that are of type stubObject 
     110     */ 
     111    public function __wakeup() 
     112    { 
     113        foreach ($this->_serializedProperties as $name => $serializedValue) { 
     114            $this->$name = $serializedValue->getUnserialized(); 
     115        } 
     116         
     117        $this->_serializedProperties = array(); 
    76118    } 
    77119     
     
    130172        $string     = $object->getClassName() . " {\n"; 
    131173        foreach ($properties as $name => $value) { 
     174            if ('_serializedProperties' == $name) { 
     175                continue; 
     176            } 
     177             
    132178            $string .= '    ' . $name . '(' . self::determineType($value) . '): '; 
    133179            if (($value instanceof self) == false) { 
  • trunk/src/test/php/net/stubbles/stubBaseObjectTestCase.php

    r387 r500  
    2626    { 
    2727        return 'test.stub2stubBaseObject'; 
     28    } 
     29     
     30    public function getSerializedProperties() 
     31    { 
     32        return $this->_serializedProperties; 
    2833    } 
    2934} 
     
    104109        $this->assertEqual((string) $this->stubBaseObject2, "test.stub2stubBaseObject {\n    stubBaseObject(test.stub1stubBaseObject): test.stub1stubBaseObject {\n        bar(integer): 5\n    }\n    foo(string): bar\n}\n"); 
    105110    } 
     111     
     112    /** 
     113     * assure that the __sleep() method works correct 
     114     */ 
     115    public function testSleep() 
     116    { 
     117        $this->assertEqual($this->stubBaseObject2->__sleep(), array('foo', '_serializedProperties')); 
     118        $serializedProperties = $this->stubBaseObject2->getSerializedProperties(); 
     119        $this->assertTrue(isset($serializedProperties['stubBaseObject'])); 
     120        $this->assertIsA($serializedProperties['stubBaseObject'], 'stubSerializedObject'); 
     121        $this->assertEqual($serializedProperties['stubBaseObject']->getSerializedClassName(), 'test.stub1stubBaseObject'); 
     122    } 
    106123} 
    107124?>