Changeset 1467
- Timestamp:
- 03/27/08 14:35:41 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/xml/stubXMLProcessor.php
r1442 r1467 11 11 'net::stubbles::lang::exceptions::stubRuntimeException', 12 12 'net::stubbles::util::stubRegistry', 13 'net::stubbles::websites::cache::stubCachableProcessor', 13 14 'net::stubbles::websites::processors::stubAbstractProcessor', 14 15 'net::stubbles::websites::processors::stubPageBasedProcessor', … … 20 21 * Default processor delivered by stubbles. 21 22 * 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 * 22 33 * @package stubbles 23 34 * @subpackage websites_xml 24 35 */ 25 class stubXMLProcessor extends stubAbstractProcessor implements stubPageBasedProcessor 36 class stubXMLProcessor extends stubAbstractProcessor implements stubPageBasedProcessor, stubCachableProcessor 26 37 { 27 38 /** 28 39 * list of xml generators to be used to create the dom tree 29 40 * 30 * @var array<st ring>41 * @var array<stubXMLGenerator> 31 42 */ 32 43 protected $xmlGenerators = array(); … … 43 54 */ 44 55 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; 69 62 70 63 /** … … 79 72 $this->page = $pageFactory->getPage($this->pageName); 80 73 $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() 87 83 { 88 84 $binder = stubRegistry::get(stubBinder::REGISTRY_KEY); 89 85 if (($binder instanceof stubBinder) === false) { 90 throw new stubRuntimeException('XML/XSL view engine requires I OC.');86 throw new stubRuntimeException('XML/XSL view engine requires IoC.'); 91 87 } 92 88 93 89 $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 { 95 149 $xmlStreamWriter = $this->createXMLStreamWriter(); 96 150 $xmlStreamWriter->writeStartElement('document'); 97 151 $xmlStreamWriter->writeAttribute('page', $this->pageName); 98 152 $xmlSerializer = $this->createXMLSerializer(); 99 foreach ($this->xmlGenerators as $xmlGeneratorClassName) { 100 $xmlGenerator = $injector->getInstance($xmlGeneratorClassName); 153 foreach ($this->xmlGenerators as $xmlGenerator) { 101 154 $xmlGenerator->generate($xmlStreamWriter, $xmlSerializer); 102 155 } trunk/src/test/php/net/stubbles/integration/ProcessorTestCase.php
r1445 r1467 16 16 class ProcessorTestCase extends PHPUnit_Framework_TestCase 17 17 { 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 18 34 /** 19 35 * helper method trunk/src/test/php/net/stubbles/websites/xml/stubXMLProcessorTestCase.php
r1442 r1467 99 99 $this->mockPageFactory->expects($this->once())->method('getPage')->will($this->returnValue($this->page)); 100 100 $this->mockSession->expects($this->at(0))->method('putValue')->with($this->equalTo('net.stubbles.websites.lastPage'), $this->equalTo('index')); 101 $this->xmlProcessor->selectPage($this->mockPageFactory);102 101 } 103 102 … … 116 115 * @expectedException stubRuntimeException 117 116 */ 118 public function processWithoutBinderInRegistryThrowsRuntimeException()117 public function selectPageWithoutBinderInRegistryThrowsRuntimeException() 119 118 { 120 119 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)); 126 165 } 127 166 … … 131 170 * @test 132 171 */ 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 135 179 $this->xmlProcessor->expects($this->once())->method('createXMLStreamWriter')->will($this->returnValue($this->mockXMLStreamWriter)); 136 180 $this->xmlProcessor->expects($this->once())->method('createXMLSerializer')->will($this->returnValue($this->mockXMLSerializer)); … … 138 182 $this->mockXMLStreamWriter->expects($this->once())->method('writeAttribute')->with($this->equalTo('page'), $this->equalTo('index')); 139 183 $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));143 184 $resultXSL = new DOMDocument(); 144 185 $mockSkinGenerator = $this->getMock('stubSkinGenerator');
