| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
stubClassLoader::load('net::stubbles::ioc::stubBinder', |
|---|
| 11 |
'net::stubbles::lang::stubRegistry', |
|---|
| 12 |
'net::stubbles::lang::exceptions::stubRuntimeException', |
|---|
| 13 |
'net::stubbles::websites::cache::stubCachableProcessor', |
|---|
| 14 |
'net::stubbles::websites::processors::stubAbstractProcessor', |
|---|
| 15 |
'net::stubbles::websites::processors::stubPageBasedProcessor', |
|---|
| 16 |
'net::stubbles::websites::xml::skin::stubSkinGeneratorFactory', |
|---|
| 17 |
'net::stubbles::xml::stubXMLStreamWriterFactory', |
|---|
| 18 |
'net::stubbles::xml::serializer::stubXMLSerializer' |
|---|
| 19 |
); |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
class stubXMLProcessor extends stubAbstractProcessor implements stubPageBasedProcessor, stubCachableProcessor |
|---|
| 37 |
{ |
|---|
| 38 |
|
|---|
| 39 |
* registry key for switch whether to serialize the current mode or not |
|---|
| 40 |
*/ |
|---|
| 41 |
const SERIALIZE_MODE_REGISTRY_KEY = 'net.stubbles.websites.xml.serializeMode'; |
|---|
| 42 |
|
|---|
| 43 |
* list of xml generators to be used to create the dom tree |
|---|
| 44 |
* |
|---|
| 45 |
* @var array<stubXMLGenerator> |
|---|
| 46 |
*/ |
|---|
| 47 |
protected $xmlGenerators = array(); |
|---|
| 48 |
|
|---|
| 49 |
* page to display |
|---|
| 50 |
* |
|---|
| 51 |
* @var stubPage |
|---|
| 52 |
*/ |
|---|
| 53 |
protected $page; |
|---|
| 54 |
|
|---|
| 55 |
* name of page to display |
|---|
| 56 |
* |
|---|
| 57 |
* @var string |
|---|
| 58 |
*/ |
|---|
| 59 |
protected $pageName; |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* selects the page to display with help of the page factory |
|---|
| 63 |
* |
|---|
| 64 |
* @param stubPageFactory $pageFactory |
|---|
| 65 |
*/ |
|---|
| 66 |
public function selectPage(stubPageFactory $pageFactory) |
|---|
| 67 |
{ |
|---|
| 68 |
$pageFactory->setPagePrefix('conf/'); |
|---|
| 69 |
$this->pageName = $pageFactory->getPageName($this->request); |
|---|
| 70 |
$this->page = $pageFactory->getPage($this->pageName); |
|---|
| 71 |
$this->session->putValue('net.stubbles.websites.lastPage', $this->pageName); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
* helper method to initialize the xml generator instances |
|---|
| 76 |
* |
|---|
| 77 |
* @throws stubRuntimeException |
|---|
| 78 |
*/ |
|---|
| 79 |
protected function initializeGenerators() |
|---|
| 80 |
{ |
|---|
| 81 |
if (count($this->xmlGenerators) > 0) { |
|---|
| 82 |
return; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
$binder = stubRegistry::get(stubBinder::REGISTRY_KEY); |
|---|
| 86 |
if (($binder instanceof stubBinder) === false) { |
|---|
| 87 |
throw new stubRuntimeException('XML/XSL view engine requires IoC.'); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
$binder->bind('stubPage')->toInstance($this->page); |
|---|
| 91 |
$injector = $binder->getInjector(); |
|---|
| 92 |
foreach ($this->getXMLGenerators() as $xmlGeneratorClassName) { |
|---|
| 93 |
$this->xmlGenerators[] = $injector->getInstance($xmlGeneratorClassName); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
* configure the xml generators |
|---|
| 99 |
* |
|---|
| 100 |
* @return array<string> |
|---|
| 101 |
*/ |
|---|
| 102 |
protected function getXMLGenerators() |
|---|
| 103 |
{ |
|---|
| 104 |
$generators = array('net::stubbles::websites::xml::generator::stubSessionXMLGenerator', |
|---|
| 105 |
'net::stubbles::websites::xml::generator::stubPageXMLGenerator', |
|---|
| 106 |
'net::stubbles::websites::xml::generator::stubRequestXMLGenerator' |
|---|
| 107 |
); |
|---|
| 108 |
if (stubRegistry::getConfig(self::SERIALIZE_MODE_REGISTRY_KEY, false) !== false) { |
|---|
| 109 |
$generators[] = 'net::stubbles::websites::xml::generator::stubModeXMLGenerator'; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
return $generators; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
* adds the cache variables for the current request and returns whether |
|---|
| 117 |
* response is cachable or not |
|---|
| 118 |
* |
|---|
| 119 |
* @param stubWebsiteCache $cache |
|---|
| 120 |
* @return bool |
|---|
| 121 |
*/ |
|---|
| 122 |
public function addCacheVars(stubWebsiteCache $cache) |
|---|
| 123 |
{ |
|---|
| 124 |
$this->initializeGenerators(); |
|---|
| 125 |
foreach ($this->xmlGenerators as $xmlGenerator) { |
|---|
| 126 |
if ($xmlGenerator->isCachable() === false) { |
|---|
| 127 |
return false; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
$cache->addCacheVars($xmlGenerator->getCacheVars()); |
|---|
| 131 |
$cache->addUsedFiles($xmlGenerator->getUsedFiles()); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
$cache->addCacheVar('page', $this->pageName); |
|---|
| 135 |
return true; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
* returns the name of the current page |
|---|
| 140 |
* |
|---|
| 141 |
* Non-page-based processors should return another unique identifier for |
|---|
| 142 |
* the current request if they want to implement this interface. |
|---|
| 143 |
* |
|---|
| 144 |
* @return string |
|---|
| 145 |
*/ |
|---|
| 146 |
public function getPageName() |
|---|
| 147 |
{ |
|---|
| 148 |
return $this->pageName; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
* processes the request |
|---|
| 153 |
*/ |
|---|
| 154 |
public function process() |
|---|
| 155 |
{ |
|---|
| 156 |
$this->initializeGenerators(); |
|---|
| 157 |
$xmlStreamWriter = $this->createXMLStreamWriter(); |
|---|
| 158 |
$xmlStreamWriter->writeStartElement('document'); |
|---|
| 159 |
$xmlStreamWriter->writeAttribute('page', $this->pageName); |
|---|
| 160 |
$xmlSerializer = $this->createXMLSerializer(); |
|---|
| 161 |
foreach ($this->xmlGenerators as $xmlGenerator) { |
|---|
| 162 |
$xmlGenerator->generate($xmlStreamWriter, $xmlSerializer); |
|---|
| 163 |
if ($this->request->isCancelled() === true) { |
|---|
| 164 |
return; |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
$xmlStreamWriter->writeEndElement(); |
|---|
| 169 |
$xslProcessor = $this->createXSLProcessor(); |
|---|
| 170 |
$xslProcessor->importXSLStylesheet($this->createSkinGenerator()->generate($this->session, $this->page)); |
|---|
| 171 |
$xslProcessor->setXMLDocument($xmlStreamWriter->asDOM()); |
|---|
| 172 |
$this->session->putValue('net.stubbles.websites.lastRequestResponseData', $xmlStreamWriter->asXML()); |
|---|
| 173 |
$this->response->replaceData(str_replace(' xmlns=""', '', preg_replace('/ xml:base="(.*)"/U', '', $xslProcessor->transformToXML()))); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
* returns a xml stream writer |
|---|
| 178 |
* |
|---|
| 179 |
* @return stubXMLStreamWriter |
|---|
| 180 |
*/ |
|---|
| 181 |
// @codeCoverageIgnoreStart |
|---|
| 182 |
protected function createXMLStreamWriter() |
|---|
| 183 |
{ |
|---|
| 184 |
return stubXMLStreamWriterFactory::createAsAvailable(); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
/** |
|---|
| 189 |
* returns the xml serializer |
|---|
| 190 |
* |
|---|
| 191 |
* @return stubXMLSerializer |
|---|
| 192 |
*/ |
|---|
| 193 |
// @codeCoverageIgnoreStart |
|---|
| 194 |
protected function createXMLSerializer() |
|---|
| 195 |
{ |
|---|
| 196 |
return new stubXMLSerializer(); |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
/** |
|---|
| 201 |
* creates a new skin generator instance |
|---|
| 202 |
* |
|---|
| 203 |
* @return stubSkinGenerator |
|---|
| 204 |
*/ |
|---|
| 205 |
// @codeCoverageIgnoreStart |
|---|
| 206 |
protected function createSkinGenerator() |
|---|
| 207 |
{ |
|---|
| 208 |
$factory = new stubSkinGeneratorFactory(); |
|---|
| 209 |
return $factory->create(); |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
/** |
|---|
| 214 |
* creates a stubXSLProcessor instance |
|---|
| 215 |
* |
|---|
| 216 |
* @return stubXSLProcessor |
|---|
| 217 |
*/ |
|---|
| 218 |
// @codeCoverageIgnoreStart |
|---|
| 219 |
protected function createXSLProcessor() |
|---|
| 220 |
{ |
|---|
| 221 |
return new stubXSLProcessor(); |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
} |
|---|
| 225 |
?> |
|---|