Web components
File detail
Source code
<?php
/**
* @file PageTransform.class.php
* Definition of PageTransform class.
* @author Kamil Dudka <xdudka00@gmail.com>
*/
class PageTransform {
public static function buildIfNeeded($target, $xslt, $xml = 'index.xml') {
$obj = new self($target, $xslt, $xml);
if ($obj->needUpdate())
$obj->buildTarget();
}
// Avoid class instantiation
private function __construct($target, $xslt, $xml) {
$config = Config::instance();
$this->pageDir_ = $config->documentRoot().$config->pageDir();
$this->target_ = $target;
$this->xslt_ = $xslt;
$this->xml_ = $xml;
}
private $pageDir_;
private $target_;
private $xslt_;
private $xml_;
private function needUpdate() {
if (!file_exists($this->target_))
return true;
$ts = filemtime($this->target_);
if (filemtime($this->pageDir_)>$ts)
return true;
$fileList = scandir($this->pageDir_);
array_shift($fileList);
array_shift($fileList);
foreach($fileList as $current)
if (filemtime($this->pageDir_.'/'.$current)>$ts)
return true;
return false;
}
private function buildTarget() {
// Load the XML source
$xml = new DOMDocument;
$xml->load($this->pageDir_.'/'.$this->xml_);
$xsl = new DOMDocument;
$xsl->load($this->pageDir_.'/'.$this->xslt_);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
// Process XSLT and write to $this->target_
$fd = fopen($this->target_, 'w');
fwrite($fd, $proc->transformToXML($xml));
fclose($fd);
}
};
?>