Changeset 878

Show
Ignore:
Timestamp:
08/22/07 17:57:00 (1 year ago)
Author:
mikey
Message:

made xsl callbacks configurable (requires stubIOCPreInterceptor is configured to be sure that a stubBinder instance can be found within registry) (fixes ticket #62)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/stubbles/build.xml

    r859 r878  
    3939    <mkdir dir="${build.src.dir}/cache/xml/elements" /> 
    4040 
     41    <mkdir dir="${build.src.dir}/config" /> 
     42    <copy file="${project.basedir}/../../config/xsl-callbacks.ini" tofile="${build.src.dir}/config/xsl-callbacks-dist.ini" /> 
     43     
    4144    <mkdir dir="${build.src.dir}/config/errors" /> 
    4245    <copy file="${project.basedir}/../../config/errors/500.html" tofile="${build.src.dir}/config/errors/500-dist.html" /> 
  • trunk/src/main/php/net/stubbles/websites/xml/stubXMLPostInterceptor.php

    r842 r878  
    1313                      'net.stubbles.util.stubRegistry', 
    1414                      'net.stubbles.xml.xsl.stubXSLProcessor', 
    15                       'net.stubbles.xml.xsl.util.stubXSLImageDimensions', 
    1615                      'net.stubbles.xml.stubXMLXIncludeStreamWrapper', 
    1716                      'net.stubbles.xml.stubXMLStreamWriterFactory' 
     
    1918/** 
    2019 * Post interceptor that transforms the xml result document into html. 
     20 *  
     21 * When using this post interceptor please make sure that within the registry 
     22 * a value accessible with key 'net.stubbles.ioc.stubBinder' is configured and 
     23 * an instance of net.stubbles.ioc.stubBinder. If the pre interceptor 
     24 * net.stubbles.ioc.stubIOCPreInterceptor is configured as pre interceptor this 
     25 * one will put a stubBinder instance into the registry. 
     26 * Please make sure as well that a file named xsl-callbacks.ini resides in the 
     27 * base config directory configured in stubConfig::getConfigPath(). 
    2128 * 
    2229 * @package     stubbles 
     
    3946     * does the postprocessing stuff 
    4047     * 
    41      * @param  stubRequest   $request   access to request data 
    42      * @param  stubSession   $session   access to session data 
    43      * @param  stubResponse  $response  access to response data 
     48     * @param   stubRequest    $request   access to request data 
     49     * @param   stubSession    $session   access to session data 
     50     * @param   stubResponse   $response  access to response data 
     51     * @throws  stubException 
    4452     */ 
    4553    public function postProcess(stubRequest $request, stubSession $session, stubResponse $response) 
    4654    { 
    4755        // does not work with another response type 
    48         if (($response instanceof stubXMLResponse) == false) { 
     56        if (($response instanceof stubXMLResponse) === false) { 
    4957            return false; 
    5058        } 
    5159 
    52         // first we create the xsl with the master.xsl 
     60        $binder = stubRegistry::get('net.stubbles.ioc.stubBinder'); 
     61        if (($binder instanceof stubBinder) === false) { 
     62            throw new stubException('No instance of net.stubbles.ioc.stubBinder in registry.'); 
     63        } 
     64         
     65        $binder->bind('stubXMLStreamWriter')->to(stubXMLStreamWriterFactory::getFqClassNameAsAvailable()); 
     66        $binder->bindConstant()->named('imagePath')->to(getcwd()); 
    5367        $xslProcessor = $this->createXSLProcessor(); 
    54         $xslProcessor->registerCallback('image', $this->createImageCallback()); 
     68        $injector     = $binder->getInjector(); 
     69        foreach ($this->getCallbackList() as $callbackName => $callbackClass) { 
     70            $xslProcessor->registerCallback($callbackName, $injector->getInstance($callbackClass)); 
     71        } 
     72         
    5573        $xslProcessor->importXSLStylesheet($this->createXSLStylesheet()); 
    5674        $page = $response->getPage(); 
     
    6583            $language = 'en_EN'; 
    6684        } 
     85         
    6786        $xslProcessor->setParameter('', 'lang', $language); 
    68         // use same xsl processor for the xincludes 
     87        // use same xsl processor for xincludes 
    6988        $this->setXIncludeProcessor($xslProcessor); 
    7089 
     90        // first we create the xsl with applying the master.xsl on the skin and the page 
    7191        $skin = (($page->hasProperty('skin') === true) ? ($page->getProperty('skin')) : ('default')); 
    7292        $xslProcessor->setXMLDocument($this->createXMLSkinDocument($skin)); 
    7393        $resultXSL = $xslProcessor->transformToDoc(); 
    7494        @$resultXSL->xinclude(); 
    75  
     95         
    7696        // now we use the created xsl to transform the xml document created by the application 
    7797        $xslProcessor = $this->createXSLProcessor(); 
     
    106126     * creates a stubXSLImageDimensions 
    107127     * 
    108      * @return  stubXSLImageDimensions 
     128     * @return  array 
     129     * @throws  stubException 
    109130     */ 
    110     protected function createImageCallback() 
     131    protected function getCallbackList() 
    111132    { 
    112         $image = new stubXSLImageDimensions(stubXMLStreamWriterFactory::createAsAvailable()); 
    113         $image->setPath(getcwd()); 
    114         return $image; 
     133        $iniFile = stubConfig::getConfigPath() . '/xsl-callbacks.ini'; 
     134        if (file_exists($iniFile) === false) { 
     135            throw new stubException('Configuration file ' . $iniFile . ' for XSL callback configuration is missing.'); 
     136        } 
     137         
     138        return parse_ini_file($iniFile); 
    115139    } 
    116140 
  • trunk/src/test/php/net/stubbles/websites/xml/stubXMLPostInterceptorTestCase.php

    r840 r878  
    1212Mock::generate('stubSession'); 
    1313Mock::generate('stubXSLProcessor'); 
    14 Mock::generate('stubObject'); 
    15 Mock::generatePartial('stubXMLPostInterceptor', 'stubTestXMLPostInterceptor', array('setXIncludeProcessor', 'createXSLProcessor', 'createImageCallback', 'createXSLStylesheet', 'createXMLSkinDocument')); 
     14Mock::generatePartial('stubXMLPostInterceptor', 'stubTestXMLPostInterceptor', array('setXIncludeProcessor', 'createXSLProcessor', 'getCallbackList', 'createXSLStylesheet', 'createXMLSkinDocument')); 
    1615class TestDOMDocument extends DOMDocument 
    1716{ 
     
    7473     */ 
    7574    protected $mockImageCallback; 
    76      
     75 
    7776    /** 
    7877     * set up test environment 
     
    9493        $this->mockXSLProcessor->setReturnValue('transformToXML', '<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>'); 
    9594        $this->xmlPostInterceptor->setReturnValue('createXSLProcessor', $this->mockXSLProcessor); 
    96         $this->mockImageCallback  = new MockstubObject(); 
    97         $this->xmlPostInterceptor->setReturnValue('createImageCallback', $this->mockImageCallback); 
     95        $this->xmlPostInterceptor->setReturnValue('getCallbackList', array()); 
    9896        if (stubRegistry::hasConfig('net.stubbles.language') == true) { 
    9997            stubRegistry::setConfig('net.stubbles.language', null); 
    10098        } 
     99         
     100        stubRegistry::set('net.stubbles.ioc.stubBinder', new stubBinder()); 
    101101    } 
    102      
     102 
     103    /** 
     104     * clear test environment 
     105     */ 
     106    public function tearDown() 
     107    { 
     108        stubRegistry::remove('net.stubbles.ioc.stubBinder'); 
     109    } 
     110 
    103111    /** 
    104112     * assure that a wrong response does not trigger any action 
     
    108116        $this->assertFalse($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->mockResponse)); 
    109117    } 
    110      
     118 
    111119    /** 
    112120     * assure that a correct response works as expected 
     
    121129        $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 
    122130    } 
    123      
     131 
    124132    /** 
    125133     * assure that another skin will be used 
     
    131139        $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 
    132140    } 
    133      
     141 
    134142    /** 
    135143     * assure that the language value from the registry is taken 
     
    141149        $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 
    142150    } 
    143      
     151 
    144152    /** 
    145153     * assure that the language value from the page is taken 
     
    152160        $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 
    153161    } 
    154      
     162 
    155163    /** 
    156164     * assure that the language value from the session is taken 
     
    165173        $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 
    166174    } 
     175 
     176    /** 
     177     * assure that missing binder triggers an exception 
     178     */ 
     179    public function testWithoutBinderInRegistry() 
     180    { 
     181        stubRegistry::remove('net.stubbles.ioc.stubBinder'); 
     182        $this->expectException('stubException'); 
     183        $this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse); 
     184    } 
    167185} 
    168186?>