Changeset 1467

Show
Ignore:
Timestamp:
03/27/08 14:35:41 (5 months ago)
Author:
mikey
Message:

implemented enhancement #133: xml processor should use website cache

Files:

Legend:

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

    r1442 r1467  
    1111                      'net::stubbles::lang::exceptions::stubRuntimeException', 
    1212                      'net::stubbles::util::stubRegistry', 
     13                      'net::stubbles::websites::cache::stubCachableProcessor', 
    1314                      'net::stubbles::websites::processors::stubAbstractProcessor', 
    1415                      'net::stubbles::websites::processors::stubPageBasedProcessor', 
     
    2021 * Default processor delivered by stubbles. 
    2122 * 
     23 * For a customized version it is recommended to extend this processor. Possible 
     24 * useful methods to be overwritten in a customized version: 
     25 * - protected function getXMLGenerators() 
     26 *   This method should return a list of full qualified class names denoting 
     27 *   xml generator implementations. All these will be used to generate the 
     28 *   result xml document. 
     29 * - protected function create*() 
     30 *   These methods return instances that are used to create the result xml 
     31 *   document. 
     32 * 
    2233 * @package     stubbles 
    2334 * @subpackage  websites_xml 
    2435 */ 
    25 class stubXMLProcessor extends stubAbstractProcessor implements stubPageBasedProcessor 
     36class stubXMLProcessor extends stubAbstractProcessor implements stubPageBasedProcessor, stubCachableProcessor 
    2637{ 
    2738    /** 
    2839     * list of xml generators to be used to create the dom tree 
    2940     * 
    30      * @var  array<string
     41     * @var  array<stubXMLGenerator
    3142     */ 
    3243    protected $xmlGenerators = array(); 
     
    4354     */ 
    4455    protected $pageName; 
    45  
    46     /** 
    47      * constructor 
    48      * 
    49      * @param  stubRequest   $request   the current request 
    50      * @param  stubSession   $session   the current session 
    51      * @param  stubResponse  $response  the current response 
    52      */ 
    53     public function __construct(stubRequest $request, stubSession $session, stubResponse $response) 
    54     { 
    55         parent::__construct($request, $session, $response); 
    56         $this->configureXMLGenerators(); 
    57     } 
    58  
    59     /** 
    60      * configure the xml generators 
    61      */ 
    62     protected function configureXMLGenerators() 
    63     { 
    64         $this->xmlGenerators = array('net::stubbles::websites::xml::generator::stubSessionXMLGenerator', 
    65                                      'net::stubbles::websites::xml::generator::stubPageXMLGenerator', 
    66                                      'net::stubbles::websites::xml::generator::stubRequestXMLGenerator' 
    67                                ); 
    68     } 
     56    /** 
     57     * binder to be used for ioc 
     58     * 
     59     * @var  stubBinder 
     60     */ 
     61    protected $binder; 
    6962 
    7063    /** 
     
    7972        $this->page     = $pageFactory->getPage($this->pageName); 
    8073        $this->session->putValue('net.stubbles.websites.lastPage', $this->pageName); 
    81     } 
    82  
    83     /** 
    84      * processes the request 
    85      */ 
    86     public function process() 
     74        $this->initializeGenerators(); 
     75    } 
     76 
     77    /** 
     78     * helper method to initialize the xml generator instances 
     79     * 
     80     * @throws  stubRuntimeException 
     81     */ 
     82    protected function initializeGenerators() 
    8783    { 
    8884        $binder = stubRegistry::get(stubBinder::REGISTRY_KEY); 
    8985        if (($binder instanceof stubBinder) === false) { 
    90             throw new stubRuntimeException('XML/XSL view engine requires IOC.'); 
     86            throw new stubRuntimeException('XML/XSL view engine requires IoC.'); 
    9187        } 
    9288         
    9389        $binder->bind('stubPage')->toInstance($this->page); 
    94         $injector        = $binder->getInjector(); 
     90        $injector = $binder->getInjector(); 
     91        foreach ($this->getXMLGenerators() as $xmlGeneratorClassName) { 
     92            $this->xmlGenerators[] = $injector->getInstance($xmlGeneratorClassName); 
     93        } 
     94    } 
     95 
     96    /** 
     97     * configure the xml generators 
     98     * 
     99     * @return  array<string> 
     100     */ 
     101    protected function getXMLGenerators() 
     102    { 
     103        return array('net::stubbles::websites::xml::generator::stubSessionXMLGenerator', 
     104                     'net::stubbles::websites::xml::generator::stubPageXMLGenerator', 
     105                     'net::stubbles::websites::xml::generator::stubRequestXMLGenerator' 
     106               ); 
     107    } 
     108 
     109    /** 
     110     * adds the cache variables for the current request and returns whether 
     111     * response is cachable or not 
     112     * 
     113     * @param   stubWebsiteCache  $websiteCache 
     114     * @return  bool 
     115     */ 
     116    public function addCacheVars(stubWebsiteCache $cache) 
     117    { 
     118        foreach ($this->xmlGenerators as $xmlGenerator) { 
     119            if ($xmlGenerator->isCachable() === false) { 
     120                return false; 
     121            } 
     122             
     123            $cache->addCacheVars($xmlGenerator->getCacheVars()); 
     124            $cache->addUsedFiles($xmlGenerator->getUsedFiles()); 
     125        } 
     126         
     127        $cache->addCacheVar('page', $this->pageName); 
     128        return true; 
     129    } 
     130 
     131    /** 
     132     * returns the name of the current page 
     133     * 
     134     * Non-page-based processors should return another unique identifier for 
     135     * the current request if they want to implement this interface. 
     136     * 
     137     * @return  string 
     138     */ 
     139    public function getPageName() 
     140    { 
     141        return $this->pageName; 
     142    } 
     143 
     144    /** 
     145     * processes the request 
     146     */ 
     147    public function process() 
     148    { 
    95149        $xmlStreamWriter = $this->createXMLStreamWriter(); 
    96150        $xmlStreamWriter->writeStartElement('document'); 
    97151        $xmlStreamWriter->writeAttribute('page', $this->pageName); 
    98152        $xmlSerializer = $this->createXMLSerializer(); 
    99         foreach ($this->xmlGenerators as $xmlGeneratorClassName) { 
    100             $xmlGenerator = $injector->getInstance($xmlGeneratorClassName); 
     153        foreach ($this->xmlGenerators as $xmlGenerator) { 
    101154            $xmlGenerator->generate($xmlStreamWriter, $xmlSerializer); 
    102155        } 
  • trunk/src/test/php/net/stubbles/integration/ProcessorTestCase.php

    r1445 r1467  
    1616class ProcessorTestCase extends PHPUnit_Framework_TestCase 
    1717{ 
     18    /** 
     19     * set up test environment 
     20     */ 
     21    public function setUp() 
     22    { 
     23        stubRegistry::set(stubBinder::REGISTRY_KEY, new stubBinder($this->getMock('stubInjector'))); 
     24    } 
     25 
     26    /** 
     27     * clean up test environment 
     28     */ 
     29    public function tearDown() 
     30    { 
     31        stubRegistry::remove(stubBinder::REGISTRY_KEY); 
     32    } 
     33 
    1834    /** 
    1935     * helper method 
  • trunk/src/test/php/net/stubbles/websites/xml/stubXMLProcessorTestCase.php

    r1442 r1467  
    9999        $this->mockPageFactory->expects($this->once())->method('getPage')->will($this->returnValue($this->page)); 
    100100        $this->mockSession->expects($this->at(0))->method('putValue')->with($this->equalTo('net.stubbles.websites.lastPage'), $this->equalTo('index')); 
    101         $this->xmlProcessor->selectPage($this->mockPageFactory); 
    102101    } 
    103102 
     
    116115     * @expectedException  stubRuntimeException 
    117116     */ 
    118     public function processWithoutBinderInRegistryThrowsRuntimeException() 
     117    public function selectPageWithoutBinderInRegistryThrowsRuntimeException() 
    119118    { 
    120119        stubRegistry::remove(stubBinder::REGISTRY_KEY); 
    121         $this->xmlProcessor->expects($this->never())->method('createXMLStreamWriter'); 
    122         $this->xmlProcessor->expects($this->never())->method('createXMLSerializer'); 
    123         $this->xmlProcessor->expects($this->never())->method('createSkinGenerator'); 
    124         $this->xmlProcessor->expects($this->never())->method('createXSLProcessor'); 
    125         $this->xmlProcessor->process(); 
     120        $this->xmlProcessor->selectPage($this->mockPageFactory); 
     121    } 
     122 
     123    /** 
     124     * page to be generated is cachable 
     125     * 
     126     * @test 
     127     */ 
     128    public function isCachable() 
     129    { 
     130        $mockXMLGenerator = $this->getMock('stubXMLGenerator'); 
     131        $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockXMLGenerator)); 
     132        $mockXMLGenerator->expects($this->exactly(3))->method('isCachable')->will($this->returnValue(true)); 
     133        $mockXMLGenerator->expects($this->exactly(3))->method('getCacheVars')->will($this->returnValue(array('foo' => 'bar'))); 
     134        $mockXMLGenerator->expects($this->exactly(3))->method('getUsedFiles')->will($this->returnValue(array('foo.bar'))); 
     135        $this->xmlProcessor->selectPage($this->mockPageFactory); 
     136         
     137        $this->assertEquals('index', $this->xmlProcessor->getPageName()); 
     138        $mockWebsiteCache = $this->getMock('stubWebsiteCache'); 
     139        $mockWebsiteCache->expects($this->once())->method('addCacheVar')->with($this->equalTo('page'), $this->equalTo('index')); 
     140        $mockWebsiteCache->expects($this->exactly(3))->method('addCacheVars')->with($this->equalTo(array('foo' => 'bar'))); 
     141        $mockWebsiteCache->expects($this->exactly(3))->method('addUsedFiles')->with($this->equalTo(array('foo.bar'))); 
     142        $this->assertTrue($this->xmlProcessor->addCacheVars($mockWebsiteCache)); 
     143    } 
     144 
     145    /** 
     146     * page to be generated is not cachable 
     147     * 
     148     * @test 
     149     */ 
     150    public function isNotCachable() 
     151    { 
     152        $mockXMLGenerator = $this->getMock('stubXMLGenerator'); 
     153        $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockXMLGenerator)); 
     154        $mockXMLGenerator->expects($this->once())->method('isCachable')->will($this->returnValue(false)); 
     155        $mockXMLGenerator->expects($this->never())->method('getCacheVars'); 
     156        $mockXMLGenerator->expects($this->never())->method('getUsedFiles'); 
     157        $this->xmlProcessor->selectPage($this->mockPageFactory); 
     158         
     159        $this->assertEquals('index', $this->xmlProcessor->getPageName()); 
     160        $mockWebsiteCache = $this->getMock('stubWebsiteCache'); 
     161        $mockWebsiteCache->expects($this->never())->method('addCacheVar'); 
     162        $mockWebsiteCache->expects($this->never())->method('addCacheVars'); 
     163        $mockWebsiteCache->expects($this->never())->method('addUsedFiles'); 
     164        $this->assertFalse($this->xmlProcessor->addCacheVars($mockWebsiteCache)); 
    126165    } 
    127166 
     
    131170     * @test 
    132171     */ 
    133    public function correctPage() 
    134     { 
     172   public function processSuccessful() 
     173    { 
     174        $mockXMLGenerator = $this->getMock('stubXMLGenerator'); 
     175        $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockXMLGenerator)); 
     176        $mockXMLGenerator->expects($this->exactly(3))->method('generate')->with($this->equalTo($this->mockXMLStreamWriter), $this->equalTo($this->mockXMLSerializer)); 
     177        $this->xmlProcessor->selectPage($this->mockPageFactory); 
     178         
    135179        $this->xmlProcessor->expects($this->once())->method('createXMLStreamWriter')->will($this->returnValue($this->mockXMLStreamWriter)); 
    136180        $this->xmlProcessor->expects($this->once())->method('createXMLSerializer')->will($this->returnValue($this->mockXMLSerializer)); 
     
    138182        $this->mockXMLStreamWriter->expects($this->once())->method('writeAttribute')->with($this->equalTo('page'), $this->equalTo('index')); 
    139183        $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 
    140         $mockXMLGenerator = $this->getMock('stubXMLGenerator'); 
    141         $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockXMLGenerator)); 
    142         $mockXMLGenerator->expects($this->exactly(3))->method('generate')->with($this->equalTo($this->mockXMLStreamWriter), $this->equalTo($this->mockXMLSerializer)); 
    143184        $resultXSL         = new DOMDocument(); 
    144185        $mockSkinGenerator = $this->getMock('stubSkinGenerator');