Changeset 584

Show
Ignore:
Timestamp:
04/20/07 14:32:56 (1 year ago)
Author:
mikey
Message:

reworked the way extensions are handled, allows lazy loading of net.xjconf.ext.Extension
allowed processing of more than one configuration file

Files:

Legend:

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

    r547 r584  
    109109 
    110110    /** 
     111     * returns a list of extensions for the parser 
     112     * 
     113     * @return  array<Extension> 
     114     */ 
     115    public function getExtensions() 
     116    { 
     117        return array(); 
     118    } 
     119 
     120    /** 
    111121     * will be called in case the stubXJConfProxy did not found the data in the 
    112122     * cache and the initializer has to load values from the facade 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseInitializer.php

    r538 r584  
    5757 
    5858    /** 
     59     * returns a list of extensions for the parser 
     60     * 
     61     * @return  array<Extension> 
     62     */ 
     63    public function getExtensions() 
     64    { 
     65        return array(); 
     66    } 
     67 
     68    /** 
    5969     * will be called in case the stubXJConfProxy did not found the data in the 
    6070     * cache and the initializer has to load values from the facade 
  • trunk/src/main/php/net/stubbles/util/log/stubLoggerXJConfFactory.php

    r562 r584  
    7575 
    7676    /** 
     77     * returns a list of extensions for the parser 
     78     * 
     79     * @return  array<Extension> 
     80     */ 
     81    public function getExtensions() 
     82    { 
     83        return array(new stubConfigXJConfExtension()); 
     84    } 
     85 
     86    /** 
    7787     * will be called in case the stubXJConfProxy did not found the data in the 
    7888     * cache and the initializer has to load values from the facade 
     
    93103    { 
    94104        $xjconfProxy = new stubXJConfProxy($this); 
    95         $xjconfProxy->process(array(new stubConfigXJConfExtension())); 
     105        $xjconfProxy->process(); 
    96106    } 
    97107} 
  • trunk/src/main/php/net/stubbles/util/stubRegistryXJConfInitializer.php

    r538 r584  
    5757 
    5858    /** 
     59     * returns a list of extensions for the parser 
     60     * 
     61     * @return  array<Extension> 
     62     */ 
     63    public function getExtensions() 
     64    { 
     65        return array(); 
     66    } 
     67 
     68    /** 
    5969     * will be called in case the stubXJConfProxy did not found the data in the 
    6070     * cache and the initializer has to load values from the facade 
  • trunk/src/main/php/net/stubbles/util/xjconf/stubXJConfInitializer.php

    r538 r584  
    4444 
    4545    /** 
     46     * returns a list of extensions for the parser 
     47     * 
     48     * @return  array<Extension> 
     49     */ 
     50    public function getExtensions(); 
     51     
     52    /** 
    4653     * will be called in case the stubXJConfProxy did not found the data in the 
    4754     * cache and the initializer has to load values from the facade 
  • trunk/src/main/php/net/stubbles/util/xjconf/stubXJConfProxy.php

    r580 r584  
    6060     * initialize with configuration values from given configuration file 
    6161     * 
    62      * @param   array<Extension>     a list of XJConf extensions 
     62     * @param   array<string>        optional  a list of sources to parse 
    6363     * @return  bool                 true if data was cached, else false 
    6464     * @throws  stubXJConfException 
    6565     */ 
    66     public function process(array $extensions = array()) 
     66    public function process(array $sources = array()) 
    6767    { 
    68         if (file_exists($this->cacheFile) == true && filemtime($this->cacheFile) >= filemtime($this->configFile)) { 
    69             $this->initializer->setCacheData(unserialize(file_get_contents($this->cacheFile))); 
    70             return; 
     68        if (file_exists($this->cacheFile) == true) { 
     69            $useCache = true; 
     70            if (count($sources) == 0 && filemtime($this->cacheFile) < filemtime($this->configFile)) { 
     71                $useCache = false; 
     72            } elseif (count($sources) > 0) { 
     73                foreach ($sources as $source) { 
     74                    if (filemtime($this->cacheFile) < filemtime($source)) { 
     75                        $useCache = false; 
     76                    } 
     77                } 
     78            } 
     79             
     80            if (true == $useCache) { 
     81                $this->initializer->setCacheData(unserialize(file_get_contents($this->cacheFile))); 
     82                return; 
     83            } 
    7184        } 
    7285         
     
    7588        $xjconf->addDefinitions(stubFactory::getResourceURIs('xjconf/' . $this->initializer->getDescriptor() . '.xml')); 
    7689        $xjconf->enableXIncludes(); 
    77         foreach ($extensions as $extension) { 
     90        foreach ($this->initializer->getExtensions() as $extension) { 
    7891            $xjconf->addExtension($extension); 
    7992        } 
    8093         
    81         $xjconf->parse($this->configFile); 
    82         $this->initializer->loadData($xjconf); 
     94        if (count($sources) == 0) { 
     95            $xjconf->parse($this->configFile); 
     96            $this->initializer->loadData($xjconf); 
     97        } else { 
     98            foreach ($sources as $source) { 
     99                $xjconf->parse($source); 
     100                $this->initializer->loadData($xjconf); 
     101            } 
     102        } 
     103         
    83104        file_put_contents($this->cacheFile, serialize($this->initializer->getCacheData())); 
    84105    } 
  • trunk/src/main/php/net/stubbles/websites/processors/stubProcessorResolverXJConfFactory.php

    r561 r584  
    5757 
    5858    /** 
     59     * returns a list of extensions for the parser 
     60     * 
     61     * @return  array<Extension> 
     62     */ 
     63    public function getExtensions() 
     64    { 
     65        return array(); 
     66    } 
     67 
     68    /** 
    5969     * will be called in case the stubXJConfProxy did not found the data in the 
    6070     * cache and the initializer has to load values from the facade 
  • trunk/src/main/php/net/stubbles/websites/variantmanager/stubVariantXJConfFactory.php

    r551 r584  
    7272 
    7373    /** 
     74     * returns a list of extensions for the parser 
     75     * 
     76     * @return  array<Extension> 
     77     */ 
     78    public function getExtensions() 
     79    { 
     80        return array(); 
     81    } 
     82 
     83    /** 
    7484     * will be called in case the stubXJConfProxy did not found the data in the 
    7585     * cache and the initializer has to load values from the facade 
  • trunk/src/test/php/net/stubbles/util/xjconf/stubXJConfProxyTestCase.php

    r580 r584  
    4242    protected $configFile; 
    4343    /** 
     44     * list of source files 
     45     * 
     46     * @var  array<string> 
     47     */ 
     48    protected $sourceFiles = array(); 
     49    /** 
    4450     * the cache file 
    4551     * 
     
    6571        $this->dir               = dirname(__FILE__); 
    6672        $this->configFile        = $this->dir . '/config.xml'; 
     73        $this->sourceFiles       = array($this->dir . '/config1.xml', $this->dir . '/config2.xml'); 
    6774        $this->cacheFile         = $this->dir . '/config.cache'; 
    6875        $this->xjConfProxy       = new stubXJConfProxy($this->xjConfInitializer, $this->dir, $this->dir); 
     
    7380  <config name=\"net.stubbles.mode\" value=\"test\" /> 
    7481</xj:configuration>"); 
     82        file_put_contents($this->sourceFiles[0], "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?> 
     83<xj:configuration 
     84    xmlns:xj=\"http://xjconf.net/XJConf\" 
     85    xmlns=\"http://stubbles.net/util/registry\"> 
     86  <config name=\"net.stubbles.mode\" value=\"test\" /> 
     87</xj:configuration>"); 
     88        file_put_contents($this->sourceFiles[1], "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?> 
     89<xj:configuration 
     90    xmlns:xj=\"http://xjconf.net/XJConf\" 
     91    xmlns=\"http://stubbles.net/util/registry\"> 
     92  <config name=\"net.stubbles.mode\" value=\"test\" /> 
     93</xj:configuration>"); 
    7594    } 
    7695 
     
    82101        if (file_exists($this->configFile) == true) { 
    83102            unlink($this->configFile); 
     103        } 
     104         
     105        foreach ($this->sourceFiles as $sourceFile) { 
     106            if (file_exists($sourceFile) == true) { 
     107                unlink($sourceFile); 
     108            } 
    84109        } 
    85110         
     
    99124        $this->xjConfInitializer->expectOnce('setCacheData', array(array('foo' => 'bar'))); 
    100125        $this->xjConfInitializer->expectNever('getCacheData'); 
     126        $this->xjConfInitializer->expectNever('getExtensions'); 
    101127        $this->xjConfProxy->process(); 
     128    } 
     129 
     130    /** 
     131     * assure that values are taken from cache is cache file is newer than source files 
     132     */ 
     133    public function testIsCachedAndCacheIsNewerWithSourceFiles() 
     134    { 
     135        file_put_contents($this->cacheFile, serialize(array('foo' => 'bar'))); 
     136        foreach ($this->sourceFiles as $sourceFile) { 
     137            touch($sourceFile, (time() - 100)); 
     138        } 
     139        touch($this->cacheFile, time()); 
     140        $this->xjConfInitializer->expectOnce('setCacheData', array(array('foo' => 'bar'))); 
     141        $this->xjConfInitializer->expectNever('getCacheData'); 
     142        $this->xjConfInitializer->expectNever('getExtensions'); 
     143        $this->xjConfProxy->process($this->sourceFiles); 
    102144    } 
    103145 
     
    114156        $this->xjConfInitializer->expectOnce('getCacheData'); 
    115157        $this->xjConfInitializer->setReturnValue('getCacheData', array('baz' => 'bar')); 
     158        $this->xjConfInitializer->setReturnValue('getExtensions', array()); 
    116159        $this->xjConfProxy->process(); 
    117160        $this->assertEqual(unserialize(file_get_contents($this->cacheFile)), array('baz' => 'bar')); 
     
    119162 
    120163    /** 
     164     * assure that values are taken from cache is cache file is newer than source files 
     165     */ 
     166    public function testIsCachedAndCacheIsOlderWithSourceFiles() 
     167    { 
     168        file_put_contents($this->cacheFile, serialize(array('foo' => 'bar'))); 
     169        foreach ($this->sourceFiles as $sourceFile) { 
     170            touch($sourceFile, (time())); 
     171        } 
     172        touch($this->cacheFile, time() - 100); 
     173        $this->xjConfInitializer->expectNever('setCacheData'); 
     174        $this->xjConfInitializer->expectCallcount('loadData', 2); 
     175        $this->xjConfInitializer->expectOnce('getCacheData'); 
     176        $this->xjConfInitializer->setReturnValue('getCacheData', array('baz' => 'bar')); 
     177        $this->xjConfInitializer->setReturnValue('getExtensions', array()); 
     178        $this->xjConfProxy->process($this->sourceFiles); 
     179        $this->assertEqual(unserialize(file_get_contents($this->cacheFile)), array('baz' => 'bar')); 
     180    } 
     181 
     182    /** 
    121183     * assure that values are created and cached if they were not cached before 
    122184     */ 
     
    127189        $this->xjConfInitializer->expectOnce('getCacheData'); 
    128190        $this->xjConfInitializer->setReturnValue('getCacheData', array('baz' => 'bum')); 
     191        $this->xjConfInitializer->setReturnValue('getExtensions', array()); 
    129192        $this->xjConfProxy->process(); 
    130193        $this->assertEqual(unserialize(file_get_contents($this->cacheFile)), array('baz' => 'bum')); 
    131194    } 
     195 
     196    /** 
     197     * assure that values are created and cached if they were not cached before 
     198     */ 
     199    public function testIsNotCachedWithSourceFiles() 
     200    { 
     201        $this->xjConfInitializer->expectNever('setCacheData'); 
     202        $this->xjConfInitializer->expectCallcount('loadData', 2); 
     203        $this->xjConfInitializer->expectOnce('getCacheData'); 
     204        $this->xjConfInitializer->setReturnValue('getCacheData', array('baz' => 'bum')); 
     205        $this->xjConfInitializer->setReturnValue('getExtensions', array()); 
     206        $this->xjConfProxy->process($this->sourceFiles); 
     207        $this->assertEqual(unserialize(file_get_contents($this->cacheFile)), array('baz' => 'bum')); 
     208    } 
    132209} 
    133210?>