Changeset 1429

Show
Ignore:
Timestamp:
03/17/08 15:46:51 (2 months ago)
Author:
mikey
Message:

enhancement #136, part 1: make stubSkinGenerator an interface, move implementation to stubDefaultSkinGenerator

Files:

Legend:

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

    r1425 r1429  
    11<?php 
    22/** 
    3  * Class to generate the skin to be applied onto the XML result document. 
     3 * Interface to generate the skin to be applied onto the XML result document. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    77 * @subpackage  websites_xml_skin 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ioc::stubBinder', 
    10                       'net::stubbles::ipo::session::stubSession', 
    11                       'net::stubbles::lang::exceptions::stubRuntimeException', 
    12                       'net::stubbles::util::stubFactory', 
    13                       'net::stubbles::util::stubRegistry', 
    14                       'net::stubbles::websites::stubPage', 
    15                       'net::stubbles::xml::stubXMLXIncludeStreamWrapper', 
    16                       'net::stubbles::xml::stubXMLStreamWriterFactory', 
    17                       'net::stubbles::xml::xsl::stubXSLProcessor' 
     9stubClassLoader::load('net::stubbles::ipo::session::stubSession', 
     10                      'net::stubbles::websites::stubPage' 
    1811); 
    1912/** 
    20  * Class to generate the skin to be applied onto the XML result document. 
    21  * 
    22  * Please make sure as well that a file named xsl-callbacks.ini resides in the 
    23  * base config directory configured in stubConfig::getConfigPath(). 
     13 * Interface to generate the skin to be applied onto the XML result document. 
    2414 * 
    2515 * @package     stubbles 
    2616 * @subpackage  websites_xml_skin 
    2717 */ 
    28 class stubSkinGenerator extends stubBaseObject 
     18interface stubSkinGenerator extends stubObject 
    2919{ 
    3020    /** 
    31      * callback config file name 
     21     * returns the key for the skin to be generated 
    3222     * 
    33      * @var  string 
     23     * @param   stubSession  $session 
     24     * @param   stubPage     $page 
     25     * @return  string 
    3426     */ 
    35     protected $callbackConfigFile; 
    36  
    37     /** 
    38      * constructor 
    39      */ 
    40     public function __construct() 
    41     { 
    42         stubXMLXIncludeStreamWrapper::register(); 
    43         stubXMLXIncludeStreamWrapper::setIncludePath(stubConfig::getPagePath() . '/txt'); 
    44         stubXMLXIncludeStreamWrapper::setCachePath(stubConfig::getCachePath()); 
    45         $this->callbackConfigFile = stubConfig::getConfigPath() . '/xsl-callbacks.ini'; 
    46     } 
    47  
    48     /** 
    49      * sets the callback config file 
    50      * 
    51      * @param  string  $callbackConfigFile 
    52      */ 
    53     public function setCallbackConfigFile($callbackConfigFile) 
    54     { 
    55         $this->callbackConfigFile = $callbackConfigFile; 
    56     } 
     27    public function getSkinKey(stubSession $session, stubPage $page); 
    5728 
    5829    /** 
     
    6233     * @param   stubPage     $page 
    6334     * @return  DOMDocument 
    64      * @throws  stubRuntimeException 
    6535     */ 
    66     public function generate(stubSession $session, stubPage $page) 
    67     { 
    68         $binder = stubRegistry::get(stubBinder::REGISTRY_KEY); 
    69         if (($binder instanceof stubBinder) === false) { 
    70             throw new stubRuntimeException('No instance of net::stubbles::ioc::stubBinder in registry.'); 
    71         } 
    72          
    73         $binder->bind('stubXMLStreamWriter')->to(stubXMLStreamWriterFactory::getFqClassNameAsAvailable()); 
    74         $binder->bindConstant()->named('imagePath')->to(getcwd()); 
    75         $xslProcessor = $this->createXSLProcessor(); 
    76         $injector     = $binder->getInjector(); 
    77         foreach ($this->getCallbackList() as $callbackName => $callbackClass) { 
    78             $xslProcessor->registerCallback($callbackName, $injector->getInstance($callbackClass)); 
    79         } 
    80          
    81         $xslProcessor->importXSLStylesheet($this->createXSLStylesheet()); 
    82         $xslProcessor->setParameter('', 'page', $page->getProperty('name')); 
    83         if ($session->hasValue('net.stubbles.language') == true) { 
    84             $language = $session->getValue('net.stubbles.language'); 
    85         } elseif ($page->hasProperty('language') == true) { 
    86             $language = $page->getProperty('language'); 
    87         } elseif (stubRegistry::hasConfig('net.stubbles.language') == true) { 
    88             $language = stubRegistry::getConfig('net.stubbles.language'); 
    89         } else { 
    90             $language = 'en_EN'; 
    91         } 
    92          
    93         $xslProcessor->setParameter('', 'lang', $language); 
    94         // use same xsl processor for xincludes 
    95         stubXMLXIncludeStreamWrapper::setXSLProcessor($xslProcessor); 
    96  
    97         // first we create the xsl with applying the master.xsl on the skin and the page 
    98         $skin = (($page->hasProperty('skin') === true) ? ($page->getProperty('skin')) : ('default')); 
    99         $xslProcessor->setXMLDocument($this->createXMLSkinDocument($skin)); 
    100         $resultXSL = $xslProcessor->transformToDoc(); 
    101         @$resultXSL->xinclude(); 
    102         return $resultXSL; 
    103     } 
    104  
    105     /** 
    106      * creates a stubXSLProcessor instance 
    107      * 
    108      * @return  stubXSLProcessor 
    109      */ 
    110     // @codeCoverageIgnoreStart 
    111     protected function createXSLProcessor() 
    112     { 
    113         return new stubXSLProcessor(); 
    114     } 
    115     // @codeCoverageIgnoreEnd 
    116  
    117     /** 
    118      * creates the list of callbacks 
    119      * 
    120      * @return  array 
    121      * @throws  stubRuntimeException 
    122      */ 
    123     protected function getCallbackList() 
    124     { 
    125         if (file_exists($this->callbackConfigFile) === false) { 
    126             throw new stubRuntimeException('Configuration file ' . $this->callbackConfigFile . ' for XSL callback configuration is missing.'); 
    127         } 
    128          
    129         return parse_ini_file($this->callbackConfigFile); 
    130     } 
    131  
    132     /** 
    133      * creates the xsl stylesheet 
    134      * 
    135      * @return  DOMDocument 
    136      * @todo    fix selection of uri 
    137      */ 
    138     // @codeCoverageIgnoreStart 
    139     protected function createXSLStylesheet() 
    140     { 
    141         $uris = stubFactory::getResourceURIs('xsl/master.xsl'); 
    142         return DOMDocument::load($uris[0]); 
    143     } 
    144     // @codeCoverageIgnoreEnd 
    145  
    146     /** 
    147      * creates the skin document 
    148      * 
    149      * @param   string       $skin 
    150      * @return  DOMDocument 
    151      */ 
    152     // @codeCoverageIgnoreStart 
    153     protected function createXMLSkinDocument($skin) 
    154     { 
    155         return DOMDocument::load(stubConfig::getPagePath() . '/skin/' . $skin . '.xml'); 
    156     } 
    157     // @codeCoverageIgnoreEnd 
     36    public function generate(stubSession $session, stubPage $page); 
    15837} 
    15938?> 
  • trunk/src/main/php/net/stubbles/websites/xml/stubXMLProcessor.php

    r1426 r1429  
    1212                      'net::stubbles::util::stubRegistry', 
    1313                      'net::stubbles::websites::processors::stubAbstractPageProcessor', 
    14                       'net::stubbles::websites::xml::skin::stubSkinGenerator', 
     14                      'net::stubbles::websites::xml::skin::stubDefaultSkinGenerator', 
    1515                      'net::stubbles::xml::stubXMLStreamWriterFactory', 
    1616                      'net::stubbles::xml::serializer::stubXMLSerializer' 
     
    122122    protected function createSkinGenerator() 
    123123    { 
    124         return new stubSkinGenerator(); 
     124        return new stubDefaultSkinGenerator(); 
    125125    } 
    126126    // @codeCoverageIgnoreEnd 
  • trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php

    r1426 r1429  
    5252 
    5353        // xml skin 
    54         $suite->addTestFile($dir . '/xml/skin/stubSkinGeneratorTestCase.php'); 
     54        $suite->addTestFile($dir . '/xml/skin/stubDefaultSkinGeneratorTestCase.php'); 
    5555 
    5656        // xml generator 
  • trunk/src/test/php/net/stubbles/websites/xml/skin/stubDefaultSkinGeneratorTestCase.php

    r1425 r1429  
    11<?php 
    22/** 
    3  * Tests for net::stubbles::websites::xml::skin::stubSkinGenerator. 
     3 * Tests for net::stubbles::websites::xml::skin::stubDefaultSkinGenerator. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    77 * @subpackage  websites_xml_skin_test 
    88 */ 
    9 stubClassLoader::load('net::stubbles::websites::xml::skin::stubSkinGenerator'); 
     9stubClassLoader::load('net::stubbles::websites::xml::skin::stubDefaultSkinGenerator'); 
    1010/** 
    1111 * Helper class to access a protected method. 
     
    1414 * @subpackage  websites_xml_skin_test 
    1515 */ 
    16 class TeststubSkinGenerator extends stubSkinGenerator 
     16class TeststubDefaultSkinGenerator extends stubDefaultSkinGenerator 
    1717{ 
    1818    /** 
     
    2727} 
    2828/** 
    29  * Tests for net::stubbles::websites::xml::skin::stubSkinGenerator. 
     29 * Tests for net::stubbles::websites::xml::skin::stubDefaultSkinGenerator. 
    3030 * 
    3131 * @package     stubbles 
    3232 * @subpackage  websites_xml_skin_test 
    3333 */ 
    34 class stubSkinGeneratorTestCase extends PHPUnit_Framework_TestCase 
     34class stubDefaultSkinGeneratorTestCase extends PHPUnit_Framework_TestCase 
    3535{ 
    3636    /** 
    3737     * instance to test 
    3838     * 
    39      * @var  stubSkinGenerator 
     39     * @var  stubDefaultSkinGenerator 
    4040     */ 
    4141    protected $skinGenerator; 
     
    8282    public function setUp() 
    8383    { 
    84         $this->skinGenerator = $this->getMock('stubSkinGenerator', 
     84        $this->skinGenerator = $this->getMock('stubDefaultSkinGenerator', 
    8585                                              array('createXSLProcessor', 
    8686                                                    'getCallbackList', 
     
    141141    { 
    142142        $this->page->setProperty('skin', 'another'); 
     143        $this->page->setProperty('name', 'foo'); 
    143144        $this->skinGenerator->expects($this->any())->method('getCallbackList')->will($this->returnValue(array())); 
    144145        $this->skinGenerator->expects($this->once()) 
     
    147148                            ->will($this->returnValue($this->getMock('DOMDocument'))); 
    148149        $this->assertSame($this->resultDomDocument, $this->skinGenerator->generate($this->mockSession, $this->page)); 
     150        $this->assertEquals(md5('anotherfooen_EN'), $this->skinGenerator->getSkinKey($this->mockSession, $this->page)); 
    149151    } 
    150152 
     
    157159    { 
    158160        stubRegistry::setConfig('net.stubbles.language', 'foo'); 
     161        $this->page->setProperty('name', 'bar'); 
    159162        $this->skinGenerator->expects($this->any())->method('getCallbackList')->will($this->returnValue(array())); 
    160163        $this->skinGenerator->expects($this->any()) 
     
    163166        $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('foo')); 
    164167        $this->assertSame($this->resultDomDocument, $this->skinGenerator->generate($this->mockSession, $this->page)); 
     168        $this->assertEquals(md5('defaultbarfoo'), $this->skinGenerator->getSkinKey($this->mockSession, $this->page)); 
    165169    } 
    166170 
     
    173177    { 
    174178        $this->page->setProperty('language', 'bar'); 
     179        $this->page->setProperty('name', 'baz'); 
    175180        stubRegistry::setConfig('net.stubbles.language', 'foo'); 
    176181        $this->skinGenerator->expects($this->any())->method('getCallbackList')->will($this->returnValue(array())); 
     
    180185        $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('bar')); 
    181186        $this->assertSame($this->resultDomDocument, $this->skinGenerator->generate($this->mockSession, $this->page)); 
     187        $this->assertEquals(md5('defaultbazbar'), $this->skinGenerator->getSkinKey($this->mockSession, $this->page)); 
    182188    } 
    183189 
     
    193199                            ->method('createXMLSkinDocument') 
    194200                            ->will($this->returnValue($this->getMock('DOMDocument'))); 
    195         $this->mockSession->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 
    196         $this->mockSession->expects($this->once())->method('getValue')->will($this->returnValue('baz')); 
     201        $this->mockSession->expects($this->exactly(2))->method('hasValue')->will($this->returnValue(true)); 
     202        $this->mockSession->expects($this->exactly(2))->method('getValue')->will($this->returnValue('baz')); 
    197203        $this->page->setProperty('language', 'bar'); 
     204        $this->page->setProperty('name', 'baz'); 
    198205        stubRegistry::setConfig('net.stubbles.language', 'foo'); 
    199206        $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('baz')); 
    200207        $this->assertSame($this->resultDomDocument, $this->skinGenerator->generate($this->mockSession, $this->page)); 
     208        $this->assertEquals(md5('defaultbazbaz'), $this->skinGenerator->getSkinKey($this->mockSession, $this->page)); 
    201209    } 
    202210 
     
    220228    public function callbacks() 
    221229    { 
    222         $skinGenerator = new TeststubSkinGenerator(); 
     230        $skinGenerator = new TeststubDefaultSkinGenerator(); 
    223231        $this->assertEquals(array('image' => 'net::stubbles::xml::xsl::util::stubXSLImageDimensions'), $skinGenerator->callGetCallbackList()); 
    224232    } 
     
    232240    public function callbacksConfigurationFileMissing() 
    233241    { 
    234         $skinGenerator = new TeststubSkinGenerator(); 
     242        $skinGenerator = new TeststubDefaultSkinGenerator(); 
    235243        $skinGenerator->setCallbackConfigFile(dirname(__FILE__) . '/doesNotExist.ini'); 
    236244        $skinGenerator->callGetCallbackList();