Changeset 388

Show
Ignore:
Timestamp:
03/15/07 21:22:54 (1 year ago)
Author:
mikey
Message:

allow lazy loading of classes stored within session if they are an instance of stubObject

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/session/stubAbstractSession.php

    r377 r388  
    4242     */ 
    4343    private $sessionName = ''; 
     44    /** 
     45     * list of serialized stubobjects 
     46     * 
     47     * @var  array<string,stubObject> 
     48     */ 
     49    private $stubObjects = array(); 
    4450     
    4551    /** 
     
    5662            if ($this->hasValue(stubSession::START_TIME) == false) { 
    5763                // prevent session fixation 
    58                 $this->regenerateID(); 
     64                $this->regenerateId(); 
    5965            } else { 
    6066                // prevent session hijacking 
     
    192198     
    193199    /** 
     200     * stores a value associated with the key 
     201     * 
     202     * @param  string  $key    key to store value under 
     203     * @param  mixed   $value  data to store 
     204     */ 
     205    public function putValue($key, $value) 
     206    { 
     207        // This will enable lazy loading for stubObjects that are stored within 
     208        // the session. 
     209        if ($value instanceof stubObject) { 
     210            $this->doPutValue($key, $value->getSerialized()); 
     211            $this->stubObjects[$key] = $value; 
     212        } else {         
     213            $this->doPutValue($key, $value); 
     214        } 
     215    } 
     216     
     217    /** 
     218     * returns a value associated with the key or the default value 
     219     * 
     220     * @param  string  $key    key to store value under 
     221     * @param  mixed   $value  data to store 
     222     */ 
     223    protected abstract function doPutValue($key, $value); 
     224     
     225    /** 
    194226     * returns a value associated with the key or the default value 
    195227     * 
     
    206238         
    207239        if ($this->hasValue($key) == true) { 
    208             return $this->doGetValue($key); 
     240            if (isset($this->stubObjects[$key]) == true) { 
     241                return $this->stubObjects[$key]; 
     242            } 
     243             
     244            $value = $this->doGetValue($key); 
     245            if ($value instanceof stubSerializedObject) { 
     246                $stubObject = $value->getUnserialized(); 
     247                $this->stubObjects[$key] = $stubObject; 
     248                return $stubObject; 
     249            } 
     250             
     251            return $value; 
    209252        } 
    210253         
  • trunk/src/main/php/net/stubbles/ipo/session/stubPHPSession.php

    r376 r388  
    5858     * @return  string  the session id 
    5959     */ 
    60     public function getID() 
     60    public function getId() 
    6161    { 
    6262        return session_id(); 
     
    6666     * regenerates the session id but leaves session data 
    6767     */ 
    68     public function regenerateID() 
     68    public function regenerateId() 
    6969    { 
    7070        @session_regenerate_id(true); 
     
    8787     * @param  mixed   $value  data to store 
    8888     */ 
    89     public function putValue($key, $value) 
     89    protected function doPutValue($key, $value) 
    9090    { 
    9191        $_SESSION[$key] = $value; 
  • trunk/src/main/php/net/stubbles/ipo/session/stubSession.php

    r377 r388  
    5252     * @return  string  the session id 
    5353     */ 
    54     public function getID(); 
     54    public function getId(); 
    5555     
    5656    /** 
     
    6464     * regenerates the session id but leaves session data 
    6565     */ 
    66     public function regenerateID(); 
     66    public function regenerateId(); 
    6767 
    6868    /** 
  • trunk/src/main/php/net/stubbles/util/log/stubBaseLogData.php

    r372 r388  
    6262        $this->level   = $level; 
    6363        $this->logData = array(date('Y-m-d H:i:s'), 
    64                                $session->getID() 
     64                               $session->getId() 
    6565                         ); 
    6666    } 
  • trunk/src/test/php/net/stubbles/ipo/session/stubAbstractSessionTestCase.php

    r377 r388  
    99stubClassLoader::load('net.stubbles.ipo.session.stubAbstractSession'); 
    1010Mock::generate('stubRequest'); 
     11Mock::generate('stubObject'); 
    1112class stubTestSession extends stubAbstractSession 
    1213{ 
     
    3233    protected function getFingerprint() { return 'foobarbaz'; } 
    3334     
    34     public function getID() { return $this->id; } 
    35  
    36     public function regenerateID() { $this->id = 'foo'; } 
     35    public function getId() { return $this->id; } 
     36 
     37    public function regenerateId() { $this->id = 'foo'; } 
    3738 
    3839    public function invalidate() { $this->data = array(); } 
    39  
    40     public function putValue($key, $value) { $this->data[$key] = $value; } 
     40     
     41    public function inject($key, $value) { $this->data[$key] = $value; } 
     42 
     43    protected function doPutValue($key, $value) { $this->data[$key] = $value; } 
    4144     
    4245    public function hasValue($key) { return isset($this->data[$key]); } 
     
    195198        $this->assertFalse($this->session->hasValue('foo')); 
    196199    } 
     200     
     201    /** 
     202     * assure that instances of stubObject are handled correct 
     203     */ 
     204    public function testStubObjectSerialization() 
     205    { 
     206        $mockstubObject = new MockstubObject(); 
     207        $mockstubObject->setReturnValue('getClassName', 'MockstubObject'); 
     208        $mockstubObject->setReturnValue('hashCode', 'mock'); 
     209        $serialized = new stubSerializedObject($mockstubObject); 
     210        $mockstubObject->setReturnValue('getSerialized', $serialized); 
     211        $mockstubObject->expectOnce('getSerialized'); 
     212        $this->session->putValue('foo', $mockstubObject); 
     213        $foo = $this->session->getValue('foo'); 
     214        $this->assertReference($foo, $mockstubObject); 
     215    } 
     216     
     217    /** 
     218     * assure that instances of stubObject are handled correct 
     219     */ 
     220    public function testStubObjectUnserialization() 
     221    { 
     222        $mockstubObject = new MockstubObject(); 
     223        $mockstubObject->setReturnValue('getClassName', 'MockstubObject'); 
     224        $mockstubObject->setReturnValue('hashCode', 'mock'); 
     225        $serialized = new stubSerializedObject($mockstubObject); 
     226        $this->session->inject('foo', $serialized); 
     227        $second = $this->session->getValue('foo'); 
     228        $third  = $this->session->getValue('foo'); 
     229        $this->assertReference($second, $third); 
     230    } 
    197231} 
    198232?> 
  • trunk/src/test/php/net/stubbles/websites/stubFrontControllerInjectionMapTestCase.php

    r385 r388  
    3434    public function invalidate() {} 
    3535    public function reset() {} 
    36     public function putValue($key, $value) {} 
     36    protected function doPutValue($key, $value) {} 
    3737    protected function doGetValue($key) {} 
    3838    public function hasValue($key) {}