1 | <?php |
||
9 | class Builder { |
||
10 | private $template; |
||
11 | private $tss; |
||
12 | private $rootDir; |
||
|
|||
13 | private $cache; |
||
14 | private $time; |
||
15 | private $modules = []; |
||
16 | private $config; |
||
17 | private $filePath; |
||
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) { |
||
26 | $this->template = $template; |
||
27 | $this->tss = $tss; |
||
28 | $this->cache = new Cache(new \ArrayObject()); |
||
29 | $this->filePath = new FilePath(); |
||
30 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
||
31 | foreach ($modules as $module) $this->loadModule(new $module); |
||
32 | } |
||
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) { |
||
37 | $this->time = $time; |
||
38 | } |
||
39 | |||
40 | public function loadModule(Module $module) { |
||
41 | $this->modules[get_class($module)] = $module; |
||
42 | } |
||
43 | |||
44 | public function addPath($dir) { |
||
45 | $this->filePath->addPath($dir); |
||
46 | } |
||
47 | |||
48 | public function output($data = null, $document = false) { |
||
49 | $headers = []; |
||
50 | |||
51 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
||
52 | $functionSet = new FunctionSet($elementData); |
||
53 | |||
54 | $cachedOutput = $this->loadTemplate(); |
||
55 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
||
56 | $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
||
57 | $valueParser = new Parser\Value($functionSet); |
||
58 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
||
59 | |||
60 | foreach ($this->modules as $module) $module->load($this->config); |
||
61 | |||
62 | $this->processRules($template, $this->config); |
||
63 | |||
64 | $result = ['body' => $template->output($document), 'headers' => array_merge($cachedOutput['headers'], $headers)]; |
||
65 | $this->cache->write($this->template, $result); |
||
66 | $result['body'] = $this->doPostProcessing($template)->output($document); |
||
67 | return (object) $result; |
||
68 | } |
||
69 | |||
70 | private function processRules($template, $config) { |
||
77 | |||
78 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
||
79 | private function doPostProcessing($template) { |
||
83 | |||
84 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
||
85 | private function executeTssRule($rule, $template, $config) { |
||
86 | $rule->touch(); |
||
87 | |||
88 | $pseudoMatcher = $config->createPseudoMatcher($rule->pseudo); |
||
89 | $hook = new Hook\PropertyHook($rule->properties, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath()); |
||
90 | $config->loadProperties($hook); |
||
91 | $template->addHook($rule->query, $hook); |
||
92 | } |
||
93 | |||
94 | //Load a template, firstly check if it's a file or a valid string |
||
95 | private function loadTemplate() { |
||
102 | |||
103 | //Load the TSS rules either from a file or as a string |
||
104 | //N.b. only files can be cached |
||
105 | private function getRules($template, $config) { |
||
109 | |||
110 | public function setCache(\ArrayAccess $cache) { |
||
113 | |||
114 | private function isValidDoc($xml) { |
||
117 | |||
118 | public function __destruct() { |
||
119 | //Required hack as DomXPath can only register static functions clear, the statically stored instance to avoid memory leaks |
||
120 | $this->config->getCssToXpath()->cleanup(); |
||
121 | } |
||
122 | } |
||
123 |
This check marks private properties in classes that are never used. Those properties can be removed.