|
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; |
|
8
|
|
|
//Separates out TSS file loading/caching from parsing |
|
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; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function getRulesFromCache($file) { |
|
23
|
|
|
//The cache for the key: the filename and template prefix |
|
24
|
|
|
//Each template may have a different prefix which changes the parsed TSS, |
|
25
|
|
|
//Because of this the cache needs to be generated for each template prefix. |
|
26
|
|
|
$key = $this->getCacheKey($file); |
|
27
|
|
|
//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
|
28
|
|
|
$rules = $this->cache->load($key, filemtime($file)); |
|
29
|
|
|
if ($rules) { |
|
30
|
|
|
foreach ($rules['import'] as $file) { |
|
31
|
|
|
if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
return $rules; |
|
35
|
|
|
} |
|
36
|
|
|
//Allows controlling whether any updates are required to the template |
|
37
|
|
|
//e.g. return false |
|
38
|
|
|
// 1. If all update-frequencies haven't expired |
|
39
|
|
|
// 2. If the data hasn't changed since the last run |
|
40
|
|
|
public function updateRequired($data) { |
|
|
|
|
|
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function addImport($import) { |
|
45
|
|
|
$this->import[] = $import; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function getCacheKey($file) { |
|
49
|
|
|
return $file . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
|
50
|
|
|
} |
|
51
|
|
|
//write the sheet to cache |
|
52
|
|
|
public function write($file, $rules, $imports = []) { |
|
53
|
|
|
if (is_file($file)) { |
|
54
|
|
|
$key = $this->getCacheKey($file); |
|
55
|
|
|
$existing = $this->cache->load($key, filemtime($file)); |
|
56
|
|
|
if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
57
|
|
|
$this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
|
58
|
|
|
} |
|
59
|
|
|
return $rules; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function processRules($template, \Transphporm\Config $config) { |
|
63
|
|
|
$rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
|
64
|
|
|
|
|
65
|
|
|
usort($rules, [$this, 'sortRules']); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($rules as $rule) { |
|
68
|
|
|
if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
//Load the TSS |
|
75
|
|
|
public function getRules($tss, $cssToXpath, $valueParser) { |
|
76
|
|
|
if (is_file($tss)) { |
|
77
|
|
|
//$rules = $this->cache->load($tss); |
|
|
|
|
|
|
78
|
|
|
$rules = $this->getRulesFromCache($tss)['rules']; |
|
79
|
|
|
$this->filePath->addPath(dirname(realpath($tss))); |
|
80
|
|
|
if (empty($rules)) $tss = file_get_contents($tss); |
|
81
|
|
|
else return $rules; |
|
82
|
|
|
} |
|
83
|
|
|
return (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
//Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
|
87
|
|
|
private function executeTssRule($rule, $template, $config) { |
|
88
|
|
|
$rule->touch(); |
|
89
|
|
|
|
|
90
|
|
|
$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo); |
|
91
|
|
|
$hook = new Hook\PropertyHook($rule->properties, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath()); |
|
|
|
|
|
|
92
|
|
|
$config->loadProperties($hook); |
|
93
|
|
|
$template->addHook($rule->query, $hook); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
private function sortRules($a, $b) { |
|
98
|
|
|
//If they have the same depth, compare on index |
|
99
|
|
|
if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
100
|
|
|
|
|
101
|
|
|
if ($a->depth === $b->depth) $property = 'index'; |
|
102
|
|
|
else $property = 'depth'; |
|
103
|
|
|
|
|
104
|
|
|
return ($a->$property < $b->$property) ? -1 : 1; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
private function sortPseudo($a, $b) { |
|
109
|
|
|
return count($a->pseudo) < count($b->pseudo) ? -1 :1; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.