@@ -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 | |
@@ -111,6 +117,11 @@ discard block |
||
| 111 | 117 | } |
| 112 | 118 | |
| 113 | 119 | //Load the TSS |
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param Parser\CssToXpath $cssToXpath |
|
| 123 | + * @param Parser\Value $valueParser |
|
| 124 | + */ |
|
| 114 | 125 | public function getRules($tss, $cssToXpath, $valueParser) { |
| 115 | 126 | if (is_file($tss)) { |
| 116 | 127 | //$rules = $this->cache->load($tss); |
@@ -122,6 +133,10 @@ discard block |
||
| 122 | 133 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
| 123 | 134 | } |
| 124 | 135 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param Config $config |
|
| 139 | + */ |
|
| 125 | 140 | private function executeTssRule($rule, $template, $config) { |
| 126 | 141 | $rule->touch(); |
| 127 | 142 | |
@@ -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()); |
@@ -113,12 +113,12 @@ discard block |
||
| 113 | 113 | //Load the TSS |
| 114 | 114 | public function getRules($tss, $cssToXpath, $valueParser) { |
| 115 | 115 | if (is_file($tss)) { |
| 116 | - //$rules = $this->cache->load($tss); |
|
| 117 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 116 | + //$rules = $this->cache->load($tss); |
|
| 117 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 118 | 118 | $this->filePath->addPath(dirname(realpath($tss))); |
| 119 | 119 | if (empty($rules)) $tss = file_get_contents($tss); |
| 120 | 120 | else return $rules; |
| 121 | - } |
|
| 121 | + } |
|
| 122 | 122 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
| 123 | 123 | } |
| 124 | 124 | //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 ''; |
@@ -144,6 +144,6 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | |
| 146 | 146 | private function sortPseudo($a, $b) { |
| 147 | - return count($a->pseudo) < count($b->pseudo) ? -1 :1; |
|
| 147 | + return count($a->pseudo) < count($b->pseudo) ? -1 : 1; |
|
| 148 | 148 | } |
| 149 | 149 | } |
@@ -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; |
@@ -104,10 +119,14 @@ discard block |
||
| 104 | 119 | |
| 105 | 120 | usort($rules, [$this, 'sortRules']); |
| 106 | 121 | foreach ($rules as $rule) { |
| 107 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
| 122 | + if ($rule->shouldRun($this->time)) { |
|
| 123 | + $this->executeTssRule($rule, $template, $config); |
|
| 124 | + } |
|
| 108 | 125 | } |
| 109 | 126 | |
| 110 | - if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
| 127 | + if (is_file($this->tss)) { |
|
| 128 | + $this->write($this->tss, $rules, $this->import); |
|
| 129 | + } |
|
| 111 | 130 | } |
| 112 | 131 | |
| 113 | 132 | //Load the TSS |
@@ -116,8 +135,11 @@ discard block |
||
| 116 | 135 | //$rules = $this->cache->load($tss); |
| 117 | 136 | $rules = $this->getRulesFromCache($tss)['rules']; |
| 118 | 137 | $this->filePath->addPath(dirname(realpath($tss))); |
| 119 | - if (empty($rules)) $tss = file_get_contents($tss); |
|
| 120 | - else return $rules; |
|
| 138 | + if (empty($rules)) { |
|
| 139 | + $tss = file_get_contents($tss); |
|
| 140 | + } else { |
|
| 141 | + return $rules; |
|
| 142 | + } |
|
| 121 | 143 | } |
| 122 | 144 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
| 123 | 145 | } |
@@ -134,10 +156,15 @@ discard block |
||
| 134 | 156 | |
| 135 | 157 | private function sortRules($a, $b) { |
| 136 | 158 | //If they have the same depth, compare on index |
| 137 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
| 159 | + if ($a->query === $b->query) { |
|
| 160 | + return $this->sortPseudo($a, $b); |
|
| 161 | + } |
|
| 138 | 162 | |
| 139 | - if ($a->depth === $b->depth) $property = 'index'; |
|
| 140 | - else $property = 'depth'; |
|
| 163 | + if ($a->depth === $b->depth) { |
|
| 164 | + $property = 'index'; |
|
| 165 | + } else { |
|
| 166 | + $property = 'depth'; |
|
| 167 | + } |
|
| 141 | 168 | |
| 142 | 169 | return ($a->$property < $b->$property) ? -1 : 1; |
| 143 | 170 | } |
@@ -42,9 +42,9 @@ discard block |
||
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public function setLocale($locale) { |
| 45 | - $format = new \Transphporm\Module\Format($locale); |
|
| 46 | - $this->modules[get_class($format)] = $format; |
|
| 47 | - } |
|
| 45 | + $format = new \Transphporm\Module\Format($locale); |
|
| 46 | + $this->modules[get_class($format)] = $format; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | public function addPath($dir) { |
| 50 | 50 | $this->filePath->addPath($dir); |
@@ -97,15 +97,15 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | //Load a template, firstly check if it's a file or a valid string |
| 99 | 99 | private function loadTemplate() { |
| 100 | - $result = ['cache' => $this->template, 'headers' => []]; |
|
| 100 | + $result = ['cache' => $this->template, 'headers' => []]; |
|
| 101 | 101 | if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
| 102 | 102 | return $result; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - private function loadTemplateFromFile($file) { |
|
| 106 | - $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 107 | - return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
| 108 | - } |
|
| 105 | + private function loadTemplateFromFile($file) { |
|
| 106 | + $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 107 | + return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | public function setCache(\ArrayAccess $cache) { |
| 111 | 111 | $this->cache = new Cache($cache); |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | 'body' => $this->doPostProcessing($template)->output($document) |
| 70 | 70 | ]; |
| 71 | 71 | |
| 72 | - $this->cache->write($tssCache->getCacheKey($data) . $this->template, $result); |
|
| 72 | + $this->cache->write($tssCache->getCacheKey($data).$this->template, $result); |
|
| 73 | 73 | } |
| 74 | 74 | unset($result['cache'], $result['renderTime']); |
| 75 | 75 | return (object) $result; |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
| 80 | 80 | $functionSet = new FunctionSet($elementData); |
| 81 | 81 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
| 82 | - $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' ); |
|
| 82 | + $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>'.$body.'</template>'); |
|
| 83 | 83 | |
| 84 | 84 | $valueParser = new Parser\Value($functionSet); |
| 85 | 85 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | private function loadTemplateFromFile($file) { |
| 106 | - $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 106 | + $xml = $this->cache->load($this->cacheKey.$this->template, filemtime($this->template)); |
|
| 107 | 107 | return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | public function parse($indexStart = 0) { |
| 27 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
| 27 | + if (!empty($this->rules)) { |
|
| 28 | + return $this->rules['rules']; |
|
| 29 | + } |
|
| 28 | 30 | $rules = $this->parseTokens($indexStart); |
| 29 | 31 | $this->checkError($rules); |
| 30 | 32 | return $rules; |
@@ -35,8 +37,9 @@ discard block |
||
| 35 | 37 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
| 36 | 38 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
| 37 | 39 | $this->rules = array_merge($this->rules, $processing); |
| 40 | + } else if (!in_array($token['type'], [Tokenizer::NEW_LINE, Tokenizer::AT_SIGN])) { |
|
| 41 | + $this->addRules($token, $indexStart++); |
|
| 38 | 42 | } |
| 39 | - else if (!in_array($token['type'], [Tokenizer::NEW_LINE, Tokenizer::AT_SIGN])) $this->addRules($token, $indexStart++); |
|
| 40 | 43 | } |
| 41 | 44 | |
| 42 | 45 | return $this->rules; |
@@ -46,13 +49,17 @@ discard block |
||
| 46 | 49 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
| 47 | 50 | |
| 48 | 51 | $this->tss->skip(count($selector)); |
| 49 | - if (count($selector) === 0) return; |
|
| 52 | + if (count($selector) === 0) { |
|
| 53 | + return; |
|
| 54 | + } |
|
| 50 | 55 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
| 51 | 56 | $this->rules = $this->writeRule($this->rules, $newRules); |
| 52 | 57 | } |
| 53 | 58 | |
| 54 | 59 | private function checkError($rules) { |
| 55 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
| 60 | + if (empty($rules) && count($this->tss) > 0) { |
|
| 61 | + throw new \Exception('No TSS rules parsed'); |
|
| 62 | + } |
|
| 56 | 63 | } |
| 57 | 64 | |
| 58 | 65 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -79,7 +86,9 @@ discard block |
||
| 79 | 86 | } |
| 80 | 87 | |
| 81 | 88 | private function processingInstructions($token, $indexStart) { |
| 82 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
| 89 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 83 | 92 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
| 84 | 93 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
| 85 | 94 | $funcToks = $tokens->from(Tokenizer::NAME); |