@@ -42,6 +42,9 @@ discard block |
||
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | |
| 45 | + /** |
|
| 46 | + * @param \Transphporm\Template $template |
|
| 47 | + */ |
|
| 45 | 48 | public function processRules($template, \Transphporm\Config $config) { |
| 46 | 49 | $rules = $this->getRules($config->getCssToXpath(), $config->getValueParser()); |
| 47 | 50 | |
@@ -57,11 +60,20 @@ discard block |
||
| 57 | 60 | } |
| 58 | 61 | |
| 59 | 62 | //Load the TSS |
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param \Transphporm\Parser\CssToXpath $cssToXpath |
|
| 66 | + * @param \Transphporm\Parser\Value $valueParser |
|
| 67 | + */ |
|
| 60 | 68 | public function getRules($cssToXpath, $valueParser, $indexStart = 0) { |
| 61 | 69 | return $this->tss->getRules($cssToXpath, $valueParser, $this, $indexStart); |
| 62 | 70 | } |
| 63 | 71 | |
| 64 | 72 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param \Transphporm\Config $config |
|
| 76 | + */ |
|
| 65 | 77 | private function executeTssRule($rule, $template, $config) { |
| 66 | 78 | $rule->touch(); |
| 67 | 79 | |
@@ -7,17 +7,17 @@ |
||
| 7 | 7 | namespace Transphporm\SheetLoader; |
| 8 | 8 | //Separates out TSS file loading/caching from parsing |
| 9 | 9 | class SheetLoader { |
| 10 | - private $tss; |
|
| 11 | - private $filePath; |
|
| 12 | - private $time; |
|
| 13 | - private $import = []; |
|
| 14 | - |
|
| 15 | - public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) { |
|
| 16 | - $this->cache = $cache; |
|
| 17 | - $this->filePath = $filePath; |
|
| 18 | - $this->tss = $tss; |
|
| 19 | - $this->time = $time ?? time(); |
|
| 20 | - } |
|
| 10 | + private $tss; |
|
| 11 | + private $filePath; |
|
| 12 | + private $time; |
|
| 13 | + private $import = []; |
|
| 14 | + |
|
| 15 | + public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) { |
|
| 16 | + $this->cache = $cache; |
|
| 17 | + $this->filePath = $filePath; |
|
| 18 | + $this->tss = $tss; |
|
| 19 | + $this->time = $time ?? time(); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | 22 | //Allows controlling whether any updates are required to the template |
| 23 | 23 | //e.g. return false |
@@ -84,6 +84,6 @@ |
||
| 84 | 84 | |
| 85 | 85 | |
| 86 | 86 | private function sortPseudo($a, $b) { |
| 87 | - return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
| 87 | + return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
| 88 | 88 | } |
| 89 | 89 | } |
@@ -49,7 +49,9 @@ discard block |
||
| 49 | 49 | usort($rules, [$this, 'sortRules']); |
| 50 | 50 | |
| 51 | 51 | foreach ($rules as $rule) { |
| 52 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
| 52 | + if ($rule->shouldRun($this->time)) { |
|
| 53 | + $this->executeTssRule($rule, $template, $config); |
|
| 54 | + } |
|
| 53 | 55 | } |
| 54 | 56 | |
| 55 | 57 | //if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
@@ -74,10 +76,15 @@ discard block |
||
| 74 | 76 | |
| 75 | 77 | private function sortRules($a, $b) { |
| 76 | 78 | //If they have the same depth, compare on index |
| 77 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
| 79 | + if ($a->query === $b->query) { |
|
| 80 | + return $this->sortPseudo($a, $b); |
|
| 81 | + } |
|
| 78 | 82 | |
| 79 | - if ($a->depth === $b->depth) $property = 'index'; |
|
| 80 | - else $property = 'depth'; |
|
| 83 | + if ($a->depth === $b->depth) { |
|
| 84 | + $property = 'index'; |
|
| 85 | + } else { |
|
| 86 | + $property = 'depth'; |
|
| 87 | + } |
|
| 81 | 88 | |
| 82 | 89 | return ($a->$property < $b->$property) ? -1 : 1; |
| 83 | 90 | } |
@@ -12,6 +12,10 @@ |
||
| 12 | 12 | private $cache; |
| 13 | 13 | private $time; |
| 14 | 14 | |
| 15 | + /** |
|
| 16 | + * @param string $fileName |
|
| 17 | + * @param \Transphporm\Cache $cache |
|
| 18 | + */ |
|
| 15 | 19 | public function __construct($fileName, \Transphporm\FilePath $filePath, $cache, $time) { |
| 16 | 20 | $this->fileName = $fileName; |
| 17 | 21 | $this->filePath = $filePath; |
@@ -16,8 +16,8 @@ discard block |
||
| 16 | 16 | $this->fileName = $fileName; |
| 17 | 17 | $this->filePath = $filePath; |
| 18 | 18 | $this->cache = $cache; |
| 19 | - $this->time = $time ?? time(); |
|
| 20 | - $this->cacheName = $this->fileName; |
|
| 19 | + $this->time = $time ?? time(); |
|
| 20 | + $this->cacheName = $this->fileName; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | private function getRulesFromCache($file) { |
@@ -74,16 +74,16 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | //write the sheet to cache |
| 77 | - public function write($rules, $imports = []) { |
|
| 77 | + public function write($rules, $imports = []) { |
|
| 78 | 78 | |
| 79 | 79 | $existing = $this->cache->load($this->fileName, filemtime($this->fileName)); |
| 80 | 80 | if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
| 81 | 81 | $this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
| 82 | 82 | |
| 83 | 83 | return $rules; |
| 84 | - } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - //Gets the minimum update-frequency for a sheet's rules |
|
| 86 | + //Gets the minimum update-frequency for a sheet's rules |
|
| 87 | 87 | private function getMinUpdateFreq($rules) { |
| 88 | 88 | $min = \PHP_INT_MAX; |
| 89 | 89 | |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | public function updateRequired($data) { |
| 44 | - $this->cacheName = $this->getCacheKey($data) . $this->fileName; |
|
| 44 | + $this->cacheName = $this->getCacheKey($data).$this->fileName; |
|
| 45 | 45 | |
| 46 | 46 | $rules = $this->getRulesFromCache($this->fileName, $data); |
| 47 | 47 | //Nothing was cached or the TSS file has changed, update is required |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | $this->getRulesFromCache($this->fileName); |
| 58 | 58 | if ($this->cacheKey) { |
| 59 | 59 | $parser = new \Transphporm\Parser\Value($data); |
| 60 | - $x= $parser->parseTokens($this->cacheKey)[0]; |
|
| 61 | - $this->cacheName = $x . $this->tss; |
|
| 60 | + $x = $parser->parseTokens($this->cacheKey)[0]; |
|
| 61 | + $this->cacheName = $x.$this->tss; |
|
| 62 | 62 | return $x; |
| 63 | 63 | } |
| 64 | 64 | else return ''; |
@@ -29,7 +29,9 @@ discard block |
||
| 29 | 29 | if ($rules) { |
| 30 | 30 | foreach ($rules['import'] as $file) { |
| 31 | 31 | //Check that the import file hasn't been changed since the cache was written |
| 32 | - if (filemtime($file) > $rules['ctime']) return false; |
|
| 32 | + if (filemtime($file) > $rules['ctime']) { |
|
| 33 | + return false; |
|
| 34 | + } |
|
| 33 | 35 | } |
| 34 | 36 | } |
| 35 | 37 | |
@@ -45,10 +47,14 @@ discard block |
||
| 45 | 47 | |
| 46 | 48 | $rules = $this->getRulesFromCache($this->fileName, $data); |
| 47 | 49 | //Nothing was cached or the TSS file has changed, update is required |
| 48 | - if (empty($rules)) return true; |
|
| 50 | + if (empty($rules)) { |
|
| 51 | + return true; |
|
| 52 | + } |
|
| 49 | 53 | |
| 50 | 54 | //Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required |
| 51 | - if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true; |
|
| 55 | + if ($rules['ctime']+$rules['minFreq'] <= $this->time) { |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 52 | 58 | |
| 53 | 59 | return false; |
| 54 | 60 | } |
@@ -60,15 +66,19 @@ discard block |
||
| 60 | 66 | $x= $parser->parseTokens($this->cacheKey)[0]; |
| 61 | 67 | $this->cacheName = $x . $this->tss; |
| 62 | 68 | return $x; |
| 69 | + } else { |
|
| 70 | + return ''; |
|
| 63 | 71 | } |
| 64 | - else return ''; |
|
| 65 | 72 | } |
| 66 | 73 | |
| 67 | 74 | public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) { |
| 68 | 75 | $rules = $this->getRulesFromCache($this->fileName)['rules']; |
| 69 | 76 | $this->filePath->addPath(dirname(realpath($this->fileName))); |
| 70 | - if (empty($rules)) $tss = file_get_contents($this->fileName); |
|
| 71 | - else return $rules; |
|
| 77 | + if (empty($rules)) { |
|
| 78 | + $tss = file_get_contents($this->fileName); |
|
| 79 | + } else { |
|
| 80 | + return $rules; |
|
| 81 | + } |
|
| 72 | 82 | |
| 73 | 83 | return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart); |
| 74 | 84 | } |
@@ -77,7 +87,9 @@ discard block |
||
| 77 | 87 | public function write($rules, $imports = []) { |
| 78 | 88 | |
| 79 | 89 | $existing = $this->cache->load($this->fileName, filemtime($this->fileName)); |
| 80 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
| 90 | + if (isset($existing['import']) && empty($imports)) { |
|
| 91 | + $imports = $existing['import']; |
|
| 92 | + } |
|
| 81 | 93 | $this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
| 82 | 94 | |
| 83 | 95 | return $rules; |
@@ -89,7 +101,9 @@ discard block |
||
| 89 | 101 | |
| 90 | 102 | foreach ($rules as $rule) { |
| 91 | 103 | $ruleFreq = $rule->getUpdateFrequency(); |
| 92 | - if ($ruleFreq < $min) $min = $ruleFreq; |
|
| 104 | + if ($ruleFreq < $min) { |
|
| 105 | + $min = $ruleFreq; |
|
| 106 | + } |
|
| 93 | 107 | } |
| 94 | 108 | |
| 95 | 109 | return $min; |
@@ -9,6 +9,9 @@ discard block |
||
| 9 | 9 | private $str; |
| 10 | 10 | private $filePath; |
| 11 | 11 | |
| 12 | + /** |
|
| 13 | + * @param string $str |
|
| 14 | + */ |
|
| 12 | 15 | public function __construct($str, $filePath) { |
| 13 | 16 | $this->str = $str; |
| 14 | 17 | $this->filePath = $filePath; |
@@ -22,6 +25,11 @@ discard block |
||
| 22 | 25 | return ''; |
| 23 | 26 | } |
| 24 | 27 | |
| 28 | + /** |
|
| 29 | + * @param \Transphporm\Parser\CssToXpath $cssToXpath |
|
| 30 | + * @param \Transphporm\Parser\Value $valueParser |
|
| 31 | + * @param SheetLoader $sheetLoader |
|
| 32 | + */ |
|
| 25 | 33 | public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) { |
| 26 | 34 | return (new \Transphporm\Parser\Sheet($this->str, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart); |
| 27 | 35 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | 'headers' => array_merge($result['headers'], $headers), |
| 73 | 73 | 'body' => $this->doPostProcessing($template)->output($document) |
| 74 | 74 | ]; |
| 75 | - $this->cache->write($tssCache->getCacheKey($data) . $this->template, $result); |
|
| 75 | + $this->cache->write($tssCache->getCacheKey($data).$this->template, $result); |
|
| 76 | 76 | } |
| 77 | 77 | unset($result['cache'], $result['renderTime']); |
| 78 | 78 | return (object) $result; |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
| 83 | 83 | $functionSet = new FunctionSet($elementData); |
| 84 | 84 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
| 85 | - $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' ); |
|
| 85 | + $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>'.$body.'</template>'); |
|
| 86 | 86 | |
| 87 | 87 | $valueParser = new Parser\Value($functionSet); |
| 88 | 88 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | private function loadTemplateFromFile($file) { |
| 109 | - $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template)); |
|
| 109 | + $xml = $this->cache->load($this->cacheKey.$this->template, filemtime($this->template)); |
|
| 110 | 110 | return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
| 111 | 111 | } |
| 112 | 112 | |
@@ -100,14 +100,14 @@ |
||
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | private function getProperties($tokens) { |
| 103 | - $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
|
| 103 | + $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
|
| 104 | 104 | |
| 105 | - $return = []; |
|
| 106 | - foreach ($rules as $rule) { |
|
| 107 | - $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
|
| 108 | - $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
| 109 | - } |
|
| 105 | + $return = []; |
|
| 106 | + foreach ($rules as $rule) { |
|
| 107 | + $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
|
| 108 | + $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - return $return; |
|
| 112 | - } |
|
| 111 | + return $return; |
|
| 112 | + } |
|
| 113 | 113 | } |