Changeset 589

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

added caching to stubPageXJConfFactory

Files:

Legend:

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

    r317 r589  
    4242        return $this->name; 
    4343    } 
    44      
     44 
     45    /** 
     46     * returns a list of required class names 
     47     * 
     48     * @return  array<string> 
     49     */ 
     50    public function getRequiredClassNames() 
     51    { 
     52        return array(); 
     53    } 
     54 
    4555    /** 
    4656     * checks whether the page element is available or not 
  • trunk/src/main/php/net/stubbles/websites/stubPage.php

    r277 r589  
    8989     * returns the list of elements 
    9090     * 
    91      * @return unknown 
     91     * @return array<string,stubPageElement> 
    9292     */ 
    9393    public function getElements() 
  • trunk/src/main/php/net/stubbles/websites/stubPageElement.php

    r317 r589  
    1717 * @subpackage  websites 
    1818 */ 
    19 interface stubPageElement 
     19interface stubPageElement extends stubObject 
    2020{ 
    2121    /** 
     
    3232     */ 
    3333    public function getName(); 
    34      
     34 
     35    /** 
     36     * returns a list of required class names 
     37     * 
     38     * @return  array<string> 
     39     */ 
     40    public function getRequiredClassNames(); 
     41 
    3542    /** 
    3643     * checks whether the page element is available or not 
  • trunk/src/main/php/net/stubbles/websites/stubPageXJConfFactory.php

    r580 r589  
    77 * @subpackage  websites 
    88 */ 
    9 stubClassLoader::load('net.stubbles.util.stubFactory', 
    10                       'net.stubbles.util.xjconf.xjconf', 
    11                       'net.stubbles.util.xjconf.xjconfReal', 
     9stubClassLoader::load('net.stubbles.util.xjconf.xjconf', 
    1210                      'net.stubbles.websites.stubPageConfigurationException', 
    1311                      'net.stubbles.websites.stubPageFactory' 
     
    2725     */ 
    2826    private static $xjconf; 
     27    /** 
     28     * path to cache files 
     29     * 
     30     * @var  string 
     31     */ 
     32    protected $cachePath; 
     33    /** 
     34     * path to config files 
     35     * 
     36     * @var  string 
     37     */ 
     38    protected $configPath; 
    2939 
    3040    /** 
    3141     * constructor 
     42     * 
     43     * @param  string  $cachePath   optional  path to cache files 
     44     * @param  string  $configPath  optional  path to config files 
    3245     */ 
    33     public function __construct(
     46    public function __construct($cachePath = null, $configPath = null
    3447    { 
    35         if (null == self::$xjconf) { 
    36             self::$xjconf = new stubXJConfFacade(new XJConfFacade(array('__default' => stubXJConfLoader::getInstance()))); 
    37             self::$xjconf->addDefinitions(stubFactory::getResourceURIs('xjconf/websites.xml')); 
    38         } 
     48        $this->cachePath  = ((null == $cachePath) ? (stubConfig::getCachePath() . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR) : ($cachePath)); 
     49        $this->configPath = ((null == $configPath) ? (stubConfig::getConfigPath() . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR) : ($configPath)); 
    3950    } 
    40      
     51 
    4152    /** 
    4253     * checks whether the page factory knows the page or not 
     
    4758    public function hasPage($configSource) 
    4859    { 
    49         return file_exists(stubConfig::getConfigPath() . '/xml/pages/' . $configSource . '.xml'); 
     60        return file_exists($this->configPath . $configSource . '.xml'); 
    5061    } 
    5162 
     
    5970    public function getPage($configSource) 
    6071    { 
    61         $configSource = stubConfig::getConfigPath() . '/xml/pages/' . $configSource . '.xml'; 
     72        $configSource = str_replace('/', DIRECTORY_SEPARATOR, $configSource); 
     73        $cacheSource  = $this->cachePath . $configSource . '.cache'; 
     74        $configSource = $this->configPath . $configSource . '.xml'; 
     75        if (file_exists($cacheSource) && filemtime($cacheSource) >= filemtime($configSource)) { 
     76            $cachedPage = unserialize(file_get_contents($cacheSource)); 
     77            foreach ($cachedPage['classes'] as $class) { 
     78                stubClassLoader::load($class); 
     79            } 
     80             
     81            $page = unserialize($cachedPage['data']); 
     82            return $page; 
     83        } 
     84         
     85        $page = $this->getPageFromXJConf($configSource); 
     86        $cachedPage = array('classes' => array($page->getClassName()), 
     87                            'data'    => serialize($page) 
     88                      ); 
     89        foreach ($page->getElements() as $pageElement) { 
     90            $cachedPage['classes'][] = $pageElement->getClassName(); 
     91            foreach ($pageElement->getRequiredClassNames() as $requiredClassName) { 
     92                $cachedPage['classes'][] = $requiredClassName; 
     93            } 
     94        } 
     95         
     96        if (file_exists(dirname($cacheSource)) == false) { 
     97            mkdir(dirname($cacheSource), 0700, true); 
     98        } 
     99         
     100        file_put_contents($cacheSource, serialize($cachedPage)); 
     101        return $page; 
     102    } 
     103 
     104    /** 
     105     * returns the configured stubPage instance 
     106     * 
     107     * @param   string    $configSource   source of the page configuration to use 
     108     * @return  stubPage 
     109     * @throws  stubPageConfigurationException 
     110     */ 
     111    protected function getPageFromXJConf($configSource) 
     112    { 
     113        if (null == self::$xjconf) { 
     114            stubClassLoader::load('net.stubbles.util.stubFactory', 
     115                                  'net.stubbles.util.xjconf.xjconfReal' 
     116            ); 
     117            self::$xjconf = new stubXJConfFacade(new XJConfFacade(array('__default' => stubXJConfLoader::getInstance()))); 
     118            self::$xjconf->addDefinitions(stubFactory::getResourceURIs('xjconf/websites.xml')); 
     119        } 
     120         
    62121        try { 
    63122            self::$xjconf->parse($configSource); 
     123            return self::$xjconf->getConfigValue('page'); 
    64124        } catch (stubXJConfException $xjce) { 
    65125            throw new stubPageConfigurationException('Can not read page configuration from ' . $configSource, $xjce); 
    66126        } 
    67  
    68         return self::$xjconf->getConfigValue('page'); 
    69127    } 
    70128} 
  • trunk/src/main/php/net/stubbles/websites/xml/stubXMLPageElementDecorator.php

    r565 r589  
    1515 * @subpackage  websites 
    1616 */ 
    17 abstract class stubXMLPageElementDecorator implements stubXMLPageElement 
     17abstract class stubXMLPageElementDecorator extends stubBaseObject implements stubXMLPageElement 
    1818{ 
    1919    /** 
     
    4949    public function getName() { 
    5050        return $this->element->getName(); 
     51    } 
     52 
     53    /** 
     54     * returns a list of required class names 
     55     * 
     56     * @return  array<string> 
     57     */ 
     58    public function getRequiredClassNames() 
     59    { 
     60        return array($this->element->getClassName()); 
    5161    } 
    5262 
  • trunk/src/test/runIntegration.php

    r563 r589  
    3636        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/ProcessorTestCase.php'); 
    3737        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/RegistryTestCase.php'); 
     38        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/stubPageXJConfFactoryTestCase.php'); 
    3839        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/stubRequestValueErrorXJConfFactoryTestCase.php'); 
    3940        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/VariantManagerTestCase.php');