Changeset 538

Show
Ignore:
Timestamp:
04/15/07 13:03:27 (1 year ago)
Author:
mikey
Message:

introduced net.stubbles.util.xjconf.stubXJConfProxy which allows caching of XJConf results via the net.stubbles.util.xjconf.stubXJConfInitializer interface

Files:

Legend:

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

    r443 r538  
    88 */ 
    99stubClassLoader::load('net.stubbles.ipo.interceptors.stubInterceptorInitializer', 
    10                       'net.stubbles.util.stubFactory', 
    1110                      'net.stubbles.util.xjconf.xjconf' 
    1211); 
     
    1615 * @package     stubbles 
    1716 * @subpackage  ipo_interceptors 
     17 * @todo        write unit test 
    1818 */ 
    19 class stubInterceptorXJConfInitializer extends stubBaseObject implements stubInterceptorInitializer 
     19class stubInterceptorXJConfInitializer extends stubBaseObject implements stubInterceptorInitializer, stubXJConfInitializer 
    2020{ 
    21     /** 
    22      * name of the config file to use for initializing 
    23      * 
    24      * @var  string 
    25      */ 
    26     protected $configFile; 
    2721    /** 
    2822     * list of pre interceptors 
     
    3731     */ 
    3832    protected $postInterceptors = array(); 
    39      
     33 
    4034    /** 
    41      * constructor 
     35     * returns the descriptor that identifies this initializer 
     36     * 
     37     * @return  string 
    4238     */ 
    43     public function __construct() 
     39    public function getDescriptor() 
    4440    { 
    45         $this->configFile = stubConfig::getConfigPath() . '/xml/interceptors.xml'; 
     41        return 'interceptors'; 
    4642    } 
    47      
     43 
    4844    /** 
    49      * sets the name of the config gile to use 
     45     * returns the data to cache  
    5046     * 
    51      * @param  string  $configFile 
     47     * @return  array 
    5248     */ 
    53     public function setConfigFile($configFile
     49    public function getCacheData(
    5450    { 
    55         $this->configFile = $configFile; 
     51        $cacheData = array('preInterceptors' => array(), 'postInterceptors' => array()); 
     52        foreach ($this->preInterceptors as $preInterceptor) { 
     53            if ($preInterceptor instanceof stubSerializable) { 
     54                $cacheData['preInterceptors'][] = $preInterceptor->getSerialized(); 
     55            } else { 
     56                $cacheData['preInterceptors'][] = $preInterceptor->getClassName(); 
     57            } 
     58        } 
     59         
     60        foreach ($this->postInterceptors as $postInterceptor) { 
     61            if ($postInterceptor instanceof stubSerializable) { 
     62                $cacheData['postInterceptors'][] = $postInterceptor->getSerialized(); 
     63            } else { 
     64                $cacheData['postInterceptors'][] = $postInterceptor->getClassName(); 
     65            } 
     66        } 
     67         
     68        return $cacheData; 
    5669    } 
    57      
     70 
     71    /** 
     72     * sets the data from the cache 
     73     * 
     74     * @param  array  $cacheData 
     75     */ 
     76    public function setCacheData(array $cacheData) 
     77    { 
     78        foreach ($cacheData['preInterceptors'] as $preInterceptor) { 
     79            if ($preInterceptor instanceof stubSerializable) { 
     80                $this->preInterceptors[] = $preInterceptor->getUnserialized(); 
     81            } else { 
     82                stubClassLoader::load($preInterceptor); 
     83                $nqClassName = stubClassLoader::getNonQualifiedClassName($preInterceptor); 
     84                if (class_exists($nqClassName, false) == false) { 
     85                    stubClassLoader::load($preInterceptor); 
     86                } 
     87                 
     88                $this->preInterceptors[] = new $nqClassName(); 
     89            } 
     90        } 
     91         
     92        foreach ($cacheData['postInterceptors'] as $postInterceptor) { 
     93            if ($postInterceptor instanceof stubSerializable) { 
     94                $this->postInterceptors[] = $postInterceptor->getUnserialized(); 
     95            } else { 
     96                $nqClassName = stubClassLoader::getNonQualifiedClassName($postInterceptor); 
     97                if (class_exists($nqClassName, false) == false) { 
     98                    stubClassLoader::load($postInterceptor); 
     99                } 
     100                 
     101                $this->postInterceptors[] = new $nqClassName(); 
     102            } 
     103        } 
     104    } 
     105 
     106    /** 
     107     * will be called in case the stubXJConfProxy did not found the data in the 
     108     * cache and the initializer has to load values from the facade 
     109     * 
     110     * @param  stubXJConfFacade  $xjconf 
     111     */ 
     112    public function loadData(stubXJConfFacade $xjconf) 
     113    { 
     114        $this->preInterceptors  = $xjconf->getConfigValue('preInterceptors'); 
     115        $this->postInterceptors = $xjconf->getConfigValue('postInterceptors'); 
     116    } 
     117 
    58118    /** 
    59119     * initialize the interceptors 
     
    63123    public function init() 
    64124    { 
    65         $xjconf = new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance())); 
    66         $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/interceptors.xml')); 
    67         $xjconf->enableXIncludes(); 
    68         $xjconf->parse($this->configFile); 
    69         $this->preInterceptors  = $xjconf->getConfigValue('preInterceptors'); 
    70         $this->postInterceptors = $xjconf->getConfigValue('postInterceptors'); 
     125        $xjconfProxy = new stubXJConfProxy($this); 
     126        $xjconfProxy->process(); 
    71127    } 
    72      
     128 
     129    /** 
     130     * sets the list of pre interceptors 
     131     * 
     132     * @param  array<stubPreInterceptor>  $preInterceptors 
     133     */ 
     134    public function setPreInterceptors(array $preInterceptors) 
     135    { 
     136        $this->preInterceptors = $preInterceptors; 
     137    } 
     138 
    73139    /** 
    74140     * returns the list of pre interceptors 
     
    80146        return $this->preInterceptors; 
    81147    } 
    82      
     148 
     149    /** 
     150     * sets the list of pre interceptors 
     151     * 
     152     * @param  array<stubPostInterceptor>  $postInterceptors 
     153     */ 
     154    public function setPostInterceptors(array $postInterceptors) 
     155    { 
     156        $this->postInterceptors = $postInterceptors; 
     157    } 
     158 
    83159    /** 
    84160     * returns the list of post interceptors 
  • trunk/src/main/php/net/stubbles/ipo/interceptors/stubPostInterceptor.php

    r473 r538  
    1616 * @subpackage  ipo_interceptors 
    1717 */ 
    18 interface stubPostInterceptor 
     18interface stubPostInterceptor extends stubObject 
    1919{ 
    2020    /** 
  • trunk/src/main/php/net/stubbles/ipo/interceptors/stubPreInterceptor.php

    r473 r538  
    1616 * @subpackage  ipo_interceptors 
    1717 */ 
    18 interface stubPreInterceptor 
     18interface stubPreInterceptor extends stubObject 
    1919{ 
    2020    /** 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionData.php

    r400 r538  
    1313 * @subpackage  rdbms 
    1414 */ 
    15 class stubDatabaseConnectionData extends stubBaseObject 
     15class stubDatabaseConnectionData extends stubSerializableObject 
    1616{ 
    1717    /** 
     
    5656     */ 
    5757    protected $driverOptions = array(); 
    58      
     58 
    5959    /** 
    6060     * set the id to use for the connection 
     
    7070        $this->id = $id; 
    7171    } 
    72      
     72 
    7373    /** 
    7474     * return the id to use for the connection 
     
    8080        return $this->id; 
    8181    } 
    82      
     82 
    8383    /** 
    8484     * checks whether a value is equal to the class 
     
    9999        return false; 
    100100    } 
    101      
     101 
    102102    /** 
    103103     * sets the full qualified name of the class to use for the connection 
     
    109109        $this->fqClassName = $fqClassName; 
    110110    } 
    111      
     111 
    112112    /** 
    113113     * returns the full qualified name of the class to use for the connection 
     
    119119        return $this->fqClassName; 
    120120    } 
    121      
     121 
    122122    /** 
    123123     * sets the Data Source Name 
     
    129129        $this->dsn = $dsn; 
    130130    } 
    131      
     131 
    132132    /** 
    133133     * returns the Data Source Name 
     
    139139        return $this->dsn; 
    140140    } 
    141      
     141 
    142142    /** 
    143143     * sets the user name 
     
    149149        $this->userName = $userName; 
    150150    } 
    151      
     151 
    152152    /** 
    153153     * returns the user name 
     
    159159        return $this->userName; 
    160160    } 
    161      
     161 
    162162    /** 
    163163     * sets the password 
     
    169169        $this->password = $password; 
    170170    } 
    171      
     171 
    172172    /** 
    173173     * returns the user password 
     
    179179        return $this->password; 
    180180    } 
    181      
     181 
    182182    /** 
    183183     * sets a key=>value array of driver-specific connection options 
     
    189189        $this->driverOptions = $driverOptions; 
    190190    } 
    191      
     191 
    192192    /** 
    193193     * returns a key=>value array of driver-specific connection options 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseInitializer.php

    r473 r538  
    88 */ 
    99stubClassLoader::load('net.stubbles.ipo.interceptors.stubPreInterceptor', 
    10                       'net.stubbles.util.stubFactory', 
     10                      'net.stubbles.rdbms.stubDatabaseConnectionPool', 
    1111                      'net.stubbles.util.xjconf.xjconf' 
    1212); 
     
    1717 * @subpackage  rdbms 
    1818 */ 
    19 class stubDatabaseInitializer extends stubBaseObject implements stubPreInterceptor 
     19class stubDatabaseInitializer extends stubBaseObject implements stubPreInterceptor, stubXJConfInitializer 
    2020{ 
     21    /** 
     22     * returns the descriptor that identifies this initializer 
     23     * 
     24     * @return  string 
     25     */ 
     26    public function getDescriptor() 
     27    { 
     28        return 'rdbms'; 
     29    } 
     30 
     31    /** 
     32     * returns the data to cache  
     33     * 
     34     * @return  array 
     35     */ 
     36    public function getCacheData() 
     37    { 
     38        $cacheData = array(); 
     39        foreach (stubDatabaseConnectionPool::getConnectionDataIds() as $connectionDataId) { 
     40            $cacheData[$connectionDataId] = stubDatabaseConnectionPool::getConnectionData($connectionDataId)->getSerialized(); 
     41        } 
     42         
     43        return $cacheData; 
     44    } 
     45 
     46    /** 
     47     * sets the data from the cache 
     48     * 
     49     * @param  array  $cacheData 
     50     */ 
     51    public function setCacheData(array $cacheData) 
     52    { 
     53        foreach ($cacheData as $serialized) { 
     54            stubDatabaseConnectionPool::addConnectionData($serialized->getUnserialized()); 
     55        } 
     56    } 
     57 
     58    /** 
     59     * will be called in case the stubXJConfProxy did not found the data in the 
     60     * cache and the initializer has to load values from the facade 
     61     * 
     62     * @param  stubXJConfFacade  $xjconf 
     63     */ 
     64    public function loadData(stubXJConfFacade $xjconf) 
     65    { 
     66        // intentionally empty 
     67    } 
     68 
    2169    /** 
    2270     * initialize the Registry with configuration values from given configuration file 
    2371     * 
    24      * @param   string  $configFile 
    2572     * @throws  stubXJConfException 
    2673     */ 
    27     public static function init($configFile
     74    public function init(
    2875    { 
    29         $xjconf = new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance())); 
    30         $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/rdbms.xml')); 
    31         $xjconf->enableXIncludes(); 
    32         $xjconf->parse($configFile); 
     76        $xjconfProxy = new stubXJConfProxy($this); 
     77        $xjconfProxy->process(); 
    3378    } 
    34      
     79 
    3580    /** 
    3681     * does the preprocessing stuff 
    3782     * 
    38      * @param  stubRequest   $request   access to request data 
    39      * @param  stubSession   $session   access to session data 
    40      * @param  stubResponse  $response  access to response data 
     83     * @param   stubRequest   $request   access to request data 
     84     * @param   stubSession   $session   access to session data 
     85     * @param   stubResponse  $response  access to response data 
     86     * @throws  stubXJConfException 
    4187     */ 
    4288    public function preProcess(stubRequest $request, stubSession $session, stubResponse $response) 
    4389    { 
    44         self::init(stubConfig::getConfigPath() . '/xml/rdbms.xml'); 
     90        $this->init(); 
    4591    } 
    4692} 
  • trunk/src/main/php/net/stubbles/util/stubRegistryXJConfInitializer.php

    r528 r538  
    11<?php 
    22/** 
    3  * Class for initializing the Registry
     3 * Class for initializing the stubRegistry via XJConf
    44 *  
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    77 * @subpackage  util 
    88 */ 
    9 stubClassLoader::load('net.stubbles.util.stubFactory', 
    10                       'net.stubbles.util.stubRegistry', 
     9stubClassLoader::load('net.stubbles.util.stubRegistry', 
    1110                      'net.stubbles.util.stubRegistryInitializer', 
    1211                      'net.stubbles.util.xjconf.xjconf' 
    1312); 
    1413/** 
    15  * Class for initializing the Registry
     14 * Class for initializing the stubRegistry via XJConf
    1615 * 
    1716 * @package     stubbles 
    1817 * @subpackage  util 
    1918 */ 
    20 class stubRegistryXJConfInitializer extends stubBaseObject implements stubRegistryInitializer 
     19class stubRegistryXJConfInitializer extends stubBaseObject implements stubRegistryInitializer, stubXJConfInitializer 
    2120{ 
    2221    /** 
    23      * name of the config file to use for initializing 
     22     * returns the descriptor that identifies this initializer 
    2423     * 
    25      * @var  string 
     24     * @return  string 
    2625     */ 
    27     protected $configFile; 
    28     /** 
    29      * name of the cache file to use 
    30      * 
    31      * @var  string 
    32      */ 
    33     protected $cacheFile; 
    34  
    35     /** 
    36      * constructor 
    37      */ 
    38     public function __construct() 
     26    public function getDescriptor() 
    3927    { 
    40         $this->configFile = stubConfig::getConfigPath() . '/xml/config.xml'; 
    41         $this->cacheFile  = stubConfig::getCachePath() . '/config.cache'; 
     28        return 'config'; 
    4229    } 
    4330 
    4431    /** 
    45      * sets the name of the config file to use 
     32     * returns the data to cache  
    4633     * 
    47      * @param  string  $configFile 
     34     * @return  array 
    4835     */ 
    49     public function setConfigFile($configFile
     36    public function getCacheData(
    5037    { 
    51         $this->configFile = $configFile; 
     38        $cacheData = array(); 
     39        foreach (stubRegistry::getConfigKeys() as $key) { 
     40            $cacheData[$key] = stubRegistry::getConfig($key); 
     41        } 
     42         
     43        return $cacheData; 
     44    } 
     45 
     46    /** 
     47     * sets the data from the cache 
     48     * 
     49     * @param  array  $cacheData 
     50     */ 
     51    public function setCacheData(array $cacheData) 
     52    { 
     53        foreach ($cacheData as $key => $value) { 
     54            stubRegistry::setConfig($key, $value); 
     55        } 
     56    } 
     57 
     58    /** 
     59     * will be called in case the stubXJConfProxy did not found the data in the 
     60     * cache and the initializer has to load values from the facade 
     61     * 
     62     * @param  stubXJConfFacade  $xjconf 
     63     */ 
     64    public function loadData(stubXJConfFacade $xjconf) 
     65    { 
     66        // intentionally empty 
    5267    } 
    5368 
     
    5570     * initialize the Registry with configuration values from given configuration file 
    5671     * 
    57      * @param   string  $configFile 
    5872     * @throws  stubXJConfException 
    5973     */ 
    6074    public function init() 
    6175    { 
    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(); 
    71         $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/registry.xml')); 
    72         $xjconf->enableXIncludes(); 
    73         $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())); 
     76        $xjconfProxy = new stubXJConfProxy($this); 
     77        $xjconfProxy->process(); 
    12078    } 
    12179} 
  • trunk/src/main/php/net/stubbles/util/xjconf/xjconf.php

    r535 r538  
    2424                      'net.stubbles.util.xjconf.stubXJConfFacade', 
    2525                      'net.stubbles.util.xjconf.stubXJConfException', 
     26                      'net.stubbles.util.xjconf.stubXJConfInitializer', 
     27                      'net.stubbles.util.xjconf.stubXJConfProxy', 
    2628                      'net.stubbles.util.xjconf.stubConfigXJConfExtension' 
    2729); 
  • trunk/src/test/php/net/stubbles/integration/DatabaseTestCase.php

    r400 r538  
    2121    public function testDatabaseInitializer() 
    2222    { 
    23         stubDatabaseInitializer::init(stubConfig::getConfigPath() . '/xml/rdbms.xml'); 
     23        $dbInitializer = new stubDatabaseInitializer(); 
     24        $dbInitializer->init(); 
    2425        $this->assertTrue(stubDatabaseConnectionPool::hasConnectionData()); 
    2526        $connectionData = stubDatabaseConnectionPool::getConnectionData(); 
  • trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php

    r482 r538  
    2424        $this->addTestFile($dir . '/stubDatabaseConnectionDataTestCase.php'); 
    2525        $this->addTestFile($dir . '/stubDatabaseConnectionPoolTestCase.php'); 
     26        $this->addTestFile($dir . '/stubDatabaseInitializerTestCase.php'); 
    2627         
    2728        // criteria 
  • trunk/src/test/php/net/stubbles/util/UtilTestSuite.php

    r531 r538  
    5353        $this->addTestFile($dir . '/validators/stubRegexValidatorTestCase.php'); 
    5454        $this->addTestFile($dir . '/validators/stubXorValidatorTestCase.php'); 
     55         
     56        // xjconf 
     57        $this->addTestFile($dir . '/xjconf/stubXJConfProxyTestCase.php'); 
    5558    } 
    5659} 
  • trunk/src/test/php/net/stubbles/util/stubRegistryXJConfInitializerTestCase.php

    r526 r538  
    33 * Tests for net.stubbles.util.stubRegistryXJConfInitializer 
    44 * 
    5  * @author      Frank Kleine <frank@kl-s.com
     5 * @author      Frank Kleine <mikey@stubbles.net
    66 * @package     stubbles 
    77 * @subpackage  util_test 
    88 */ 
    9 stubClassLoader::load('net.stubbles.util.stubRegistry', 
    10                       'net.stubbles.util.stubRegistryXJConfInitializer' 
    11 ); 
    12 Mock::generate('stubXJConfFacade'); 
    13 Mock::generatePartial('stubRegistryXJConfInitializer', 
    14                       'MockstubRegistryXJConfInitializer', 
    15                       array('isCacheUpToDate', 
    16                             'getCacheData', 
    17                             'setCacheData', 
    18                             'createXJConfFacade' 
    19                       ) 
    20 ); 
     9stubClassLoader::load('net.stubbles.util.stubRegistryXJConfInitializer'); 
    2110/** 
    2211 * Tests for net.stubbles.util.stubRegistryXJConfInitializer 
     
    3019     * instance to test 
    3120     * 
    32      * @var  MockstubRegistryXJConfInitializer 
     21     * @var  stubRegistryXJConfInitializer 
    3322     */ 
    3423    protected $registryXJConfInitializer; 
     
    3928    public function setUp() 
    4029    { 
    41         $this->registryXJConfInitializer = new MockstubRegistryXJConfInitializer(); 
    42         $this->registryXJConfInitializer->setReturnValue('createXJConfFacade', new MockstubXJConfFacade()); 
     30        $this->registryXJConfInitializer = new stubRegistryXJConfInitializer(); 
    4331    } 
    4432 
     
    5442 
    5543    /** 
    56      * assure that values are cached if they were not cached before 
     44     * test that the descriptor is correct 
    5745     */ 
    58     public function testNonCached() 
     46    public function testDescriptor() 
     47    { 
     48        $this->assertEqual($this->registryXJConfInitializer->getDescriptor(), 'config'); 
     49    } 
     50 
     51    /** 
     52     * assure that values are returned correct 
     53     */ 
     54    public function testGetCacheData() 
    5955    { 
    6056        stubRegistry::setConfig('foo', 'bar'); 
    6157        stubRegistry::setConfig('baz', 313); 
    62         $this->registryXJConfInitializer->setReturnValue('isCacheUpToDate', false); 
    63         $this->registryXJConfInitializer->expect('setCacheData', array(array('foo' => 'bar', 'baz' => 313))); 
    64         $this->registryXJConfInitializer->init(); 
     58        $this->assertEqual($this->registryXJConfInitializer->getCacheData(), array('foo' => 'bar', 'baz' => 313)); 
    6559    } 
    6660 
    6761    /** 
    68      * assure that values are read from cache 
     62     * assure that values are set correct 
    6963     */ 
    70     public function testCached() 
     64    public function testSetCacheData() 
    7165    { 
    72         $this->registryXJConfInitializer->setReturnValue('isCacheUpToDate', true); 
    73         $this->registryXJConfInitializer->setReturnValue('getCacheData', array('foo' => 'bar', 'baz' => 313)); 
    74         $this->registryXJConfInitializer->expectNever('setCacheData'); 
    75         $this->registryXJConfInitializer->init(); 
    76          
     66        $this->registryXJConfInitializer->setCacheData(array('foo' => 'bar', 'baz' => 313)); 
    7767        $this->assertEqual(stubRegistry::getConfigKeys(), array('foo', 'baz')); 
    7868        $this->assertEqual(stubRegistry::getConfig('foo'), 'bar');