Changeset 551

Show
Ignore:
Timestamp:
04/17/07 16:21:19 (2 years ago)
Author:
mikey
Message:

added caching for xjconf variant configurations

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/websites/variantmanager/stubAbstractVariantFactory.php

    r379 r551  
    88 * @subpackage  websites_variantmanager 
    99 */ 
    10 stubClassLoader::load('net.stubbles.websites.variantmanager.stubVariantFactory'); 
     10stubClassLoader::load('net.stubbles.websites.variantmanager.stubVariantFactory', 
     11                      'net.stubbles.websites.variantmanager.stubVariantsMap' 
     12); 
    1113/** 
    1214 * Basic abstract implementation for a variant factory. 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantXJConfFactory.php

    r447 r551  
    88 * @subpackage  websites_variantmanager 
    99 */ 
    10 stubClassLoader::load('net.stubbles.util.stubFactory', 
    11                       'net.stubbles.util.xjconf.xjconf', 
     10stubClassLoader::load('net.stubbles.util.xjconf.xjconf', 
    1211                      'net.stubbles.websites.variantmanager.stubAbstractVariantFactory' 
    1312); 
     
    1817 * @subpackage  websites_variantmanager 
    1918 */ 
    20 class stubVariantXJConfFactory extends stubAbstractVariantFactory 
     19class stubVariantXJConfFactory extends stubAbstractVariantFactory implements stubXJConfInitializer 
    2120{ 
    2221    /** 
    2322     * constructor 
    2423     * 
    25      * @param   string  $configfile  optional  name of configuration file where variants are configured 
    2624     * @throws  stubVariantConfigurationException 
    2725     */ 
    28     public function __construct($configFile = null
     26    public function __construct(
    2927    { 
    30         $xjconf = new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance())); 
    31         $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/variantmanager.xml')); 
     28        $xjconfProxy = new stubXJConfProxy($this); 
     29        try { 
     30            $xjconfProxy->process(); 
     31        } catch (stubXJConfException $xjce) { 
     32            throw new stubVariantConfigurationException('Can not read variant configuration: ' . $xjce->getMessage(), $xjce); 
     33        } 
     34    } 
     35 
     36    /** 
     37     * returns the descriptor that identifies this initializer 
     38     * 
     39     * @return  string 
     40     */ 
     41    public function getDescriptor() 
     42    { 
     43        return 'variantmanager'; 
     44    } 
     45 
     46    /** 
     47     * returns the data to cache  
     48     * 
     49     * @return  array 
     50     */ 
     51    public function getCacheData() 
     52    { 
     53        $cacheData = array('name'           => $this->variantsMap->getName(), 
     54                           'usePersistence' => $this->variantsMap->shouldUsePersistence(), 
     55                           'rootVariant'    => $this->variantsMap->getRootVariant()->getSerialized() 
     56                     ); 
     57        return $cacheData; 
     58    } 
     59 
     60    /** 
     61     * sets the data from the cache 
     62     * 
     63     * @param  array  $cacheData 
     64     */ 
     65    public function setCacheData(array $cacheData) 
     66    { 
     67        $this->variantsMap = new stubVariantsMap($cacheData['rootVariant']->getUnserialized()); 
     68        $this->variantsMap->setName($cacheData['name']); 
     69        $this->variantsMap->setUsePersistence($cacheData['usePersistence']); 
    3270         
    33         if (null == $configFile) { 
    34             $configFile = stubConfig::getConfigPath() . '/xml/variantmanager.xml'; 
    35         } 
    36          
    37         try { 
    38             $xjconf->parse($configFile); 
    39         } catch (stubXJConfException $xjce) { 
    40             throw new stubVariantConfigurationException('Can not read variant configuration.', $xjce); 
    41         } 
    42          
     71    } 
     72 
     73    /** 
     74     * will be called in case the stubXJConfProxy did not found the data in the 
     75     * cache and the initializer has to load values from the facade 
     76     * 
     77     * @param  stubXJConfFacade  $xjconf 
     78     */ 
     79    public function loadData(stubXJConfFacade $xjconf) 
     80    { 
    4381        $this->variantsMap = $xjconf->getConfigValue('variants'); 
    4482    } 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantsMap.php

    r328 r551  
    5454     */ 
    5555    protected $root;  
    56      
     56 
    5757    /** 
    5858     * constructor 
    59      */ 
    60     public function __construct() 
    61     { 
    62         $this->root = new stubRootVariant(); 
     59     *  
     60     * @param  stubRootVariant  $rootVariant  optional 
     61     */ 
     62    public function __construct(stubRootVariant $rootVariant = null) 
     63    { 
     64        if (null == $rootVariant) { 
     65            $this->root = new stubRootVariant(); 
     66        } else { 
     67            $this->root = $rootVariant; 
     68            $children   = $rootVariant->getChildren(); 
     69            foreach ($children as $child) { 
     70                $this->flattenVariantTree($child); 
     71            } 
     72        } 
    6373    } 
    6474 
     
    97107        return array_keys($this->variants); 
    98108    } 
    99      
     109 
    100110    /** 
    101111     * Add a new variant 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubAbstractVariant.php

    r524 r551  
    2525 * @subpackage  websites_variantmanager_types 
    2626 */ 
    27 abstract class stubAbstractVariant extends stubBaseObject implements stubVariant 
     27abstract class stubAbstractVariant extends stubSerializableObject implements stubVariant 
    2828{ 
    2929    /** 
     
    3333     */ 
    3434    protected $children = array(); 
    35      
    3635    /** 
    3736     * Reference to the parent variant. 
     
    4039     */ 
    4140    protected $parent; 
    42  
    4341    /** 
    4442     * Name of the variant 
     
    4745     */ 
    4846    protected $name     = ''; 
    49      
    5047    /** 
    5148     * Title of the variant, only used when exporting the 
     
    5552     */ 
    5653    protected $title    = ''; 
    57  
    5854    /** 
    5955     * alias of the variant, only used when exporting the 
     
    6359     */ 
    6460    protected $alias    = ''; 
    65      
     61 
    6662    /** 
    6763     * returns the name of the variant 
     
    7369        return $this->name; 
    7470    } 
    75      
     71 
    7672    /** 
    7773     * returns the full qualified name of the variant 
     
    8783        return $this->name; 
    8884    } 
    89      
     85 
    9086    /** 
    9187     * sets the name of the variant 
     
    10298        $this->name = $name; 
    10399    } 
    104      
     100 
    105101    /** 
    106102     * returns title of the variant 
     
    112108        return $this->title; 
    113109    } 
    114      
     110 
    115111    /** 
    116112     * sets the title of the variant 
     
    122118        $this->title = $title; 
    123119    } 
    124      
     120 
    125121    /** 
    126122     * returns alias name of the variant 
     
    132128        return $this->alias; 
    133129    } 
    134      
     130 
    135131    /** 
    136132     * set alias name of the variant 
     
    142138        $this->alias = $alias; 
    143139    } 
    144      
     140 
    145141    /** 
    146142     * return the forced variant 
     
    167163        return null; 
    168164    } 
    169      
     165 
    170166    /** 
    171167     * return the variant 
     
    193189        return null; 
    194190    } 
    195      
     191 
    196192    /** 
    197193     * check whether the conditions for this variant are met 
     
    205201        return $this->isValid($session, $request); 
    206202    } 
    207      
     203 
    208204    /** 
    209205     * assign that this variant has been choosen 
     
    222218        return false; 
    223219    } 
    224      
     220 
    225221    /** 
    226222     * returns parent variant 
     
    232228        return $this->parent; 
    233229    } 
    234      
     230 
    235231    /** 
    236232     * set parent variant 
     
    242238        $this->parent = $parent; 
    243239    } 
    244      
     240 
    245241    /** 
    246242     * check whether the variant has a parent variant 
     
    252248        return (null != $this->parent); 
    253249    } 
    254      
     250 
    255251    /** 
    256252     * return child variants of this variant 
     
    262258        return $this->children; 
    263259    } 
    264      
     260 
    265261    /** 
    266262     * add a child variant 
     
    270266    public function addChild(stubVariant $child) 
    271267    { 
     268        if ($child->hashCode() == $this->hashCode()) { 
     269            throw new stubVariantConfigurationException('A variant can not add itself as child.'); 
     270        } 
     271         
    272272        $this->children[$child->getName()] = $child; 
    273273        $child->setParent($this); 
    274274    } 
    275      
     275 
    276276    /** 
    277277     * remove a child variant 
     
    286286        } 
    287287    } 
    288      
     288 
    289289    /** 
    290290     * return string representation of this variant 
     
    305305        return $this->name . '(' . $return . ')'; 
    306306    } 
     307 
     308    /** 
     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(); 
     316        foreach ($this->children as $name => $child) { 
     317            $this->_serializedProperties[$name] = $child->getSerialized(); 
     318        } 
     319         
     320        return array('name', 'title', 'alias', '_serializedProperties'); 
     321    } 
     322 
     323    /** 
     324     * restore all children 
     325     */ 
     326    public function __wakeup() 
     327    { 
     328        foreach ($this->_serializedProperties as $name => $serializedChild) { 
     329            $this->addChild($serializedChild->getUnserialized()); 
     330        } 
     331         
     332        $this->_serializedProperties = array(); 
     333    } 
    307334} 
    308335?> 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubVariant.php

    r523 r551  
    1717 * @subpackage  websites_variantmanager_types 
    1818 */ 
    19 interface stubVariant 
     19interface stubVariant extends stubSerializable 
    2020{ 
    2121    /** 
     
    2525     */ 
    2626    public function getName(); 
    27      
     27 
    2828    /** 
    2929     * returns the full qualified name of the variant 
     
    3232     */ 
    3333    public function getFullQualifiedName(); 
    34      
     34 
    3535    /** 
    3636     * sets the name of the variant 
     
    3939     */ 
    4040    public function setName($name); 
    41      
     41 
    4242    /** 
    4343     * returns title of the variant 
     
    4646     */ 
    4747    public function getTitle(); 
    48      
     48 
    4949    /** 
    5050     * sets the title of the variant 
     
    5353     */ 
    5454    public function setTitle($title); 
    55      
     55 
    5656    /** 
    5757     * returns alias name of the variant 
     
    6060     */ 
    6161    public function getAlias(); 
    62      
     62 
    6363    /** 
    6464     * check whether the variant is an enforcing variant 
     
    6969     */ 
    7070    public function isEnforcing(stubSession $session, stubRequest $request); 
    71      
     71 
    7272    /** 
    7373     * return the forced variant 
     
    7878     */ 
    7979    public function getEnforcingVariant(stubSession $session, stubRequest $request); 
    80      
     80 
    8181    /** 
    8282     * return the variant 
     
    8787     */ 
    8888    public function getVariant(stubSession $session, stubRequest $request); 
    89      
     89 
    9090    /** 
    9191     * check whether the conditions for this variant are met 
     
    9696     */ 
    9797    public function conditionsMet(stubSession $session, stubRequest $request); 
    98      
     98 
    9999    /** 
    100100     * check whether the variant is valid 
     
    113113     */ 
    114114    public function assign(stubSession $session, stubRequest $request); 
    115      
     115 
    116116    /** 
    117117     * returns parent variant 
     
    120120     */ 
    121121    public function getParent(); 
    122      
     122 
    123123    /** 
    124124     * set parent variant 
     
    127127     */ 
    128128    public function setParent(stubVariant $parent = null); 
    129      
     129 
    130130    /** 
    131131     * check whether the variant has a parent variant 
     
    134134     */ 
    135135    public function hasParent(); 
    136      
     136 
    137137    /** 
    138138     * return child variants of this variant 
     
    141141     */ 
    142142    public function getChildren(); 
    143      
     143 
    144144    /** 
    145145     * add a child variant 
     
    148148     */ 
    149149    public function addChild(stubVariant $child); 
    150      
     150 
    151151    /** 
    152152     * remove a child variant 
  • trunk/src/test/php/net/stubbles/websites/variantmanager/VariantManagerTestSuite.php

    r382 r551  
    3030        $this->addTestFile(dirname(__FILE__) . '/stubVariantsPreInterceptorCookieVariantTestCase.php'); 
    3131        $this->addTestFile(dirname(__FILE__) . '/stubVariantsPreInterceptorProcessTestCase.php'); 
     32        $this->addTestFile(dirname(__FILE__) . '/stubVariantXJConfFactoryTestCase.php'); 
    3233    } 
    3334} 
  • trunk/src/test/php/net/stubbles/websites/variantmanager/stubVariantsMapTestCase.php

    r328 r551  
    5757     */ 
    5858    protected $v3; 
    59      
     59 
    6060    /** 
    6161     * set up the test environment 
     
    8383        $this->assertEqual($this->variantMap->getName(), 'foo'); 
    8484    } 
    85      
     85 
    8686    /** 
    8787     * assure that persistence is handled correct 
     
    9393        $this->assertFalse($this->variantMap->shouldUsePersistence()); 
    9494    } 
    95      
     95 
    9696    /** 
    9797     * test handling with no variants configured 
     
    108108        $this->variantMap->getVariant($this->mockSession, $this->mockRequest); 
    109109    } 
    110      
     110 
    111111    /** 
    112112     * test handling with one variant 
     
    126126        $this->assertReference($variantTest2, $this->v1); 
    127127    } 
    128      
     128 
    129129    /** 
    130130     * Tests that all nested variants are found 
     
    139139        $this->assertTrue($this->variantMap->variantExists('v3')); 
    140140    } 
    141      
     141 
    142142    /** 
    143143     * Test that no enforcing variant is returned when there is no enforcing variant set 
     
    165165        $this->assertReference($this->variantMap->getVariantByName('v3'), $this->v3); 
    166166    } 
    167      
     167 
    168168    /** 
    169169     * Test that all variant names will be returned 
     
    177177        $this->assertEqual($this->variantMap->getVariantNames(), array('v1', 'v2', 'v3')); 
    178178    } 
     179 
     180    /** 
     181     * test creating the variants map with a root variant as argument 
     182     */ 
     183    public function testWithRootArgument() 
     184    { 
     185        $rootVariant = new stubRootVariant(); 
     186        $this->v2->addChild($this->v3); 
     187        $this->v1->addChild($this->v2); 
     188        $rootVariant->addChild($this->v1); 
     189        $variantMap = new stubVariantsMap($rootVariant); 
     190         
     191        $this->assertEqual($variantMap->getVariantNames(), array('v1', 'v2', 'v3')); 
     192        $this->assertReference($variantMap->getVariantByName('v1'), $this->v1); 
     193        $this->assertReference($variantMap->getVariantByName('v2'), $this->v2); 
     194        $this->assertReference($variantMap->getVariantByName('v3'), $this->v3); 
     195    } 
    179196} 
    180197?> 
  • trunk/src/test/php/net/stubbles/websites/variantmanager/types/stubAbstractVariantTestCase.php

    r524 r551  
    6161     */ 
    6262    protected $mockRequest; 
    63      
     63 
    6464    /** 
    6565     * set up test environment 
     
    7171        $this->mockRequest     = new MockstubRequest(); 
    7272    } 
    73      
     73 
    7474    /** 
    7575     * test that setting the name works as expected 
     
    8585        $this->abstractVariant->setName('foobarbazfoobarbaz'); 
    8686    } 
    87      
     87 
    8888    /** 
    8989     * test that setting the title works as expected 
     
    105105        $this->assertEqual($this->abstractVariant->getAlias(), 'foo'); 
    106106    } 
    107      
     107 
    108108    /** 
    109109     * test a non valid and non enforcing variant 
     
    130130        $this->assertReference($variant, $this->abstractVariant); 
    131131    } 
    132      
     132 
    133133    /** 
    134134     * test a variant that has non-valid childs 
     
    160160        $this->assertReference($variant, $this->abstractVariant); 
    161161    } 
    162      
     162 
    163163    /** 
    164164     * test with a valid child 
     
    184184        $this->assertReference($variant, $child2); 
    185185    } 
    186      
     186 
    187187    /** 
    188188     * test that removing a child works as expected 
     
    204204        $this->assertEqual($this->abstractVariant->getChildren(), array('bar' => $child2)); 
    205205    } 
    206      
     206 
     207    /** 
     208     * assure that a variant can not add itself as its own child 
     209     */ 
     210    public function testAddItself() 
     211    { 
     212        $this->expectException('stubVariantConfigurationException'); 
     213        $this->abstractVariant->addChild($this->abstractVariant); 
     214    } 
     215 
    207216    /** 
    208217     * test parent 
     
    224233        $this->assertEqual($this->abstractVariant->getFullQualifiedName(), 'foo:bar'); 
    225234    } 
    226      
     235 
    227236    /** 
    228237     * test full qualified name when parent variant is root variant 
     
    235244        $this->assertEqual($this->abstractVariant->getFullQualifiedName(), 'bar'); 
    236245    } 
     246 
     247    /** 
     248     * test __sleep() and __wakeup() 
     249     */ 
     250    public function testSleepWakeup() 
     251    { 
     252        $this->abstractVariant->setName('foo'); 
     253        $dummy1 = new stubDummyVariant(); 
     254        $dummy1->setName('bar'); 
     255        $this->abstractVariant->addChild($dummy1); 
     256        $dummy2 = new stubDummyVariant(); 
     257        $dummy2->setName('baz'); 
     258        $dummy1->addChild($dummy2); 
     259         
     260        $serialized = serialize($this->abstractVariant); 
     261        $abstractVariant = unserialize($serialized); 
     262        $this->assertFalse($abstractVariant->hasParent()); 
     263        $children1 = $abstractVariant->getChildren(); 
     264        $this->assertEqual($children1['bar']->getName(), 'bar'); 
     265        $this->assertEqual($children1['bar']->getParent()->getName(), 'foo'); 
     266        $children2 = $children1['bar']->getChildren(); 
     267        $this->assertEqual($children2['baz']->getName(), 'baz'); 
     268        $this->assertEqual($children2['baz']->getParent()->getName(), 'bar'); 
     269    } 
    237270} 
    238271?>