@@ -15,6 +15,9 @@ discard block |
||
15 | 15 | private $rules; |
16 | 16 | private $cacheName; |
17 | 17 | |
18 | + /** |
|
19 | + * @param string $tss |
|
20 | + */ |
|
18 | 21 | public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
19 | 22 | $this->cache = $cache; |
20 | 23 | $this->filePath = $filePath; |
@@ -99,6 +102,9 @@ discard block |
||
99 | 102 | return $rules; |
100 | 103 | } |
101 | 104 | |
105 | + /** |
|
106 | + * @param Template $template |
|
107 | + */ |
|
102 | 108 | public function processRules($template, \Transphporm\Config $config) { |
103 | 109 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
104 | 110 | |
@@ -113,6 +119,11 @@ discard block |
||
113 | 119 | } |
114 | 120 | |
115 | 121 | //Load the TSS |
122 | + |
|
123 | + /** |
|
124 | + * @param Parser\CssToXpath $cssToXpath |
|
125 | + * @param Parser\Value $valueParser |
|
126 | + */ |
|
116 | 127 | public function getRules($tss, $cssToXpath, $valueParser, $indexStart = 0) { |
117 | 128 | if (is_file($tss)) { |
118 | 129 | //$rules = $this->cache->load($tss); |
@@ -124,6 +135,10 @@ discard block |
||
124 | 135 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart); |
125 | 136 | } |
126 | 137 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
138 | + |
|
139 | + /** |
|
140 | + * @param Config $config |
|
141 | + */ |
|
127 | 142 | private function executeTssRule($rule, $template, $config) { |
128 | 143 | $rule->touch(); |
129 | 144 |
@@ -7,21 +7,21 @@ discard block |
||
7 | 7 | namespace Transphporm; |
8 | 8 | //Separates out TSS file loading/caching from parsing |
9 | 9 | class SheetLoader { |
10 | - private $cache; |
|
11 | - private $sheet; |
|
12 | - private $time; |
|
13 | - private $import = []; |
|
14 | - private $cacheKey; |
|
15 | - private $rules; |
|
16 | - private $cacheName; |
|
17 | - |
|
18 | - public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
19 | - $this->cache = $cache; |
|
20 | - $this->filePath = $filePath; |
|
21 | - $this->tss = $tss; |
|
22 | - $this->time = $time ?? time(); |
|
23 | - $this->cacheName = $tss; |
|
24 | - } |
|
10 | + private $cache; |
|
11 | + private $sheet; |
|
12 | + private $time; |
|
13 | + private $import = []; |
|
14 | + private $cacheKey; |
|
15 | + private $rules; |
|
16 | + private $cacheName; |
|
17 | + |
|
18 | + public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
19 | + $this->cache = $cache; |
|
20 | + $this->filePath = $filePath; |
|
21 | + $this->tss = $tss; |
|
22 | + $this->time = $time ?? time(); |
|
23 | + $this->cacheName = $tss; |
|
24 | + } |
|
25 | 25 | |
26 | 26 | private function getRulesFromCache($file) { |
27 | 27 | //Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
@@ -90,14 +90,14 @@ discard block |
||
90 | 90 | } |
91 | 91 | |
92 | 92 | //write the sheet to cache |
93 | - public function write($file, $rules, $imports = []) { |
|
93 | + public function write($file, $rules, $imports = []) { |
|
94 | 94 | if (is_file($file)) { |
95 | 95 | $existing = $this->cache->load($file, filemtime($file)); |
96 | 96 | if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
97 | 97 | $this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
98 | 98 | } |
99 | 99 | return $rules; |
100 | - } |
|
100 | + } |
|
101 | 101 | |
102 | 102 | public function processRules($template, \Transphporm\Config $config) { |
103 | 103 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
@@ -115,12 +115,12 @@ discard block |
||
115 | 115 | //Load the TSS |
116 | 116 | public function getRules($tss, $cssToXpath, $valueParser, $indexStart = 0) { |
117 | 117 | if (is_file($tss)) { |
118 | - //$rules = $this->cache->load($tss); |
|
119 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
118 | + //$rules = $this->cache->load($tss); |
|
119 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
120 | 120 | $this->filePath->addPath(dirname(realpath($tss))); |
121 | 121 | if (empty($rules)) $tss = file_get_contents($tss); |
122 | 122 | else return $rules; |
123 | - } |
|
123 | + } |
|
124 | 124 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart); |
125 | 125 | } |
126 | 126 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | public function updateRequired($data) { |
47 | 47 | if (!is_file($this->tss)) return true; |
48 | 48 | |
49 | - $this->cacheName = $this->getCacheKey($data) . $this->tss; |
|
49 | + $this->cacheName = $this->getCacheKey($data).$this->tss; |
|
50 | 50 | |
51 | 51 | $rules = $this->getRulesFromCache($this->tss, $data); |
52 | 52 | //Nothing was cached or the TSS file has changed, update is required |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | if (is_file($this->tss)) $this->getRulesFromCache($this->tss); |
83 | 83 | if ($this->cacheKey) { |
84 | 84 | $parser = new Parser\Value($data); |
85 | - $x= $parser->parseTokens($this->cacheKey)[0]; |
|
86 | - $this->cacheName = $x . $this->tss; |
|
85 | + $x = $parser->parseTokens($this->cacheKey)[0]; |
|
86 | + $this->cacheName = $x.$this->tss; |
|
87 | 87 | return $x; |
88 | 88 | } |
89 | 89 | else return ''; |
@@ -146,6 +146,6 @@ discard block |
||
146 | 146 | |
147 | 147 | |
148 | 148 | private function sortPseudo($a, $b) { |
149 | - return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
149 | + return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
150 | 150 | } |
151 | 151 | } |
@@ -32,7 +32,9 @@ discard block |
||
32 | 32 | if ($rules) { |
33 | 33 | foreach ($rules['import'] as $file) { |
34 | 34 | //Check that the import file hasn't been changed since the cache was written |
35 | - if (filemtime($file) > $rules['ctime']) return false; |
|
35 | + if (filemtime($file) > $rules['ctime']) { |
|
36 | + return false; |
|
37 | + } |
|
36 | 38 | } |
37 | 39 | } |
38 | 40 | |
@@ -44,16 +46,22 @@ discard block |
||
44 | 46 | // 2. If the data hasn't changed since the last run |
45 | 47 | //If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code |
46 | 48 | public function updateRequired($data) { |
47 | - if (!is_file($this->tss)) return true; |
|
49 | + if (!is_file($this->tss)) { |
|
50 | + return true; |
|
51 | + } |
|
48 | 52 | |
49 | 53 | $this->cacheName = $this->getCacheKey($data) . $this->tss; |
50 | 54 | |
51 | 55 | $rules = $this->getRulesFromCache($this->tss, $data); |
52 | 56 | //Nothing was cached or the TSS file has changed, update is required |
53 | - if (empty($rules)) return true; |
|
57 | + if (empty($rules)) { |
|
58 | + return true; |
|
59 | + } |
|
54 | 60 | |
55 | 61 | //Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required |
56 | - if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true; |
|
62 | + if ($rules['ctime']+$rules['minFreq'] <= $this->time) { |
|
63 | + return true; |
|
64 | + } |
|
57 | 65 | |
58 | 66 | return false; |
59 | 67 | } |
@@ -64,7 +72,9 @@ discard block |
||
64 | 72 | |
65 | 73 | foreach ($rules as $rule) { |
66 | 74 | $ruleFreq = $rule->getUpdateFrequency(); |
67 | - if ($ruleFreq < $min) $min = $ruleFreq; |
|
75 | + if ($ruleFreq < $min) { |
|
76 | + $min = $ruleFreq; |
|
77 | + } |
|
68 | 78 | } |
69 | 79 | |
70 | 80 | return $min; |
@@ -79,21 +89,26 @@ discard block |
||
79 | 89 | } |
80 | 90 | |
81 | 91 | public function getCacheKey($data) { |
82 | - if (is_file($this->tss)) $this->getRulesFromCache($this->tss); |
|
92 | + if (is_file($this->tss)) { |
|
93 | + $this->getRulesFromCache($this->tss); |
|
94 | + } |
|
83 | 95 | if ($this->cacheKey) { |
84 | 96 | $parser = new Parser\Value($data); |
85 | 97 | $x= $parser->parseTokens($this->cacheKey)[0]; |
86 | 98 | $this->cacheName = $x . $this->tss; |
87 | 99 | return $x; |
100 | + } else { |
|
101 | + return ''; |
|
88 | 102 | } |
89 | - else return ''; |
|
90 | 103 | } |
91 | 104 | |
92 | 105 | //write the sheet to cache |
93 | 106 | public function write($file, $rules, $imports = []) { |
94 | 107 | if (is_file($file)) { |
95 | 108 | $existing = $this->cache->load($file, filemtime($file)); |
96 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
109 | + if (isset($existing['import']) && empty($imports)) { |
|
110 | + $imports = $existing['import']; |
|
111 | + } |
|
97 | 112 | $this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
98 | 113 | } |
99 | 114 | return $rules; |
@@ -106,10 +121,14 @@ discard block |
||
106 | 121 | usort($rules, [$this, 'sortRules']); |
107 | 122 | |
108 | 123 | foreach ($rules as $rule) { |
109 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
124 | + if ($rule->shouldRun($this->time)) { |
|
125 | + $this->executeTssRule($rule, $template, $config); |
|
126 | + } |
|
110 | 127 | } |
111 | 128 | |
112 | - if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
129 | + if (is_file($this->tss)) { |
|
130 | + $this->write($this->tss, $rules, $this->import); |
|
131 | + } |
|
113 | 132 | } |
114 | 133 | |
115 | 134 | //Load the TSS |
@@ -118,8 +137,11 @@ discard block |
||
118 | 137 | //$rules = $this->cache->load($tss); |
119 | 138 | $rules = $this->getRulesFromCache($tss)['rules']; |
120 | 139 | $this->filePath->addPath(dirname(realpath($tss))); |
121 | - if (empty($rules)) $tss = file_get_contents($tss); |
|
122 | - else return $rules; |
|
140 | + if (empty($rules)) { |
|
141 | + $tss = file_get_contents($tss); |
|
142 | + } else { |
|
143 | + return $rules; |
|
144 | + } |
|
123 | 145 | } |
124 | 146 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart); |
125 | 147 | } |
@@ -136,10 +158,15 @@ discard block |
||
136 | 158 | |
137 | 159 | private function sortRules($a, $b) { |
138 | 160 | //If they have the same depth, compare on index |
139 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
161 | + if ($a->query === $b->query) { |
|
162 | + return $this->sortPseudo($a, $b); |
|
163 | + } |
|
140 | 164 | |
141 | - if ($a->depth === $b->depth) $property = 'index'; |
|
142 | - else $property = 'depth'; |
|
165 | + if ($a->depth === $b->depth) { |
|
166 | + $property = 'index'; |
|
167 | + } else { |
|
168 | + $property = 'depth'; |
|
169 | + } |
|
143 | 170 | |
144 | 171 | return ($a->$property < $b->$property) ? -1 : 1; |
145 | 172 | } |