Changeset 526

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

added caching to net.stubbles.util.stubRegistryXJConfInitializer
added unit test for net.stubbles.util.stubRegistryXJConfInitializer

Files:

Legend:

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

    r525 r526  
    33 * Class for storing values. 
    44 *  
    5  * @author      Frank Kleine <frank@kl-s.com
     5 * @author      Frank Kleine <mikey@stubbles.net
    66 * @package     stubbles 
    77 * @subpackage  util 
  • trunk/src/main/php/net/stubbles/util/stubRegistryXJConfInitializer.php

    r443 r526  
    33 * Class for initializing the Registry. 
    44 *  
    5  * @author      Frank Kleine <frank@kl-s.com
     5 * @author      Frank Kleine <mikey@stubbles.net
    66 * @package     stubbles 
    77 * @subpackage  util 
    88 */ 
    99stubClassLoader::load('net.stubbles.util.stubFactory', 
     10                      'net.stubbles.util.stubRegistry', 
    1011                      'net.stubbles.util.stubRegistryInitializer', 
    1112                      'net.stubbles.util.xjconf.xjconf' 
     
    2526     */ 
    2627    protected $configFile; 
    27      
     28    /** 
     29     * name of the cache file to use 
     30     * 
     31     * @var  string 
     32     */ 
     33    protected $cacheFile; 
     34 
    2835    /** 
    2936     * constructor 
     
    3239    { 
    3340        $this->configFile = stubConfig::getConfigPath() . '/xml/config.xml'; 
     41        $this->cacheFile  = stubConfig::getCachePath() . '/config.cache'; 
    3442    } 
    35      
     43 
    3644    /** 
    37      * sets the name of the config gile to use 
     45     * sets the name of the config file to use 
    3846     * 
    3947     * @param  string  $configFile 
     
    4351        $this->configFile = $configFile; 
    4452    } 
    45      
     53 
    4654    /** 
    4755     * initialize the Registry with configuration values from given configuration file 
     
    5260    public function init() 
    5361    { 
    54         $xjconf = new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance())); 
     62        if ($this->isCacheUpToDate() == true) { 
     63            foreach ($this->getCacheData() as $key => $value) { 
     64                stubRegistry::setConfig($key, $value); 
     65            } 
     66             
     67            return; 
     68        } 
     69         
     70        $xjconf = $this->createXJConfFacade(); 
    5571        $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/registry.xml')); 
    5672        $xjconf->enableXIncludes(); 
    5773        $xjconf->parse($this->configFile); 
     74        $cacheData = array(); 
     75        foreach (stubRegistry::getConfigKeys() as $key) { 
     76            $cacheData[$key] = stubRegistry::getConfig($key); 
     77        } 
     78         
     79        $this->setCacheData($cacheData); 
     80    } 
     81 
     82    /** 
     83     * checks if the cache file is newer than the config file 
     84     *  
     85     * @return  bool 
     86     */ 
     87    protected function isCacheUpToDate() 
     88    { 
     89        return (file_exists($this->cacheFile) == true && filemtime($this->cacheFile) >= filemtime($this->configFile)); 
     90    } 
     91 
     92    /** 
     93     * retrieves the cache data 
     94     * 
     95     * @return  array 
     96     */ 
     97    protected function getCacheData() 
     98    { 
     99        return unserialize(file_get_contents($this->cacheFile)); 
     100    } 
     101 
     102    /** 
     103     * saves the cache data 
     104     * 
     105     * @param  array<string,mixed>  $cacheData 
     106     */ 
     107    protected function setCacheData(array $cacheData) 
     108    { 
     109        file_put_contents($this->cacheFile, serialize($cacheData)); 
     110    } 
     111 
     112    /** 
     113     * creates an instance of the XJConfFacade 
     114     * 
     115     * @return  stubXJConfFacade 
     116     */ 
     117    protected function createXJConfFacade() 
     118    { 
     119        return new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance($this->configFile))); 
    58120    } 
    59121} 
  • trunk/src/test/php/net/stubbles/util/UtilTestSuite.php

    r426 r526  
    2424        $this->addTestFile($dir . '/BinfordTestCase.php'); 
    2525        $this->addTestFile($dir . '/stubRegistryTestCase.php'); 
     26        $this->addTestFile($dir . '/stubRegistryXJConfInitializerTestCase.php'); 
    2627         
    2728        // error handler 
  • trunk/src/test/php/net/stubbles/util/stubRegistryTestCase.php

    r525 r526  
    11<?php 
    22/** 
    3  * Tests for helper.stubRegistry 
     3 * Tests for net.stubbles.util.stubRegistry 
    44 * 
    55 * @author      Frank Kleine <frank@kl-s.com> 
     
    99stubClassLoader::load('net.stubbles.util.stubRegistry'); 
    1010/** 
    11  * Tests for helper.stubRegistry 
     11 * Tests for net.stubbles.util.stubRegistry 
    1212 * 
    1313 * @package     stubbles