@@ -20,6 +20,9 @@ discard block |
||
20 | 20 | '\\Transphporm\\Module\\Functions' |
21 | 21 | ]; |
22 | 22 | |
23 | + /** |
|
24 | + * @param string $template |
|
25 | + */ |
|
23 | 26 | public function __construct($template, $tss = '', $modules = null) { |
24 | 27 | $this->template = $template; |
25 | 28 | $this->tss = $tss; |
@@ -62,6 +65,12 @@ discard block |
||
62 | 65 | return (object) $result; |
63 | 66 | } |
64 | 67 | |
68 | + /** |
|
69 | + * @param Template $template |
|
70 | + * @param FunctionSet $data |
|
71 | + * @param Config $config |
|
72 | + * @param FunctionSet $functionSet |
|
73 | + */ |
|
65 | 74 | private function processRules($template, $data, $config, $functionSet) { |
66 | 75 | $valueParser = new Parser\Value($data); |
67 | 76 | $rules = $this->getRules($template, $valueParser); |
@@ -72,12 +81,20 @@ discard block |
||
72 | 81 | } |
73 | 82 | |
74 | 83 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
84 | + |
|
85 | + /** |
|
86 | + * @param Template $template |
|
87 | + */ |
|
75 | 88 | private function doPostProcessing($template) { |
76 | 89 | $template->addHook('//*[@transphporm]', new Hook\PostProcess()); |
77 | 90 | return $template; |
78 | 91 | } |
79 | 92 | |
80 | 93 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
94 | + |
|
95 | + /** |
|
96 | + * @param Parser\Value $valueParser |
|
97 | + */ |
|
81 | 98 | private function executeTssRule($rule, $template, $valueParser, $config, $functionSet) { |
82 | 99 | $rule->touch(); |
83 | 100 | $pseudoMatcher = $config->createPseudoMatcher($rule->pseudo); |
@@ -98,6 +115,10 @@ discard block |
||
98 | 115 | |
99 | 116 | //Load the TSS rules either from a file or as a string |
100 | 117 | //N.b. only files can be cached |
118 | + |
|
119 | + /** |
|
120 | + * @param Parser\Value $valueParser |
|
121 | + */ |
|
101 | 122 | private function getRules($template, $valueParser) { |
102 | 123 | if (is_file($this->tss)) { |
103 | 124 | $this->baseDir = dirname(realpath($this->tss)) . DIRECTORY_SEPARATOR; |
@@ -26,7 +26,9 @@ discard block |
||
26 | 26 | $this->cache = new Cache(new \ArrayObject()); |
27 | 27 | |
28 | 28 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
29 | - foreach ($modules as $module) $this->loadModule(new $module); |
|
29 | + foreach ($modules as $module) { |
|
30 | + $this->loadModule(new $module); |
|
31 | + } |
|
30 | 32 | } |
31 | 33 | |
32 | 34 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
@@ -47,7 +49,9 @@ discard block |
||
47 | 49 | $data = new FunctionSet($elementData, $this->baseDir); |
48 | 50 | $config = new Config($data, $elementData, new Hook\Formatter(), $headers, $this->baseDir); |
49 | 51 | |
50 | - foreach ($this->modules as $module) $module->load($config); |
|
52 | + foreach ($this->modules as $module) { |
|
53 | + $module->load($config); |
|
54 | + } |
|
51 | 55 | |
52 | 56 | $cachedOutput = $this->loadTemplate(); |
53 | 57 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
@@ -67,7 +71,9 @@ discard block |
||
67 | 71 | $rules = $this->getRules($template, $valueParser); |
68 | 72 | |
69 | 73 | foreach ($rules as $rule) { |
70 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $valueParser, $config, $functionSet); |
|
74 | + if ($rule->shouldRun($this->time)) { |
|
75 | + $this->executeTssRule($rule, $template, $valueParser, $config, $functionSet); |
|
76 | + } |
|
71 | 77 | } |
72 | 78 | } |
73 | 79 | |
@@ -92,8 +98,9 @@ discard block |
||
92 | 98 | if (trim($this->template)[0] !== '<') { |
93 | 99 | $xml = $this->cache->load($this->template, filemtime($this->template)); |
94 | 100 | return $xml ? $xml : ['body' => file_get_contents($this->template), 'headers' => []]; |
101 | + } else { |
|
102 | + return ['body' => $this->template, 'headers' => []]; |
|
95 | 103 | } |
96 | - else return ['body' => $this->template, 'headers' => []]; |
|
97 | 104 | } |
98 | 105 | |
99 | 106 | //Load the TSS rules either from a file or as a string |
@@ -107,10 +114,14 @@ discard block |
||
107 | 114 | $key = $this->tss . $template->getPrefix() . $this->baseDir; |
108 | 115 | //Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
109 | 116 | $rules = $this->cache->load($key, filemtime($this->tss)); |
110 | - if (!$rules) return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->baseDir, $valueParser, $template->getPrefix()))->parse()); |
|
111 | - else return $rules; |
|
117 | + if (!$rules) { |
|
118 | + return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->baseDir, $valueParser, $template->getPrefix()))->parse()); |
|
119 | + } else { |
|
120 | + return $rules; |
|
121 | + } |
|
122 | + } else { |
|
123 | + return (new Parser\Sheet($this->tss, $this->baseDir, $valueParser, $template->getPrefix()))->parse(); |
|
112 | 124 | } |
113 | - else return (new Parser\Sheet($this->tss, $this->baseDir, $valueParser, $template->getPrefix()))->parse(); |
|
114 | 125 | } |
115 | 126 | |
116 | 127 | public function setCache(\ArrayAccess $cache) { |
@@ -31,6 +31,9 @@ discard block |
||
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | + /** |
|
35 | + * @param \DOMElement $element |
|
36 | + */ |
|
34 | 37 | private function getArgs($value, $element) { |
35 | 38 | return $this->valueParser->parse($value, $element); |
36 | 39 | } |
@@ -39,6 +42,9 @@ discard block |
||
39 | 42 | $this->properties[$name] = $property; |
40 | 43 | } |
41 | 44 | |
45 | + /** |
|
46 | + * @param \DOMElement $element |
|
47 | + */ |
|
42 | 48 | private function callProperty($name, $element, $value) { |
43 | 49 | if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
44 | 50 | return false; |
@@ -61,7 +61,9 @@ discard block |
||
61 | 61 | $this->data = $data; |
62 | 62 | $this->last = null; |
63 | 63 | |
64 | - if (empty($tokens)) return [$this->data]; |
|
64 | + if (empty($tokens)) { |
|
65 | + return [$this->data]; |
|
66 | + } |
|
65 | 67 | |
66 | 68 | foreach ($tokens as $token) { |
67 | 69 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -75,27 +77,36 @@ discard block |
||
75 | 77 | |
76 | 78 | if ($this->mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) { |
77 | 79 | $this->mode = Tokenizer::NOT; |
80 | + } else { |
|
81 | + $this->mode = $token['type']; |
|
78 | 82 | } |
79 | - else $this->mode = $token['type']; |
|
80 | 83 | } |
81 | 84 | |
82 | 85 | |
83 | 86 | //Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
84 | 87 | private function moveLastToData() { |
85 | - if (isset($this->data->{$this->last})) $this->data = $this->data->{$this->last}; |
|
86 | - else if (is_array($this->data) && isset($this->data[$this->last])) $this->data = $this->data[$this->last]; |
|
88 | + if (isset($this->data->{$this->last})) { |
|
89 | + $this->data = $this->data->{$this->last}; |
|
90 | + } else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
91 | + $this->data = $this->data[$this->last]; |
|
92 | + } |
|
87 | 93 | } |
88 | 94 | |
89 | 95 | //Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
90 | 96 | private function processDot($token) { |
91 | - if ($this->last !== null) $this->moveLastToData(); |
|
92 | - else $this->data = array_pop($this->result); |
|
97 | + if ($this->last !== null) { |
|
98 | + $this->moveLastToData(); |
|
99 | + } else { |
|
100 | + $this->data = array_pop($this->result); |
|
101 | + } |
|
93 | 102 | |
94 | 103 | $this->last = null; |
95 | 104 | } |
96 | 105 | |
97 | 106 | private function processSquareBracket($token) { |
98 | - if ($this->last !== null) $this->moveLastToData(); |
|
107 | + if ($this->last !== null) { |
|
108 | + $this->moveLastToData(); |
|
109 | + } |
|
99 | 110 | |
100 | 111 | $parser = new Value($this->baseData, $this->autoLookup); |
101 | 112 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
@@ -118,15 +129,15 @@ discard block |
||
118 | 129 | private function processBrackets($token) { |
119 | 130 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
120 | 131 | $this->callTransphpormFunctions($token); |
121 | - } |
|
122 | - else if ($this->data instanceof \Transphporm\Functionset) { |
|
132 | + } else if ($this->data instanceof \Transphporm\Functionset) { |
|
123 | 133 | $this->result = $this->processValue($this->data->{$this->last}($token['value'])); |
124 | 134 | $this->last = null; |
125 | - } |
|
126 | - else { |
|
135 | + } else { |
|
127 | 136 | $parser = new Value($this->baseData, $this->autoLookup); |
128 | 137 | $args = $parser->parseTokens($token['value'], $this->data); |
129 | - if ($args[0] == $this->data) $args = []; |
|
138 | + if ($args[0] == $this->data) { |
|
139 | + $args = []; |
|
140 | + } |
|
130 | 141 | $funcResult = $this->callFunc($this->last, $args, $this->data); |
131 | 142 | $this->result = $this->processValue($funcResult); |
132 | 143 | $this->last = null; |
@@ -137,9 +148,12 @@ discard block |
||
137 | 148 | $this->result = $this->processValue($this->baseData->{$this->last}($token['value'])); |
138 | 149 | foreach ($this->result as $i => $value) { |
139 | 150 | if (is_array($this->data)) { |
140 | - if (isset($this->data[$value])) $this->result[$i] = $this->data[$value]; |
|
151 | + if (isset($this->data[$value])) { |
|
152 | + $this->result[$i] = $this->data[$value]; |
|
153 | + } |
|
154 | + } else if (is_scalar($value) && isset($this->data->$value)) { |
|
155 | + $this->result[$i] = $this->data->$value; |
|
141 | 156 | } |
142 | - else if (is_scalar($value) && isset($this->data->$value)) $this->result[$i] = $this->data->$value; |
|
143 | 157 | } |
144 | 158 | $this->last = null; |
145 | 159 | } |
@@ -149,12 +163,12 @@ discard block |
||
149 | 163 | if ($this->last !== null) { |
150 | 164 | try { |
151 | 165 | $this->result = $this->extractLast($this->result); |
152 | - } |
|
153 | - catch (\UnexpectedValueException $e) { |
|
166 | + } catch (\UnexpectedValueException $e) { |
|
154 | 167 | if (!$this->autoLookup) { |
155 | 168 | $this->result = $this->processValue($this->last); |
169 | + } else { |
|
170 | + $this->result = [false]; |
|
156 | 171 | } |
157 | - else $this->result = [false]; |
|
158 | 172 | } |
159 | 173 | } |
160 | 174 | return $this->result; |
@@ -166,8 +180,7 @@ discard block |
||
166 | 180 | private function extractLast($result) { |
167 | 181 | if ($this->autoLookup && isset($this->data->{$this->last})) { |
168 | 182 | return $this->processValue($this->data->{$this->last}); |
169 | - } |
|
170 | - else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
183 | + } else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
171 | 184 | return $this->processValue($this->data[$this->last]); |
172 | 185 | } |
173 | 186 | throw new \UnexpectedValueException('Not found'); |
@@ -178,14 +191,11 @@ discard block |
||
178 | 191 | private function processValue($newValue) { |
179 | 192 | if ($this->mode == Tokenizer::ARG) { |
180 | 193 | $this->result[] = $newValue; |
181 | - } |
|
182 | - else if ($this->mode == Tokenizer::CONCAT) { |
|
194 | + } else if ($this->mode == Tokenizer::CONCAT) { |
|
183 | 195 | $this->result[count($this->result)-1] .= $newValue; |
184 | - } |
|
185 | - else if ($this->mode == Tokenizer::NOT) { |
|
196 | + } else if ($this->mode == Tokenizer::NOT) { |
|
186 | 197 | $this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $newValue; |
187 | - } |
|
188 | - else if ($this->mode == Tokenizer::EQUALS) { |
|
198 | + } else if ($this->mode == Tokenizer::EQUALS) { |
|
189 | 199 | $this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $newValue; |
190 | 200 | } |
191 | 201 | |
@@ -193,12 +203,18 @@ discard block |
||
193 | 203 | } |
194 | 204 | |
195 | 205 | private function callFunc($name, $args, $data) { |
196 | - if ($this->data instanceof \Transphporm\FunctionSet) return $this->data->$name($args); |
|
197 | - else return $this->callFuncOnObject($this->data, $name, $args); |
|
206 | + if ($this->data instanceof \Transphporm\FunctionSet) { |
|
207 | + return $this->data->$name($args); |
|
208 | + } else { |
|
209 | + return $this->callFuncOnObject($this->data, $name, $args); |
|
210 | + } |
|
198 | 211 | } |
199 | 212 | |
200 | 213 | private function callFuncOnObject($obj, $func, $args) { |
201 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
|
202 | - else return call_user_func_array([$obj, $func], $args); |
|
214 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
215 | + return call_user_func_array($obj->$func, $args); |
|
216 | + } else { |
|
217 | + return call_user_func_array([$obj, $func], $args); |
|
218 | + } |
|
203 | 219 | } |
204 | 220 | } |