@@ -58,7 +58,7 @@ |
||
58 | 58 | |
59 | 59 | $cachedOutput = $this->loadTemplate(); |
60 | 60 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
61 | - $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
|
61 | + $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>'.$cachedOutput['body'].'</template>'); |
|
62 | 62 | $this->tssCache = new TSSCache($this->cache, $template->getPrefix()); |
63 | 63 | $valueParser = new Parser\Value($functionSet); |
64 | 64 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
@@ -28,7 +28,9 @@ discard block |
||
28 | 28 | $this->cache = new Cache(new \ArrayObject()); |
29 | 29 | $this->filePath = new FilePath(); |
30 | 30 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
31 | - foreach ($modules as $module) $this->loadModule(new $module); |
|
31 | + foreach ($modules as $module) { |
|
32 | + $this->loadModule(new $module); |
|
33 | + } |
|
32 | 34 | } |
33 | 35 | |
34 | 36 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
@@ -63,7 +65,9 @@ discard block |
||
63 | 65 | $valueParser = new Parser\Value($functionSet); |
64 | 66 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
65 | 67 | |
66 | - foreach ($this->modules as $module) $module->load($this->config); |
|
68 | + foreach ($this->modules as $module) { |
|
69 | + $module->load($this->config); |
|
70 | + } |
|
67 | 71 | |
68 | 72 | $this->processRules($template, $this->config); |
69 | 73 | |
@@ -77,10 +81,14 @@ discard block |
||
77 | 81 | $rules = $this->getRules($template, $config); |
78 | 82 | |
79 | 83 | foreach ($rules as $rule) { |
80 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
84 | + if ($rule->shouldRun($this->time)) { |
|
85 | + $this->executeTssRule($rule, $template, $config); |
|
86 | + } |
|
81 | 87 | } |
82 | 88 | |
83 | - if (is_file($this->tss)) $this->tssCache->write($this->tss, $rules); |
|
89 | + if (is_file($this->tss)) { |
|
90 | + $this->tssCache->write($this->tss, $rules); |
|
91 | + } |
|
84 | 92 | } |
85 | 93 | |
86 | 94 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
@@ -102,7 +110,9 @@ discard block |
||
102 | 110 | //Load a template, firstly check if it's a file or a valid string |
103 | 111 | private function loadTemplate() { |
104 | 112 | $result = ['body' => $this->template, 'headers' => []]; |
105 | - if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
|
113 | + if (strpos($this->template, "\n") === false && is_file($this->template)) { |
|
114 | + $result = $this->loadTemplateFromFile($this->template); |
|
115 | + } |
|
106 | 116 | return $result; |
107 | 117 | } |
108 | 118 | |
@@ -127,6 +137,8 @@ discard block |
||
127 | 137 | |
128 | 138 | public function __destruct() { |
129 | 139 | //Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks |
130 | - if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); |
|
140 | + if (isset($this->config)) { |
|
141 | + $this->config->getCssToXpath()->cleanup(); |
|
142 | + } |
|
131 | 143 | } |
132 | 144 | } |
@@ -25,14 +25,19 @@ discard block |
||
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | 27 | $this->filePath->addPath(dirname(realpath($tss))); |
28 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
29 | - else return; |
|
28 | + if (empty($this->rules)) { |
|
29 | + $tss = file_get_contents($tss); |
|
30 | + } else { |
|
31 | + return; |
|
32 | + } |
|
30 | 33 | } |
31 | 34 | $this->tss = (new Tokenizer($tss))->getTokens(); |
32 | 35 | } |
33 | 36 | |
34 | 37 | public function parse($indexStart = 0) { |
35 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
38 | + if (!empty($this->rules)) { |
|
39 | + return $this->rules['rules']; |
|
40 | + } |
|
36 | 41 | $rules = $this->parseTokens($indexStart); |
37 | 42 | usort($rules, [$this, 'sortRules']); |
38 | 43 | $this->checkError($rules); |
@@ -45,8 +50,9 @@ discard block |
||
45 | 50 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
46 | 51 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
47 | 52 | $this->rules = array_merge($this->rules, $processing); |
53 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
54 | + $this->addRules($token, $indexStart++); |
|
48 | 55 | } |
49 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart++); |
|
50 | 56 | } |
51 | 57 | |
52 | 58 | return $this->rules; |
@@ -56,14 +62,18 @@ discard block |
||
56 | 62 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
57 | 63 | |
58 | 64 | $this->tss->skip(count($selector)); |
59 | - if (count($selector) === 0) return; |
|
65 | + if (count($selector) === 0) { |
|
66 | + return; |
|
67 | + } |
|
60 | 68 | |
61 | 69 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
62 | 70 | $this->rules = $this->writeRule($this->rules, $newRules); |
63 | 71 | } |
64 | 72 | |
65 | 73 | private function checkError($rules) { |
66 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
74 | + if (empty($rules) && count($this->tss) > 0) { |
|
75 | + throw new \Exception('No TSS rules parsed'); |
|
76 | + } |
|
67 | 77 | } |
68 | 78 | |
69 | 79 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -91,7 +101,9 @@ discard block |
||
91 | 101 | } |
92 | 102 | |
93 | 103 | private function processingInstructions($token, $indexStart) { |
94 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
104 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
105 | + return false; |
|
106 | + } |
|
95 | 107 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
96 | 108 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
97 | 109 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -111,10 +123,15 @@ discard block |
||
111 | 123 | |
112 | 124 | private function sortRules($a, $b) { |
113 | 125 | //If they have the same depth, compare on index |
114 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
126 | + if ($a->query === $b->query) { |
|
127 | + return $this->sortPseudo($a, $b); |
|
128 | + } |
|
115 | 129 | |
116 | - if ($a->depth === $b->depth) $property = 'index'; |
|
117 | - else $property = 'depth'; |
|
130 | + if ($a->depth === $b->depth) { |
|
131 | + $property = 'index'; |
|
132 | + } else { |
|
133 | + $property = 'depth'; |
|
134 | + } |
|
118 | 135 | |
119 | 136 | return ($a->$property < $b->$property) ? -1 : 1; |
120 | 137 | } |
@@ -6,13 +6,13 @@ discard block |
||
6 | 6 | * @version 1.2 */ |
7 | 7 | namespace Transphporm; |
8 | 8 | class TSSCache { |
9 | - private $cache; |
|
10 | - private $prefix; |
|
9 | + private $cache; |
|
10 | + private $prefix; |
|
11 | 11 | |
12 | - public function __construct(Cache $cache, $prefix) { |
|
13 | - $this->cache = $cache; |
|
14 | - $this->prefix = $prefix; |
|
15 | - } |
|
12 | + public function __construct(Cache $cache, $prefix) { |
|
13 | + $this->cache = $cache; |
|
14 | + $this->prefix = $prefix; |
|
15 | + } |
|
16 | 16 | |
17 | 17 | private function getRulesFromCache($file) { |
18 | 18 | //The cache for the key: the filename and template prefix |
@@ -33,17 +33,17 @@ discard block |
||
33 | 33 | return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
34 | 34 | } |
35 | 35 | |
36 | - public function load($tss) { |
|
37 | - return $this->getRulesFromCache($tss); |
|
38 | - } |
|
36 | + public function load($tss) { |
|
37 | + return $this->getRulesFromCache($tss); |
|
38 | + } |
|
39 | 39 | |
40 | - public function write($file, $rules, $imports = []) { |
|
41 | - if (is_file($file)) { |
|
42 | - $key = $this->getCacheKey($file); |
|
43 | - $existing = $this->cache->load($key, filemtime($file)); |
|
44 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
45 | - $this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
|
46 | - } |
|
47 | - return $rules; |
|
48 | - } |
|
40 | + public function write($file, $rules, $imports = []) { |
|
41 | + if (is_file($file)) { |
|
42 | + $key = $this->getCacheKey($file); |
|
43 | + $existing = $this->cache->load($key, filemtime($file)); |
|
44 | + if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
45 | + $this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
|
46 | + } |
|
47 | + return $rules; |
|
48 | + } |
|
49 | 49 | } |
@@ -23,7 +23,9 @@ discard block |
||
23 | 23 | $rules = $this->cache->load($key, filemtime($file)); |
24 | 24 | if ($rules) { |
25 | 25 | foreach ($rules['import'] as $file) { |
26 | - if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false; |
|
26 | + if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) { |
|
27 | + return false; |
|
28 | + } |
|
27 | 29 | } |
28 | 30 | } |
29 | 31 | return $rules; |
@@ -41,7 +43,9 @@ discard block |
||
41 | 43 | if (is_file($file)) { |
42 | 44 | $key = $this->getCacheKey($file); |
43 | 45 | $existing = $this->cache->load($key, filemtime($file)); |
44 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
46 | + if (isset($existing['import']) && empty($imports)) { |
|
47 | + $imports = $existing['import']; |
|
48 | + } |
|
45 | 49 | $this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
46 | 50 | } |
47 | 51 | return $rules; |