@@ -7,17 +7,17 @@ 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 | - |
|
| 15 | - public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
| 16 | - $this->cache = $cache; |
|
| 17 | - $this->filePath = $filePath; |
|
| 18 | - $this->tss = $tss; |
|
| 19 | - $this->time = $time ?? time(); |
|
| 20 | - } |
|
| 10 | + private $cache; |
|
| 11 | + private $sheet; |
|
| 12 | + private $time; |
|
| 13 | + private $import = []; |
|
| 14 | + |
|
| 15 | + public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
| 16 | + $this->cache = $cache; |
|
| 17 | + $this->filePath = $filePath; |
|
| 18 | + $this->tss = $tss; |
|
| 19 | + $this->time = $time ?? time(); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | 22 | private function getRulesFromCache($file) { |
| 23 | 23 | $key = $file; |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | return dirname(realpath($file)); |
| 71 | 71 | } |
| 72 | 72 | //write the sheet to cache |
| 73 | - public function write($file, $rules, $imports = []) { |
|
| 73 | + public function write($file, $rules, $imports = []) { |
|
| 74 | 74 | |
| 75 | 75 | if (is_file($file)) { |
| 76 | 76 | $key = $this->getCacheKey($file); |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $this->cache->write($file, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => time()]); |
| 80 | 80 | } |
| 81 | 81 | return $rules; |
| 82 | - } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | public function processRules($template, \Transphporm\Config $config) { |
| 85 | 85 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
@@ -96,12 +96,12 @@ discard block |
||
| 96 | 96 | //Load the TSS |
| 97 | 97 | public function getRules($tss, $cssToXpath, $valueParser) { |
| 98 | 98 | if (is_file($tss)) { |
| 99 | - //$rules = $this->cache->load($tss); |
|
| 100 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 99 | + //$rules = $this->cache->load($tss); |
|
| 100 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 101 | 101 | $this->filePath->addPath(dirname(realpath($tss))); |
| 102 | 102 | if (empty($rules)) $tss = file_get_contents($tss); |
| 103 | 103 | else return $rules; |
| 104 | - } |
|
| 104 | + } |
|
| 105 | 105 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
| 106 | 106 | } |
| 107 | 107 | |
@@ -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 | return $rules; |
@@ -40,12 +42,18 @@ discard block |
||
| 40 | 42 | // 2. If the data hasn't changed since the last run |
| 41 | 43 | //If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code |
| 42 | 44 | public function updateRequired($data) { |
| 43 | - if (!is_file($this->tss)) return true; |
|
| 45 | + if (!is_file($this->tss)) { |
|
| 46 | + return true; |
|
| 47 | + } |
|
| 44 | 48 | $rules = $this->getRulesFromCache($this->tss); |
| 45 | 49 | //Nothing was cached or the TSS file has changed, update is required |
| 46 | - if (empty($rules)) return true; |
|
| 50 | + if (empty($rules)) { |
|
| 51 | + return true; |
|
| 52 | + } |
|
| 47 | 53 | //Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required |
| 48 | - if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true; |
|
| 54 | + if ($rules['ctime']+$rules['minFreq'] <= $this->time) { |
|
| 55 | + return true; |
|
| 56 | + } |
|
| 49 | 57 | |
| 50 | 58 | return false; |
| 51 | 59 | } |
@@ -56,7 +64,9 @@ discard block |
||
| 56 | 64 | |
| 57 | 65 | foreach ($rules as $rule) { |
| 58 | 66 | $ruleFreq = $rule->getUpdateFrequency(); |
| 59 | - if ($ruleFreq < $min) $min = $ruleFreq; |
|
| 67 | + if ($ruleFreq < $min) { |
|
| 68 | + $min = $ruleFreq; |
|
| 69 | + } |
|
| 60 | 70 | } |
| 61 | 71 | |
| 62 | 72 | return $min; |
@@ -75,7 +85,9 @@ discard block |
||
| 75 | 85 | if (is_file($file)) { |
| 76 | 86 | $key = $this->getCacheKey($file); |
| 77 | 87 | $existing = $this->cache->load($key, filemtime($file)); |
| 78 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
| 88 | + if (isset($existing['import']) && empty($imports)) { |
|
| 89 | + $imports = $existing['import']; |
|
| 90 | + } |
|
| 79 | 91 | $this->cache->write($file, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => time()]); |
| 80 | 92 | } |
| 81 | 93 | return $rules; |
@@ -87,10 +99,14 @@ discard block |
||
| 87 | 99 | usort($rules, [$this, 'sortRules']); |
| 88 | 100 | |
| 89 | 101 | foreach ($rules as $rule) { |
| 90 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
| 102 | + if ($rule->shouldRun($this->time)) { |
|
| 103 | + $this->executeTssRule($rule, $template, $config); |
|
| 104 | + } |
|
| 91 | 105 | } |
| 92 | 106 | |
| 93 | - if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
| 107 | + if (is_file($this->tss)) { |
|
| 108 | + $this->write($this->tss, $rules, $this->import); |
|
| 109 | + } |
|
| 94 | 110 | } |
| 95 | 111 | |
| 96 | 112 | //Load the TSS |
@@ -99,8 +115,11 @@ discard block |
||
| 99 | 115 | //$rules = $this->cache->load($tss); |
| 100 | 116 | $rules = $this->getRulesFromCache($tss)['rules']; |
| 101 | 117 | $this->filePath->addPath(dirname(realpath($tss))); |
| 102 | - if (empty($rules)) $tss = file_get_contents($tss); |
|
| 103 | - else return $rules; |
|
| 118 | + if (empty($rules)) { |
|
| 119 | + $tss = file_get_contents($tss); |
|
| 120 | + } else { |
|
| 121 | + return $rules; |
|
| 122 | + } |
|
| 104 | 123 | } |
| 105 | 124 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
| 106 | 125 | } |
@@ -118,10 +137,15 @@ discard block |
||
| 118 | 137 | |
| 119 | 138 | private function sortRules($a, $b) { |
| 120 | 139 | //If they have the same depth, compare on index |
| 121 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
| 140 | + if ($a->query === $b->query) { |
|
| 141 | + return $this->sortPseudo($a, $b); |
|
| 142 | + } |
|
| 122 | 143 | |
| 123 | - if ($a->depth === $b->depth) $property = 'index'; |
|
| 124 | - else $property = 'depth'; |
|
| 144 | + if ($a->depth === $b->depth) { |
|
| 145 | + $property = 'index'; |
|
| 146 | + } else { |
|
| 147 | + $property = 'depth'; |
|
| 148 | + } |
|
| 125 | 149 | |
| 126 | 150 | return ($a->$property < $b->$property) ? -1 : 1; |
| 127 | 151 | } |