Completed
Push — master ( e38a6e...39b3df )
by Richard
02:41
created

TSSCache::getRulesFromCache()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
namespace Transphporm;
3
class TSSCache {
4
    private $cache;
5
    private $prefix;
6
7
    public function __construct(Cache $cache, $prefix) {
8
        $this->cache = $cache;
9
        $this->prefix = $prefix;
10
    }
11
12
	private function getRulesFromCache($file) {
13
		//The cache for the key: the filename and template prefix
14
		//Each template may have a different prefix which changes the parsed TSS,
15
		//Because of this the cache needs to be generated for each template prefix.
16
		$key = $this->getCacheKey($file);
17
		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
18
		$rules = $this->cache->load($key, filemtime($file));
19
		if ($rules) {
20
			foreach ($rules['import'] as $file) {
21
				if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false;
22
			}
23
		}
24
		return $rules;
25
	}
26
27
	private function getCacheKey($file) {
28
		return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR;
29
	}
30
31
    public function load($tss) {
32
        return $this->getRulesFromCache($tss);
33
    }
34
35
    public function write($file, $rules, $imports = []) {
36
        if (is_file($file)) $this->cache->write($this->getCacheKey($file), ['rules' => $rules, 'import' => $imports]);
37
        return $rules;
38
    }
39
}
40