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 $defaultModules = [ |
||
18 | '\\Transphporm\\Module\\Basics', |
||
19 | '\\Transphporm\\Module\\Pseudo', |
||
20 | '\\Transphporm\\Module\\Format', |
||
21 | '\\Transphporm\\Module\\Functions' |
||
22 | ]; |
||
23 | |||
24 | public function __construct($template, $tss = '', $modules = null) { |
||
25 | $this->template = $template; |
||
26 | $this->tss = $tss; |
||
27 | $this->cache = new Cache(new \ArrayObject()); |
||
28 | $this->filePath = new FilePath(); |
||
29 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
||
30 | foreach ($modules as $module) $this->loadModule(new $module); |
||
31 | } |
||
32 | |||
33 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
||
34 | //Would be better if PHP allowed setting the script clock, but this is the simplest way of overriding it |
||
35 | public function setTime($time) { |
||
38 | |||
39 | public function loadModule(Module $module) { |
||
42 | |||
43 | public function addPath($dir) { |
||
44 | $this->filePath->addPath($dir); |
||
45 | } |
||
46 | |||
47 | public function output($data = null, $document = false) { |
||
48 | $headers = []; |
||
49 | |||
50 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
||
51 | $functionSet = new FunctionSet($elementData); |
||
52 | |||
53 | $cachedOutput = $this->loadTemplate(); |
||
54 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
||
55 | $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
||
56 | $valueParser = new Parser\Value($functionSet); |
||
57 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
||
58 | |||
59 | foreach ($this->modules as $module) $module->load($this->config); |
||
60 | |||
61 | $this->processRules($template, $this->config); |
||
62 | |||
63 | $result = ['body' => $template->output($document), 'headers' => array_merge($cachedOutput['headers'], $headers)]; |
||
64 | $this->cache->write($this->template, $result); |
||
65 | $result['body'] = $this->doPostProcessing($template)->output($document); |
||
66 | return (object) $result; |
||
67 | } |
||
68 | |||
69 | private function processRules($template, $config) { |
||
76 | |||
77 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
||
78 | private function doPostProcessing($template) { |
||
82 | |||
83 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
||
84 | private function executeTssRule($rule, $template, $config) { |
||
92 | |||
93 | //Load a template, firstly check if it's a file or a valid string |
||
94 | private function loadTemplate() { |
||
95 | if (trim($this->template)[0] !== '<' && file_exists($this->template)) { |
||
96 | $xml = $this->cache->load($this->template, filemtime($this->template)); |
||
97 | return $xml ? $xml : ['body' => file_get_contents($this->template) ?: "", 'headers' => []]; |
||
98 | } |
||
99 | else return ['body' => $this->template, 'headers' => []]; |
||
100 | } |
||
101 | |||
102 | //Load the TSS rules either from a file or as a string |
||
103 | //N.b. only files can be cached |
||
104 | private function getRules($template, $config) { |
||
108 | |||
109 | public function setCache(\ArrayAccess $cache) { |
||
112 | |||
113 | private function isValidDoc($xml) { |
||
116 | |||
117 | public function __destruct() { |
||
118 | //Required hack as DomXPath can only register static functions clear, the statically stored instance to avoid memory leaks |
||
119 | if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); |
||
121 | } |
||
122 |