TSSFile::getRulesFromCache()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
cc 6
nc 6
nop 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm\SheetLoader;
8
class TSSFile implements TSSRules {
9
	private $fileName;
10
	private $filePath;
11
	private $cacheName;
12
	private $cacheKey;
13
	private $cache;
14
	private $time;
15
16
	public function __construct($fileName, \Transphporm\FilePath $filePath, $cache, $time) {
17
		$this->fileName = $fileName;
18
		$this->filePath = $filePath;
19
		$this->cache = $cache;
20
	    $this->time = isset($time) ? $time : time();
21
	    $this->cacheName = $this->fileName;
22
	}
23
24
	private function getRulesFromCache($file) {
25
		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
26
		$rules = $this->cache->load($this->cacheName, filemtime($file));
27
28
		if (!isset($this->cacheKey) && isset($rules['cacheKey'])) {
29
			$this->cacheKey = $rules['cacheKey'];
30
		}
31
32
		if ($rules) {
33
			foreach ($rules['import'] as $file) {
34
				//Check that the import file hasn't been changed since the cache was written
35
				if (filemtime($file) > $rules['ctime']) return false;
36
			}
37
		}
38
39
		return $rules;
40
	}
41
42
	public function setCacheKey($tokens) {
43
		$this->cacheKey = $tokens;
44
	}
45
46
	public function updateRequired($data) {
47
		$this->cacheName = $this->getCacheKey($data);
48
49
		$rules = $this->getRulesFromCache($this->fileName);
50
		//Nothing was cached or the TSS file has changed, update is required
51
		if (empty($rules)) return true;
52
53
		//Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required
54
		if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true;
55
56
		return false;
57
	}
58
59
	public function getCacheKey($data) {
60
		$this->getRulesFromCache($this->fileName);
61
		if ($this->cacheKey) {
62
			$parser = new \Transphporm\Parser\Value($data);
63
			$cacheKey = $parser->parseTokens($this->cacheKey)[0];
64
			$this->cacheName = $cacheKey . $this->fileName;
65
			return $cacheKey;
66
		}
67
		else return $this->fileName;
68
	}
69
70
	public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) {
71
		$rules = $this->getRulesFromCache($this->fileName);
72
		$this->filePath->addPath(dirname(realpath($this->fileName)));
73
		if (empty($rules)) $tss = file_get_contents($this->fileName);
74
		else return $rules['rules'];
75
76
		return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
77
	}
78
79
	//write the sheet to cache
80
    public function write($rules, $imports = []) {
81
		$existing = $this->cache->load($this->fileName, filemtime($this->fileName));
82
		if (isset($existing['import']) && empty($imports)) $imports = $existing['import'];
83
		$this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]);
84
85
		return $rules;
86
    }
87
88
    //Gets the minimum update-frequency for a sheet's rules
89
	private function getMinUpdateFreq($rules) {
90
		$min = \PHP_INT_MAX;
91
92
		foreach ($rules as $rule) {
93
			$ruleFreq = $rule->getUpdateFrequency();
94
			if ($ruleFreq < $min) $min = $ruleFreq;
95
		}
96
97
		return $min;
98
	}
99
}