1 | <?php |
||
9 | class Builder { |
||
10 | private $template; |
||
11 | private $tss; |
||
12 | private $cache; |
||
13 | private $time; |
||
14 | private $modules = []; |
||
15 | private $config; |
||
16 | private $filePath; |
||
17 | private $cacheKey; |
||
18 | private $defaultModules = [ |
||
19 | '\\Transphporm\\Module\\Basics', |
||
20 | '\\Transphporm\\Module\\Pseudo', |
||
21 | '\\Transphporm\\Module\\Format', |
||
22 | '\\Transphporm\\Module\\Functions' |
||
23 | ]; |
||
24 | |||
25 | public function __construct($template, $tss = '', $modules = null) { |
||
33 | |||
34 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
||
35 | //Would be better if PHP allowed setting the script clock, but this is the simplest way of overriding it |
||
36 | public function setTime($time) { |
||
39 | |||
40 | public function loadModule(Module $module) { |
||
43 | |||
44 | public function setLocale($locale) { |
||
45 | $format = new \Transphporm\Module\Format($locale); |
||
46 | $this->modules[get_class($format)] = $format; |
||
47 | } |
||
48 | |||
49 | public function addPath($dir) { |
||
52 | |||
53 | private function getSheetLoader() { |
||
57 | |||
58 | public function output($data = null, $document = false) { |
||
59 | $headers = []; |
||
60 | |||
61 | $tssCache = $this->getSheetLoader(); |
||
62 | $this->cacheKey = $tssCache->getCacheKey($data); |
||
63 | $result = $this->loadTemplate(); |
||
64 | //If an update is required, run any rules that need to be run. Otherwise, return the result from cache |
||
65 | //without creating any further objects, loading a DomDocument, etc |
||
66 | if (empty($result['renderTime']) || $tssCache->updateRequired($data) === true) { |
||
67 | $template = $this->createAndProcessTemplate($data, $result['cache'], $headers); |
||
68 | $tssCache->processRules($template, $this->config); |
||
69 | |||
70 | $result = ['cache' => $template->output($document), |
||
71 | 'renderTime' => time(), |
||
72 | 'headers' => array_merge($result['headers'], $headers), |
||
73 | 'body' => $this->doPostProcessing($template)->output($document) |
||
74 | ]; |
||
75 | $this->cache->write($tssCache->getCacheKey($data) . $this->template, $result); |
||
76 | } |
||
77 | unset($result['cache'], $result['renderTime']); |
||
78 | return (object) $result; |
||
79 | } |
||
80 | |||
81 | private function createAndProcessTemplate($data, $body, &$headers) { |
||
93 | |||
94 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
||
95 | private function doPostProcessing($template) { |
||
99 | |||
100 | |||
101 | //Load a template, firstly check if it's a file or a valid string |
||
102 | private function loadTemplate() { |
||
103 | $result = ['cache' => $this->template, 'headers' => []]; |
||
104 | if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
||
105 | return $result; |
||
106 | } |
||
107 | |||
108 | private function loadTemplateFromFile($file) { |
||
109 | $xml = $this->cache->load($this->cacheKey . $file, filemtime($file)); |
||
110 | return $xml ? $xml : ['cache' => file_get_contents($file) ?: "", 'headers' => []]; |
||
111 | } |
||
112 | |||
113 | public function setCache(\ArrayAccess $cache) { |
||
116 | |||
117 | private function isValidDoc($xml) { |
||
120 | |||
121 | public function __destruct() { |
||
122 | //Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks |
||
123 | if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); |
||
124 | } |
||
125 | } |
||
126 |