Completed
Push — master ( 04a4d1...1bb4b5 )
by Tom
01:56
created

SheetLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 6
nc 1
nop 5
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 $prefix;
12
    private $sheet;
0 ignored issues
show
Unused Code introduced by
The property $sheet is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
    private $time;
14
    private $import = [];
15
16
    public function __construct(Cache $cache, FilePath $filePath, $tss, $prefix, $time) {
17
        $this->cache = $cache;
18
        $this->filePath = $filePath;
0 ignored issues
show
Bug introduced by
The property filePath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        $this->prefix = $prefix;
20
        $this->tss = $tss;
0 ignored issues
show
Bug introduced by
The property tss does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $this->time = $time;
22
    }
23
24
	private function getRulesFromCache($file) {
25
		//The cache for the key: the filename and template prefix
26
		//Each template may have a different prefix which changes the parsed TSS,
27
		//Because of this the cache needs to be generated for each template prefix.
28
		$key = $this->getCacheKey($file);
29
		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
30
		$rules = $this->cache->load($key, filemtime($file));
31
		if ($rules) {
32
			foreach ($rules['import'] as $file) {
33
				if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false;
34
			}
35
		}
36
		return $rules;
37
	}
38
39
	public function addImport($import) {
40
		$this->import[] = $import;
41
	}
42
43
	private function getCacheKey($file) {
44
		return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR;
45
	}
46
	//write the sheet to cache
47
    public function write($file, $rules, $imports = []) {
48
		if (is_file($file)) {
49
			$key = $this->getCacheKey($file);
50
			$existing = $this->cache->load($key, filemtime($file));
51
			if (isset($existing['import']) && empty($imports)) $imports = $existing['import'];
52
			$this->cache->write($key, ['rules' => $rules, 'import' => $imports]);
53
		}
54
		return $rules;
55
    }
56
57
	public function processRules($template, \Transphporm\Config $config) {
58
		$rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser());
59
60
		usort($rules, [$this, 'sortRules']);
61
62
		foreach ($rules as $rule) {
63
			if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config);
64
		}
65
66
		if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import);
67
	}
68
69
	//Load the TSS
70
	public function getRules($tss, $cssToXpath, $valueParser) {
71
		if (is_file($tss)) {
72
    		//$rules = $this->cache->load($tss);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
    		$rules = $this->getRulesFromCache($tss)['rules'];
74
			$this->filePath->addPath(dirname(realpath($tss)));
75
			if (empty($rules)) $tss = file_get_contents($tss);
76
			else return $rules;
77
    	}
78
		return (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse();
79
	}
80
81
	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
82
	private function executeTssRule($rule, $template, $config) {
83
		$rule->touch();
84
85
		$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo);
86
		$hook = new Hook\PropertyHook($rule->properties, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath());
0 ignored issues
show
Bug introduced by
$config->getLine() cannot be passed to __construct() as the parameter $configLine expects a reference.
Loading history...
87
		$config->loadProperties($hook);
88
		$template->addHook($rule->query, $hook);
89
	}
90
91
92
	private function sortRules($a, $b) {
93
		//If they have the same depth, compare on index
94
		if ($a->query === $b->query) return $this->sortPseudo($a, $b);
95
96
		if ($a->depth === $b->depth) $property = 'index';
97
		else $property = 'depth';
98
99
		return ($a->$property < $b->$property) ? -1 : 1;
100
	}
101
102
103
	private function sortPseudo($a, $b) {
104
		return count($a->pseudo) < count($b->pseudo)  ? -1  :1;
105
	}
106
}
107