1 | <?php |
||
9 | class Builder { |
||
10 | private $template; |
||
11 | private $tss; |
||
12 | private $registeredProperties = []; |
||
13 | private $formatters = []; |
||
14 | private $locale; |
||
15 | private $baseDir; |
||
16 | private $cache; |
||
17 | private $time; |
||
18 | |||
19 | public function __construct($template, $tss = '') { |
||
24 | |||
25 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
||
26 | //Would be better if PHP allowed setting the script clock, but this is the simplest way of overriding it |
||
27 | public function setTime($time) { |
||
30 | |||
31 | public function output($data = null, $document = false) { |
||
32 | $locale = $this->getLocale(); |
||
33 | $data = new Hook\DataFunction(new \SplObjectStorage(), $data, $locale, $this->baseDir); |
||
34 | $valueParser = new ValueParser($data); |
||
35 | $headers = []; |
||
36 | $this->registerBasicProperties($data, $locale, $headers); |
||
37 | |||
38 | $cachedOutput = $this->loadTemplate(); |
||
39 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
||
40 | $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
||
41 | |||
42 | foreach ($this->getRules($template, $valueParser) as $rule) { |
||
43 | if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $data, $valueParser); |
||
44 | } |
||
45 | |||
46 | $result = ['body' => $template->output($document), 'headers' => array_merge($cachedOutput['headers'], $headers)]; |
||
47 | $this->cache->write($this->template, $result); |
||
48 | $result['body'] = $this->doPostProcessing($template)->output($document); |
||
49 | |||
50 | return (object) $result; |
||
51 | } |
||
52 | |||
53 | //Register the basic properties, content, repeat, display and bind |
||
54 | private function registerBasicProperties($data, $locale, &$headers) { |
||
67 | |||
68 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
||
69 | private function doPostProcessing($template) { |
||
73 | |||
74 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
||
75 | private function executeTssRule($rule, $template, $data, $valueParser) { |
||
76 | $rule->touch(); |
||
77 | $hook = new Hook\Rule($rule->properties, new Hook\PseudoMatcher($rule->pseudo, $data), $valueParser); |
||
78 | foreach ($this->registeredProperties as $name => $property) $hook->registerProperty($name, $property); |
||
79 | $template->addHook($rule->query, $hook); |
||
80 | } |
||
81 | |||
82 | //Load a template, firstly check if it's a file or a valid string |
||
83 | private function loadTemplate() { |
||
90 | |||
91 | //Load the TSS rules either from a file or as a string |
||
92 | //N.b. only files can be cached |
||
93 | private function getRules($template, $valueParser) { |
||
94 | if (is_file($this->tss)) { |
||
95 | $this->baseDir = dirname(realpath($this->tss)) . DIRECTORY_SEPARATOR; |
||
96 | //The cache for the key: the filename and template prefix |
||
97 | //Each template may have a different prefix which changes the parsed TSS, |
||
98 | //Because of this the cache needs to be generated for each template prefix. |
||
99 | $key = $this->tss . $template->getPrefix() . $this->baseDir; |
||
100 | //Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
||
101 | $rules = $this->cache->load($key, filemtime($this->tss)); |
||
102 | if (!$rules) return $this->cache->write($key, (new Sheet(file_get_contents($this->tss), $this->baseDir, $valueParser, $template->getPrefix()))->parse()); |
||
103 | else return $rules; |
||
104 | } |
||
105 | else return (new Sheet($this->tss, $this->baseDir, $valueParser, $template->getPrefix()))->parse(); |
||
106 | } |
||
107 | |||
108 | public function setCache(\ArrayAccess $cache) { |
||
111 | |||
112 | private function getLocale() { |
||
117 | |||
118 | public function registerProperty($name, Property $property) { |
||
121 | |||
122 | public function registerFormatter($formatter) { |
||
125 | |||
126 | public function setLocale($locale) { |
||
129 | |||
130 | private function isValidDoc($xml) { |
||
133 | } |
||
134 |