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 $cacheName; |
11
|
|
|
private $cacheKey; |
12
|
|
|
private $cache; |
13
|
|
|
private $time; |
14
|
|
|
|
15
|
|
|
public function __construct($fileName, \Transphporm\FilePath $filePath, $cache, $time) { |
16
|
|
|
$this->fileName = $fileName; |
17
|
|
|
$this->filePath = $filePath; |
|
|
|
|
18
|
|
|
$this->cache = $cache; |
19
|
|
|
$this->time = $time ?? time(); |
20
|
|
|
$this->cacheName = $this->fileName; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function getRulesFromCache($file) { |
24
|
|
|
//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
25
|
|
|
$rules = $this->cache->load($this->cacheName, filemtime($file)); |
26
|
|
|
|
27
|
|
|
$this->cacheKey = $this->cacheKey ?? $rules['cacheKey'] ?? null; |
28
|
|
|
|
29
|
|
|
if ($rules) { |
30
|
|
|
foreach ($rules['import'] as $file) { |
31
|
|
|
//Check that the import file hasn't been changed since the cache was written |
32
|
|
|
if (filemtime($file) > $rules['ctime']) return false; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $rules; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setCacheKey($tokens) { |
40
|
|
|
$this->cacheKey = $tokens; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function updateRequired($data) { |
44
|
|
|
$this->cacheName = $this->getCacheKey($data) . $this->fileName; |
45
|
|
|
|
46
|
|
|
$rules = $this->getRulesFromCache($this->fileName, $data); |
|
|
|
|
47
|
|
|
//Nothing was cached or the TSS file has changed, update is required |
48
|
|
|
if (empty($rules)) return true; |
49
|
|
|
|
50
|
|
|
//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; |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getCacheKey($data) { |
57
|
|
|
$this->getRulesFromCache($this->fileName); |
58
|
|
|
if ($this->cacheKey) { |
59
|
|
|
$parser = new \Transphporm\Parser\Value($data); |
60
|
|
|
$x= $parser->parseTokens($this->cacheKey)[0]; |
61
|
|
|
$this->cacheName = $x . $this->tss; |
|
|
|
|
62
|
|
|
return $x; |
63
|
|
|
} |
64
|
|
|
else return ''; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) { |
68
|
|
|
$rules = $this->getRulesFromCache($this->fileName)['rules']; |
69
|
|
|
$this->filePath->addPath(dirname(realpath($this->fileName))); |
70
|
|
|
if (empty($rules)) $tss = file_get_contents($this->fileName); |
71
|
|
|
else return $rules; |
72
|
|
|
|
73
|
|
|
return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//write the sheet to cache |
77
|
|
|
public function write($rules, $imports = []) { |
78
|
|
|
|
79
|
|
|
$existing = $this->cache->load($this->fileName, filemtime($this->fileName)); |
80
|
|
|
if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
81
|
|
|
$this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]); |
82
|
|
|
|
83
|
|
|
return $rules; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//Gets the minimum update-frequency for a sheet's rules |
87
|
|
|
private function getMinUpdateFreq($rules) { |
88
|
|
|
$min = \PHP_INT_MAX; |
89
|
|
|
|
90
|
|
|
foreach ($rules as $rule) { |
91
|
|
|
$ruleFreq = $rule->getUpdateFrequency(); |
92
|
|
|
if ($ruleFreq < $min) $min = $ruleFreq; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $min; |
96
|
|
|
} |
97
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: